@@ -11,6 +11,22 @@ import (
1111 "github.com/lvan100/golib/errutil"
1212)
1313
14+ // TypeKind represents kind of a Go field type
15+ type TypeKind int
16+
17+ const (
18+ TypeKindBool TypeKind = iota
19+ TypeKindInt
20+ TypeKindUint
21+ TypeKindFloat
22+ TypeKindString
23+ TypeKindStruct
24+ TypeKindEnum
25+ TypeKindList
26+ TypeKindMap
27+ TypeKindPointer
28+ )
29+
1430// Const represents a Go constant
1531type Const struct {
1632 Type string
@@ -33,54 +49,35 @@ type EnumField struct {
3349 Comment string
3450}
3551
36- // TypeKind represents kind of a Go field type
37- type TypeKind int
38-
39- const (
40- TypeKindBool TypeKind = iota
41- TypeKindInt
42- TypeKindUint
43- TypeKindFloat
44- TypeKindString
45- TypeKindStruct
46- TypeKindEnum
47- TypeKindList
48- TypeKindMap
49- TypeKindPointer
50- )
51-
5252// Type represents a Go struct
5353type Type struct {
54- Name string
55- Fields []TypeField
56- Comment string
57- Request bool
54+ Name string
55+ Fields []TypeField
56+ Comment string
57+ Request bool
58+ RequestBody bool
5859}
5960
6061// TypeField represents a field in a Go struct
6162type TypeField struct {
62- FieldType string // for field
63- ValueType string // for getter/setter
64- TypeKind []TypeKind
6563 Name string
64+ Type string // for field
65+ TypeKind []TypeKind
66+ ValueType string // for getter/setter
6667 FieldTag string
68+ Required bool
6769 JSONTag httpidl.JSONTag
6870 FormTag httpidl.FormTag
6971 Binding * httpidl.Binding
70- Required bool
7172 Validate * string
7273 Comment string
7374}
7475
76+ // IsPointer returns true if the field is a pointer
7577func (x TypeField ) IsPointer () bool {
7678 return x .TypeKind [0 ] == TypeKindPointer
7779}
7880
79- // IsRequestBody returns true if the struct is a request body
80- func (t * Type ) IsRequestBody () bool {
81- return strings .HasSuffix (t .Name , "Body" )
82- }
83-
8481// BindingCount returns the number of fields in the struct that have binding info
8582func (t * Type ) BindingCount () int {
8683 var count int
@@ -244,7 +241,10 @@ func SplitRequestType(t Type) (req Type, body Type) {
244241 req .Request = true
245242 req .Name = t .Name
246243 req .Comment = t .Comment
244+
247245 body .Name = t .Name + "Body"
246+ body .RequestBody = true
247+
248248 for _ , field := range t .Fields {
249249 if field .Binding != nil {
250250 req .Fields = append (req .Fields , field )
@@ -392,7 +392,7 @@ func convertType(code GoCode, t httpidl.Type) (Type, error) {
392392 // Handle oneof
393393 if t .OneOf {
394394 r .Fields = append (r .Fields , TypeField {
395- FieldType : "*" + r .Name + "TypeAsString" ,
395+ Type : "*" + r .Name + "TypeAsString" ,
396396 ValueType : r .Name + "TypeAsString" ,
397397 TypeKind : []TypeKind {TypeKindPointer , TypeKindEnum },
398398 Name : "FieldType" ,
@@ -446,7 +446,7 @@ func convertType(code GoCode, t httpidl.Type) (Type, error) {
446446
447447 // Add the field to the struct
448448 field := TypeField {
449- FieldType : typeName ,
449+ Type : typeName ,
450450 ValueType : valueType ,
451451 TypeKind : typeKind ,
452452 Name : fieldName ,
@@ -628,6 +628,7 @@ func genFieldTag(f TypeField) string {
628628 s := fmt .Sprintf (`%s:"%s"` , f .Binding .From , f .Binding .Name )
629629 tags = append (tags , s )
630630 }
631+
631632 return "`" + strings .Join (tags , " " ) + "`"
632633}
633634
0 commit comments