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


##########
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)
+               }
+       }

Review Comment:
   ```suggestion
           for i := 0; i < fileDesc.Imports().Len(); i++ {
                   GetFileProto(fileDesc.Imports().Get(i).ParentFile(), 
protoMap)
           }
   ```
   
   Remove unnecessary check.



##########
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)

Review Comment:
   ```suggestion
           for _, v := range fileProtoMap {
                   fileDescList = append(fileDescList, v)
   ```
   
   Simplify the code. We don't need to copy the object that the pointer `v` 
references. Golang will handle the pointer well.



##########
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:
   +1, I didn't get why we need to check it again. I think we only need to 
implement it like:
   
   ```go
   func GetFileProto(fileDesc protoreflect.FileDescriptor, protoMap 
map[string]*descriptorpb.FileDescriptorProto) {
           for i := 0; i < fileDesc.Imports().Len(); i++ {
                   GetFileProto(fileDesc.Imports().Get(i).ParentFile(), 
protoMap)
           }
           protoMap[fileDesc.Path()] = protodesc.ToFileDescriptorProto(fileDesc)
   }
   ```



-- 
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