Skip to content

Commit 328aaab

Browse files
lvan100lianghuan
authored andcommitted
111
1 parent 2b26097 commit 328aaab

File tree

2 files changed

+34
-33
lines changed

2 files changed

+34
-33
lines changed

gen/generator/golang/spec.go

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -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
1531
type 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
5353
type 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
6162
type 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
7577
func (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
8582
func (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

gen/generator/golang/type.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ const {{$c.Name}} {{$c.Type}} = {{$c.Value}}
172172
{{- if $f.Comment}}
173173
{{$f.Comment}}
174174
{{- end}}
175-
{{$f.Name}} {{$f.FieldType}} {{$f.FieldTag}}
175+
{{$f.Name}} {{$f.Type}} {{$f.FieldTag}}
176176
{{- end}}
177177
}
178178
@@ -212,7 +212,7 @@ const {{$c.Name}} {{$c.Type}} = {{$c.Value}}
212212
}
213213
{{end}}
214214
215-
{{if $s.IsRequestBody}}
215+
{{if $s.RequestBody}}
216216
// EncodeToForm encodes the object to form data.
217217
func (x *{{$s.Name}}) EncodeToForm() (string, error) {
218218
m := make(url.Values)

0 commit comments

Comments
 (0)