Skip to content

Commit 2b26097

Browse files
lvan100lianghuan
authored andcommitted
111
1 parent 6973339 commit 2b26097

File tree

1 file changed

+30
-66
lines changed

1 file changed

+30
-66
lines changed

gen/generator/golang/spec.go

Lines changed: 30 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ type TypeField struct {
6464
TypeKind []TypeKind
6565
Name string
6666
FieldTag string
67-
JSONTag JSONTag
68-
FormTag FormTag
69-
Binding *Binding
67+
JSONTag httpidl.JSONTag
68+
FormTag httpidl.FormTag
69+
Binding *httpidl.Binding
7070
Required bool
7171
Validate *string
7272
Comment string
@@ -76,16 +76,6 @@ func (x TypeField) IsPointer() bool {
7676
return x.TypeKind[0] == TypeKindPointer
7777
}
7878

79-
// Binding represents a field binding from path, or query
80-
type Binding struct {
81-
From string // Source: path/query
82-
Name string // Field name in the source
83-
}
84-
85-
func (x Binding) String() string {
86-
return fmt.Sprintf(`%s:"%s"`, x.From, x.Name)
87-
}
88-
8979
// IsRequestBody returns true if the struct is a request body
9080
func (t *Type) IsRequestBody() bool {
9181
return strings.HasSuffix(t.Name, "Body")
@@ -460,23 +450,12 @@ func convertType(code GoCode, t httpidl.Type) (Type, error) {
460450
ValueType: valueType,
461451
TypeKind: typeKind,
462452
Name: fieldName,
463-
JSONTag: JSONTag{
464-
Name: f.JSONTag.Name,
465-
OmitEmpty: f.JSONTag.OmitEmpty,
466-
OmitZero: f.JSONTag.OmitZero,
467-
},
468-
FormTag: FormTag{
469-
Name: f.FormTag.Name,
470-
},
471-
Required: f.Required,
472-
Validate: validateExpr,
473-
Comment: formatComment(f.Comments),
474-
}
475-
if f.Binding != nil {
476-
field.Binding = &Binding{
477-
From: f.Binding.From,
478-
Name: f.Binding.Name,
479-
}
453+
JSONTag: f.JSONTag,
454+
FormTag: f.FormTag,
455+
Binding: f.Binding,
456+
Required: f.Required,
457+
Validate: validateExpr,
458+
Comment: formatComment(f.Comments),
480459
}
481460
field.FieldTag = genFieldTag(field)
482461
r.Fields = append(r.Fields, field)
@@ -622,47 +601,32 @@ func getTypeKind(code GoCode, typeName string) ([]TypeKind, string, error) {
622601
}
623602
}
624603

625-
// JSONTag represents the JSON tag of a field.
626-
type JSONTag struct {
627-
Name string
628-
OmitEmpty bool
629-
OmitZero bool
630-
}
631-
632-
// JSONTag returns a JSON tag string for a field.
633-
func (tag JSONTag) String() string {
634-
var sb strings.Builder
635-
sb.WriteString(`json:"`)
636-
sb.WriteString(tag.Name)
637-
if tag.OmitEmpty {
638-
sb.WriteString(",omitempty")
639-
}
640-
if tag.OmitZero {
641-
sb.WriteString(",omitzero")
642-
}
643-
sb.WriteString(`"`)
644-
return sb.String()
645-
}
646-
647-
// FormTag represents the form tag of a field.
648-
type FormTag struct {
649-
Name string
650-
}
651-
652-
// FormTag returns a form tag string for a field.
653-
func (tag FormTag) String() string {
654-
return `form:"` + tag.Name + `"`
655-
}
656-
657604
// genFieldTag generates the struct tag for a Go struct field.
658605
// It includes JSON tags and optional binding tags (path, query).
659606
func genFieldTag(f TypeField) string {
660607
var tags []string
661-
tags = append(tags, f.JSONTag.String())
662-
if f.Binding != nil {
663-
tags = append(tags, f.Binding.String())
608+
609+
// JSON tag
610+
{
611+
var sb strings.Builder
612+
sb.WriteString(`json:"`)
613+
sb.WriteString(f.JSONTag.Name)
614+
if f.JSONTag.OmitEmpty {
615+
sb.WriteString(",omitempty")
616+
}
617+
if f.JSONTag.OmitZero {
618+
sb.WriteString(",omitzero")
619+
}
620+
sb.WriteString(`"`)
621+
tags = append(tags, sb.String())
622+
}
623+
624+
if f.Binding == nil {
625+
s := fmt.Sprintf(`form:"%s"`, f.FormTag.Name)
626+
tags = append(tags, s)
664627
} else {
665-
tags = append(tags, f.FormTag.String())
628+
s := fmt.Sprintf(`%s:"%s"`, f.Binding.From, f.Binding.Name)
629+
tags = append(tags, s)
666630
}
667631
return "`" + strings.Join(tags, " ") + "`"
668632
}

0 commit comments

Comments
 (0)