DMwangnima commented on code in PR #2654:
URL: https://github.com/apache/dubbo-go/pull/2654#discussion_r1554627219


##########
cmd/protoc-gen-go-hessian2/generate/generate.go:
##########
@@ -0,0 +1,156 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package generate
+
+import (
+       "fmt"
+)
+
+import (
+       "google.golang.org/protobuf/compiler/protogen"
+       "google.golang.org/protobuf/reflect/protoreflect"
+       "google.golang.org/protobuf/types/descriptorpb"
+)
+
+func GenHessian2(gen *protogen.Plugin, file *protogen.File) {
+       filename := file.GeneratedFilenamePrefix + ".hessian2.go"
+       g := gen.NewGeneratedFile(filename, file.GoImportPath)
+
+       g.P("package ", file.GoPackageName)
+       g.P()
+
+       g.P("import hessian \"github.com/apache/dubbo-go-hessian2\"")
+       g.P()
+
+       for _, message := range file.Messages {
+               genMessage(g, message)
+       }
+       genRegisterInitFunc(g, file)
+}
+
+func genMessage(g *protogen.GeneratedFile, m *protogen.Message) {
+       if m.Desc.IsMapEntry() {
+               return
+       }
+       g.AnnotateSymbol(m.GoIdent.String(), protogen.Annotation{
+               Location: m.Location,
+               Semantic: descriptorpb.GeneratedCodeInfo_Annotation_SET.Enum(),
+       })
+
+       g.P("type ", m.GoIdent, " struct {")
+
+       genMessageFields(g, m)
+       g.P("}")
+       g.P()
+
+       genMessageRelatedMethods(g, m)
+}
+
+func genMessageFields(g *protogen.GeneratedFile, m *protogen.Message) {
+       for _, field := range m.Fields {
+               genMessageField(g, m, field)
+       }
+}
+
+func genMessageField(g *protogen.GeneratedFile, m *protogen.Message, field 
*protogen.Field) {
+       goType, pointer := fieldGoType(g, field)
+       if pointer {
+               goType = "*" + goType
+       }
+
+       name := field.GoName
+       g.AnnotateSymbol(m.GoIdent.GoName+"."+name, protogen.Annotation{
+               Location: field.Location,
+               Semantic: descriptorpb.GeneratedCodeInfo_Annotation_SET.Enum(),
+       })
+       g.P(name, " ", goType)
+}
+
+func genMessageRelatedMethods(g *protogen.GeneratedFile, m *protogen.Message) {
+       g.P("func ", "(x *", m.GoIdent.GoName, ")", "JavaClassName() string {")
+       // TODO(Yuukirn): get class name by extend field
+       g.P("   return ", "\"org.test.service.", m.GoIdent.GoName, "\"")
+       g.P("}")
+
+       g.P()
+
+       g.P("func ", "(x *", m.GoIdent.GoName, ")", "String() string {")

Review Comment:
   For now, we do not need this ```String()``` method since dubbo-go-hessian2 
just make use of reflection to serialize.
   In the future, we could generate serialization details to make it fast. 
(hessian2 FastCodec)



##########
cmd/protoc-gen-go-hessian2/generate/generate.go:
##########
@@ -0,0 +1,156 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package generate
+
+import (
+       "fmt"
+)
+
+import (
+       "google.golang.org/protobuf/compiler/protogen"
+       "google.golang.org/protobuf/reflect/protoreflect"
+       "google.golang.org/protobuf/types/descriptorpb"
+)
+
+func GenHessian2(gen *protogen.Plugin, file *protogen.File) {
+       filename := file.GeneratedFilenamePrefix + ".hessian2.go"
+       g := gen.NewGeneratedFile(filename, file.GoImportPath)
+
+       g.P("package ", file.GoPackageName)
+       g.P()
+
+       g.P("import hessian \"github.com/apache/dubbo-go-hessian2\"")
+       g.P()
+
+       for _, message := range file.Messages {
+               genMessage(g, message)
+       }
+       genRegisterInitFunc(g, file)
+}
+
+func genMessage(g *protogen.GeneratedFile, m *protogen.Message) {
+       if m.Desc.IsMapEntry() {
+               return
+       }
+       g.AnnotateSymbol(m.GoIdent.String(), protogen.Annotation{
+               Location: m.Location,
+               Semantic: descriptorpb.GeneratedCodeInfo_Annotation_SET.Enum(),
+       })
+
+       g.P("type ", m.GoIdent, " struct {")
+
+       genMessageFields(g, m)
+       g.P("}")
+       g.P()
+
+       genMessageRelatedMethods(g, m)
+}
+
+func genMessageFields(g *protogen.GeneratedFile, m *protogen.Message) {
+       for _, field := range m.Fields {
+               genMessageField(g, m, field)
+       }
+}
+
+func genMessageField(g *protogen.GeneratedFile, m *protogen.Message, field 
*protogen.Field) {
+       goType, pointer := fieldGoType(g, field)
+       if pointer {
+               goType = "*" + goType
+       }
+
+       name := field.GoName
+       g.AnnotateSymbol(m.GoIdent.GoName+"."+name, protogen.Annotation{
+               Location: field.Location,
+               Semantic: descriptorpb.GeneratedCodeInfo_Annotation_SET.Enum(),
+       })
+       g.P(name, " ", goType)
+}
+
+func genMessageRelatedMethods(g *protogen.GeneratedFile, m *protogen.Message) {
+       g.P("func ", "(x *", m.GoIdent.GoName, ")", "JavaClassName() string {")
+       // TODO(Yuukirn): get class name by extend field
+       g.P("   return ", "\"org.test.service.", m.GoIdent.GoName, "\"")
+       g.P("}")
+
+       g.P()
+
+       g.P("func ", "(x *", m.GoIdent.GoName, ")", "String() string {")
+       g.P("   e := hessian.NewEncoder()")
+       g.P("   err := e.Encode(x)")
+       g.P("   if err != nil {")
+       g.P("           return \"\"")
+       g.P("   }")
+       g.P("   return string(e.Buffer())")
+       g.P("}")
+       g.P()
+}
+
+func genRegisterInitFunc(g *protogen.GeneratedFile, f *protogen.File) {
+       g.P("func init() {")
+       for _, message := range f.Messages {
+               g.P("hessian.RegisterPOJO(new(", message.GoIdent.GoName, "))")
+       }
+       g.P("}")
+       g.P()
+}
+
+// fieldGoType returns the Go type used for a field.
+//
+// If it returns pointer=true, the struct field is a pointer to the type.
+func fieldGoType(g *protogen.GeneratedFile, field *protogen.Field) (goType 
string, pointer bool) {
+       if field.Desc.IsWeak() {
+               return "struct{}", false
+       }
+
+       pointer = field.Desc.HasPresence()
+       switch field.Desc.Kind() {

Review Comment:
   We should discuss the mappings in details.
   type serialization conversion: proto type => go type => java type
   helping tool conversion: java interface file => proto file
   e.g. Java does not support unsigned basic type, it would be complex when 
implementing helping tool if we decide to support unsigned basic type in proto 
and go.



##########
cmd/protoc-gen-go-hessian2/generate/generate.go:
##########
@@ -0,0 +1,156 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package generate
+
+import (
+       "fmt"
+)
+
+import (
+       "google.golang.org/protobuf/compiler/protogen"
+       "google.golang.org/protobuf/reflect/protoreflect"
+       "google.golang.org/protobuf/types/descriptorpb"
+)
+
+func GenHessian2(gen *protogen.Plugin, file *protogen.File) {
+       filename := file.GeneratedFilenamePrefix + ".hessian2.go"
+       g := gen.NewGeneratedFile(filename, file.GoImportPath)
+
+       g.P("package ", file.GoPackageName)
+       g.P()
+
+       g.P("import hessian \"github.com/apache/dubbo-go-hessian2\"")
+       g.P()
+
+       for _, message := range file.Messages {
+               genMessage(g, message)
+       }
+       genRegisterInitFunc(g, file)
+}
+
+func genMessage(g *protogen.GeneratedFile, m *protogen.Message) {
+       if m.Desc.IsMapEntry() {
+               return
+       }
+       g.AnnotateSymbol(m.GoIdent.String(), protogen.Annotation{
+               Location: m.Location,
+               Semantic: descriptorpb.GeneratedCodeInfo_Annotation_SET.Enum(),
+       })
+
+       g.P("type ", m.GoIdent, " struct {")
+
+       genMessageFields(g, m)
+       g.P("}")
+       g.P()
+
+       genMessageRelatedMethods(g, m)
+}
+
+func genMessageFields(g *protogen.GeneratedFile, m *protogen.Message) {
+       for _, field := range m.Fields {
+               genMessageField(g, m, field)
+       }
+}
+
+func genMessageField(g *protogen.GeneratedFile, m *protogen.Message, field 
*protogen.Field) {
+       goType, pointer := fieldGoType(g, field)
+       if pointer {
+               goType = "*" + goType
+       }
+
+       name := field.GoName
+       g.AnnotateSymbol(m.GoIdent.GoName+"."+name, protogen.Annotation{
+               Location: field.Location,
+               Semantic: descriptorpb.GeneratedCodeInfo_Annotation_SET.Enum(),
+       })
+       g.P(name, " ", goType)
+}
+
+func genMessageRelatedMethods(g *protogen.GeneratedFile, m *protogen.Message) {
+       g.P("func ", "(x *", m.GoIdent.GoName, ")", "JavaClassName() string {")
+       // TODO(Yuukirn): get class name by extend field
+       g.P("   return ", "\"org.test.service.", m.GoIdent.GoName, "\"")
+       g.P("}")
+
+       g.P()
+
+       g.P("func ", "(x *", m.GoIdent.GoName, ")", "String() string {")
+       g.P("   e := hessian.NewEncoder()")
+       g.P("   err := e.Encode(x)")
+       g.P("   if err != nil {")
+       g.P("           return \"\"")
+       g.P("   }")
+       g.P("   return string(e.Buffer())")
+       g.P("}")
+       g.P()
+}
+
+func genRegisterInitFunc(g *protogen.GeneratedFile, f *protogen.File) {
+       g.P("func init() {")
+       for _, message := range f.Messages {
+               g.P("hessian.RegisterPOJO(new(", message.GoIdent.GoName, "))")
+       }
+       g.P("}")
+       g.P()
+}
+
+// fieldGoType returns the Go type used for a field.
+//
+// If it returns pointer=true, the struct field is a pointer to the type.
+func fieldGoType(g *protogen.GeneratedFile, field *protogen.Field) (goType 
string, pointer bool) {

Review Comment:
   Maybe ```isPointer``` is better?



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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to