Skip to content

Commit 9cb6b25

Browse files
lvan100lianghuan
authored andcommitted
111
1 parent 00246ca commit 9cb6b25

File tree

2 files changed

+50
-21
lines changed

2 files changed

+50
-21
lines changed

gen/generator/golang/spec.go

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,10 @@ type TypeField struct {
6464
TypeKind []TypeKind
6565
Name string
6666
Tag string
67-
Validate *string
67+
JSONTag JSONTag
6868
Binding *Binding
6969
FormName string
70+
Validate *string
7071
Comment string
7172
}
7273

@@ -465,10 +466,10 @@ func convertType(code GoCode, t httpidl.Type) (Type, error) {
465466
return Type{}, errutil.Explain(nil, "parse binding for field %s in type %s error: %w", f.Name, r.Name, err)
466467
}
467468

468-
// Generate struct tag for JSON, query/path bindings
469-
fieldTag, err := genFieldTag(f.Name, typeName, f.Annotations, binding)
469+
// Generate JSON tag
470+
jsonTag, err := genJSONTag(f.Name, typeName, f.Annotations)
470471
if err != nil {
471-
return Type{}, errutil.Explain(nil, "generate field tag for field %s in type %s error: %w", f.Name, r.Name, err)
472+
return Type{}, err
472473
}
473474

474475
// Generate validation expressions for the field
@@ -478,15 +479,17 @@ func convertType(code GoCode, t httpidl.Type) (Type, error) {
478479
}
479480

480481
// Add the field to the struct
481-
r.Fields = append(r.Fields, TypeField{
482+
field := TypeField{
482483
Type: typeName,
483484
TypeKind: typeKind,
484485
Name: fieldName,
485-
Tag: fieldTag,
486-
Validate: validate,
486+
JSONTag: jsonTag,
487487
Binding: binding,
488+
Validate: validate,
488489
Comment: formatComment(f.Comments),
489-
})
490+
}
491+
field.Tag = genFieldTag(field)
492+
r.Fields = append(r.Fields, field)
490493
}
491494
return r, nil
492495
}
@@ -714,21 +717,13 @@ func genJSONTag(fieldName, typeName string, arr []httpidl.Annotation) (JSONTag,
714717

715718
// genFieldTag generates the struct tag for a Go struct field.
716719
// It includes JSON tags and optional binding tags (path, query).
717-
func genFieldTag(fieldName, typeName string, arr []httpidl.Annotation, binding *Binding) (string, error) {
720+
func genFieldTag(f TypeField) string {
718721
var tags []string
719-
720-
jsonTag, err := genJSONTag(fieldName, typeName, arr)
721-
if err != nil {
722-
return "", err
722+
tags = append(tags, f.JSONTag.String())
723+
if f.Binding != nil {
724+
tags = append(tags, f.Binding.String())
723725
}
724-
tags = append(tags, jsonTag.String())
725-
726-
// Generate binding tag
727-
if binding != nil {
728-
tags = append(tags, binding.String())
729-
}
730-
731-
return "`" + strings.Join(tags, " ") + "`", nil
726+
return "`" + strings.Join(tags, " ") + "`"
732727
}
733728

734729
// ValidateFunc represents a custom validation function

gen/testdata/manager/go/proto/Manager_http.go

Lines changed: 34 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)