Skip to content

Commit

Permalink
fix: Incompatibility with custom type annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
ahaostudy committed Oct 27, 2023
1 parent 74c9e5c commit afd2571
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 24 deletions.
14 changes: 2 additions & 12 deletions pkg/hessian2/annotation.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,8 @@ func NewTypeAnnotation(anno string) *TypeAnnotation {

// GetFieldType retrieves the type annotation for a field by its index.
func (ta *TypeAnnotation) GetFieldType(i int) string {
if ta != nil && len(ta.getFieldTypes()) > i {
return ta.getFieldTypes()[i]
if ta != nil && len(ta.fieldTypes) > i {
return ta.fieldTypes[i]
}
return ""
}

// getTypes is used to retrieve the list of types from the type annotation.
// This function will first return the existing type list if it exists. If not, it will parse the annotation string,
// store the parsing result, and then return it.
func (ta *TypeAnnotation) getFieldTypes() []string {
if ta == nil {
return nil
}
return ta.fieldTypes
}
9 changes: 3 additions & 6 deletions pkg/hessian2/parameter.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,14 +192,11 @@ func (p *parameter) getTypeByAnno() string {
case "char[]":
return "[C"
default:
if strings.HasPrefix(p.typeAnno, "java.") {
if strings.HasSuffix(p.typeAnno, "[]") {
return "[L" + p.typeAnno[:len(p.typeAnno)-2] + ";"
}
return p.typeAnno
if strings.HasSuffix(p.typeAnno, "[]") {
return "[L" + p.typeAnno[:len(p.typeAnno)-2] + ";"
}
return p.typeAnno
}
return ""
}

func (p *parameter) getTypeByValue() string {
Expand Down
15 changes: 9 additions & 6 deletions pkg/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,27 +61,30 @@ func WithJavaClassName(name string) Option {
// annotations allows specifying the Java types for DubboCodec encoding.
func WithFileDescriptor(fd *thrift_reflection.FileDescriptor) Option {
return Option{F: func(o *Options) {
o.TypeAnnotations = make(map[string]*hessian2.TypeAnnotation)
extractAnnotations(fd, o)
o.TypeAnnotations = extractAnnotations(fd)
}}
}

// extractAnnotations extracts method annotations from the given FileDescriptor
// and stores them in the Options structure. These annotations allow specifying Java types for DubboCodec encoding.
// and returns them as a map. These annotations allow specifying Java types for DubboCodec encoding.
// The annotation format is (hessian.argsType="arg1_type,arg2_type,arg3_type,..."),
// use an empty string or "-" as arg_type to use the default parsing method.
func extractAnnotations(fd *thrift_reflection.FileDescriptor, o *Options) {
func extractAnnotations(fd *thrift_reflection.FileDescriptor) map[string]*hessian2.TypeAnnotation {
if fd == nil {
return
return nil
}

annotations := make(map[string]*hessian2.TypeAnnotation)

Check failure on line 78 in pkg/options.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofmt`-ed with `-s` (gofmt)
for _, svc := range fd.GetServices() {
prefix := svc.GetName() + "."

for _, m := range svc.GetMethods() {
annos := m.GetAnnotations()
if v, ok := annos[hessian2.HESSIAN_ARGS_TYPE_TAG]; ok && len(v) > 0 {
o.TypeAnnotations[prefix+m.GetName()] = hessian2.NewTypeAnnotation(v[0])
annotations[prefix+m.GetName()] = hessian2.NewTypeAnnotation(v[0])
}
}
}
return annotations
}

0 comments on commit afd2571

Please sign in to comment.