gaoran10 commented on code in PR #1006:
URL: https://github.com/apache/pulsar-client-go/pull/1006#discussion_r1168123702


##########
pulsar/schema.go:
##########
@@ -219,6 +226,107 @@ func (ps *ProtoSchema) GetSchemaInfo() *SchemaInfo {
        return &ps.SchemaInfo
 }
 
+type ProtoNativeSchema struct {
+       SchemaInfo
+}
+
+func NewProtoNativeSchemaWithMessage(message proto.Message, properties 
map[string]string) *ProtoNativeSchema {
+       return NewProtoNativeSchema(GetProtoNativeSchemaInfo(message), 
properties)
+}
+
+func NewProtoNativeSchema(protoNativeSchemaDef string, properties 
map[string]string) *ProtoNativeSchema {
+       pns := new(ProtoNativeSchema)
+       pns.SchemaInfo.Schema = protoNativeSchemaDef
+       pns.SchemaInfo.Type = ProtoNative
+       pns.SchemaInfo.Properties = properties
+       pns.SchemaInfo.Name = "ProtoNative"
+       return pns
+}
+
+func GetProtoNativeSchemaInfo(message proto.Message) string {
+       fileDesc := message.ProtoReflect().Descriptor().ParentFile()
+       fileProtoMap := make(map[string]*descriptorpb.FileDescriptorProto)
+       GetFileProto(fileDesc, fileProtoMap)
+
+       fileDescList := make([]*descriptorpb.FileDescriptorProto, 0, 
len(fileProtoMap))
+       for k := range fileProtoMap {
+               value := descriptorpb.FileDescriptorProto{
+                       Name:             fileProtoMap[k].Name,
+                       Package:          fileProtoMap[k].Package,
+                       Dependency:       fileProtoMap[k].Dependency,
+                       PublicDependency: fileProtoMap[k].PublicDependency,
+                       MessageType:      fileProtoMap[k].MessageType,
+                       EnumType:         fileProtoMap[k].EnumType,
+                       Service:          fileProtoMap[k].Service,
+                       Extension:        fileProtoMap[k].Extension,
+                       Options:          fileProtoMap[k].Options,
+                       SourceCodeInfo:   fileProtoMap[k].SourceCodeInfo,
+                       Syntax:           fileProtoMap[k].Syntax,
+               }
+               fileDescList = append(fileDescList, &value)
+       }
+       fileDescSet := descriptorpb.FileDescriptorSet{
+               File: fileDescList,
+       }
+       bytesData, err := proto.Marshal(&fileDescSet)
+       if err != nil {
+               log.Fatalf("get serialized proto file data error: %v", err)
+       }
+       schemaData := ProtoNativeSchemaData{
+               FileDescriptorSet:      bytesData,
+               RootMessageTypeName:    
string(message.ProtoReflect().Descriptor().FullName()),
+               RootFileDescriptorName: fileDesc.Path(),
+       }
+       jsonData, err := json.Marshal(schemaData)
+       if err != nil {
+               log.Fatalf("get json schema data for proto native schema error: 
%v", err)
+       }
+       return string(jsonData)
+}
+
+type ProtoNativeSchemaData struct {
+       FileDescriptorSet      []byte `json:"fileDescriptorSet"`
+       RootMessageTypeName    string `json:"rootMessageTypeName"`
+       RootFileDescriptorName string `json:"rootFileDescriptorName"`
+}
+
+func GetFileProto(fileDesc protoreflect.FileDescriptor, protoMap 
map[string]*descriptorpb.FileDescriptorProto) {
+       if fileDesc.Imports().Len() > 0 {
+               for i := 0; i < fileDesc.Imports().Len(); i++ {
+                       GetFileProto(fileDesc.Imports().Get(i).ParentFile(), 
protoMap)
+               }
+       }
+       unResolvedDependencies := make([]string, 0)

Review Comment:
   I think so, but I found the Java client also has this 
[check](https://github.com/apache/pulsar/blob/master/pulsar-client/src/main/java/org/apache/pulsar/client/impl/schema/ProtobufNativeSchemaUtils.java#L80),
 I'm not sure if there is any potential problem.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to