Skip to content

Commit 0a1688a

Browse files
lvan100lianghuan
authored andcommitted
111
1 parent 1b3a5f7 commit 0a1688a

File tree

5 files changed

+87
-65
lines changed

5 files changed

+87
-65
lines changed

gen/generator/golang/main.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"go/format"
2121
"os"
2222
"sort"
23+
"strings"
2324

2425
"github.com/go-spring/gs-http-gen/gen/generator"
2526
"github.com/go-spring/gs-http-gen/lib/tidl"
@@ -86,3 +87,18 @@ func formatFile(fileName string, b []byte) error {
8687
}
8788
return os.WriteFile(fileName, b, os.ModePerm)
8889
}
90+
91+
// formatComment generates the comment string
92+
func formatComment(c tidl.Comments) string {
93+
var comment string
94+
for _, s := range c.Above {
95+
comment += s.Text[0]
96+
}
97+
if c.Right != nil {
98+
if c.Above != nil {
99+
comment += "\n"
100+
}
101+
comment += strings.Join(c.Right.Text, "\n")
102+
}
103+
return comment
104+
}

gen/generator/golang/server.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gen/generator/golang/type.go

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@ const {{$c.Name}} {{$c.Type}} = {{$c.Value}}
8888
}
8989
)
9090
91+
// OneOf{{$e.Name}} is usually used for validation.
92+
func OneOf{{$e.Name}}(i {{$e.Name}}) bool {
93+
_, ok := {{$e.Name}}_name[i]
94+
return ok
95+
}
96+
9197
// {{$e.Name}}AsString wraps {{$e.Name}} to encode/decode as a JSON string
9298
type {{$e.Name}}AsString {{$e.Name}}
9399
@@ -108,13 +114,6 @@ const {{$c.Name}} {{$c.Type}} = {{$c.Value}}
108114
}
109115
return fmt.Errorf("invalid {{$e.Name}} value: %q", str)
110116
}
111-
112-
// OneOf{{$e.Name}} is usually used for validation.
113-
func OneOf{{$e.Name}}(i {{$e.Name}}) bool {
114-
_, ok := {{$e.Name}}_name[i]
115-
return ok
116-
}
117-
118117
{{end}}
119118
120119
{{range $s := .Structs}}
@@ -201,21 +200,6 @@ func (g *Generator) genType(ctx Context, fileName string, doc tidl.Document) err
201200
return formatFile(fileName, buf.Bytes())
202201
}
203202

204-
// genComment generates the comment string
205-
func genComment(c tidl.Comments) string {
206-
var comment string
207-
for _, s := range c.Above {
208-
comment += s.Text[0]
209-
}
210-
if c.Right != nil {
211-
if c.Above != nil {
212-
comment += "\n"
213-
}
214-
comment += strings.Join(c.Right.Text, "\n")
215-
}
216-
return comment
217-
}
218-
219203
// Const represents a Go constant
220204
type Const struct {
221205
Type string
@@ -228,17 +212,16 @@ type Const struct {
228212
func convertConsts(ctx Context, doc tidl.Document) ([]Const, error) {
229213
var ret []Const
230214
for _, c := range doc.Consts {
231-
typeName, err := getTypeName(ctx, tidl.BaseType{
232-
Name: c.Type,
233-
}, nil)
215+
t := tidl.BaseType{Name: c.Type}
216+
typeName, err := getTypeName(ctx, t, nil)
234217
if err != nil {
235218
return nil, err
236219
}
237220
ret = append(ret, Const{
238221
Name: c.Name,
239222
Type: typeName,
240223
Value: c.Value,
241-
Comment: genComment(c.Comments),
224+
Comment: formatComment(c.Comments),
242225
})
243226
}
244227
return ret, nil
@@ -267,24 +250,28 @@ func convertEnums(ctx Context, doc tidl.Document) ([]Enum, error) {
267250
fields = append(fields, EnumField{
268251
Name: f.Name,
269252
Value: f.Value,
270-
Comment: genComment(f.Comments),
253+
Comment: formatComment(f.Comments),
271254
})
272255
}
273256
ret = append(ret, Enum{
274257
Name: e.Name,
275258
Fields: fields,
276-
Comment: genComment(e.Comments),
259+
Comment: formatComment(e.Comments),
277260
})
278261
}
279262
for _, t := range doc.Types {
280-
if !t.OneOf {
263+
if !t.OneOf { // skip oneof
281264
continue
282265
}
283266
name := t.Name + "Type"
284267
var fields []EnumField
285268
for i, f := range t.Fields {
269+
jsonName, err := getJSONName(f.Name, f.Annotations)
270+
if err != nil {
271+
return nil, err
272+
}
286273
fields = append(fields, EnumField{
287-
Name: tidl.ToPascal(f.Name),
274+
Name: jsonName,
288275
Value: int64(i),
289276
})
290277
}
@@ -296,6 +283,25 @@ func convertEnums(ctx Context, doc tidl.Document) ([]Enum, error) {
296283
return ret, nil
297284
}
298285

286+
// getJSONName returns the JSON name for a field.
287+
func getJSONName(fieldName string, arr []tidl.Annotation) (string, error) {
288+
if a, ok := tidl.GetAnnotation(arr, "json"); ok {
289+
if a.Value == nil {
290+
return "", fmt.Errorf("annotation json value is nil")
291+
}
292+
s := strings.TrimSpace(*a.Value)
293+
if s == "" {
294+
return "", fmt.Errorf("annotation json value is empty")
295+
}
296+
s = strings.Trim(s, "\"") // Remove quotes
297+
s = strings.TrimSpace(strings.SplitN(s, ",", 2)[0])
298+
if s != "" {
299+
return s, nil
300+
}
301+
}
302+
return fieldName, nil
303+
}
304+
299305
// TypeKind represents kind of a Go field type
300306
type TypeKind int
301307

@@ -493,7 +499,7 @@ func convertType(ctx Context, t tidl.Type) (Type, error) {
493499
Tag: fieldTag,
494500
Validate: validate,
495501
Binding: binding,
496-
Comment: genComment(f.Comments),
502+
Comment: formatComment(f.Comments),
497503
})
498504
}
499505
return r, nil

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

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

gen/testdata/manager/go/proto/stream.go

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

0 commit comments

Comments
 (0)