Skip to content

Commit 33bce1b

Browse files
authored
Add nested type support to go code generator (#1254)
1 parent 3873556 commit 33bce1b

File tree

2 files changed

+67
-10
lines changed

2 files changed

+67
-10
lines changed

CHANGELOG.next.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ Thanks, you're awesome :-) -->
3636

3737
#### Improvements
3838

39+
* Support `nested` types in go code generator. #1254
3940
* Go code generator now supports the `flattened` data type. #1302
4041
* Adjustments to use terminology that doesn't have negative connotation. #1315
4142

scripts/cmd/gocodegen/gocodegen.go

Lines changed: 66 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,15 @@ type {{.Name}} struct {
7575
{{$field.Name}} {{$field.Type}} \u0060ecs:"{{$field.JSONKey}}"\u0060
7676
{{ end -}}
7777
}
78+
79+
{{ range $nestedField := .NestedTypes }}
80+
type {{$nestedField.Name}} struct {
81+
{{- range $field := $nestedField.Fields}}
82+
// {{$field.Comment}}
83+
{{$field.Name}} {{$field.Type}} \u0060ecs:"{{$field.JSONKey}}"\u0060
84+
{{ end -}}
85+
}
86+
{{ end -}}
7887
`
7988

8089
const versionTmpl = `
@@ -101,9 +110,17 @@ type GoType struct {
101110
Description string
102111
Name string
103112
Fields []Field
113+
NestedTypes map[string]*NestedField
104114
ImportTime bool
105115
}
106116

117+
type NestedField struct {
118+
Name string
119+
Type string
120+
Fields []Field
121+
ImportTime bool
122+
}
123+
107124
type Field struct {
108125
Comment string
109126
Name string
@@ -167,20 +184,51 @@ func main() {
167184
License: license[1:],
168185
Description: descriptionToComment("", group.Description),
169186
Name: goTypeName(group.Name),
187+
NestedTypes: make(map[string]*NestedField),
170188
}
171189

172190
for _, field := range group.Fields {
173-
dataType := goDataType(field.Name, field.Type)
174-
if strings.HasPrefix(dataType, "time.") {
175-
t.ImportTime = true
191+
// handle `nested` fields
192+
if field.Type == "nested" {
193+
n := NestedField{
194+
Name: goTypeName(field.Name),
195+
Type: "nested",
196+
}
197+
198+
t.NestedTypes[field.Name] = &n
199+
fieldName := goTypeName(field.Name)
200+
t.Fields = append(t.Fields, Field{
201+
Comment: descriptionToComment("\t", field.Description),
202+
Name: goTypeName(fieldName),
203+
Type: "[]" + goTypeName(fieldName),
204+
JSONKey: field.Name,
205+
})
206+
207+
} else {
208+
dataType := goDataType(field.Name, field.Type)
209+
if strings.HasPrefix(dataType, "time.") {
210+
t.ImportTime = true
211+
}
212+
213+
// check if field belongs under a nested field
214+
if nestedField, ok := t.NestedTypes[(trimStringFromDot(field.Name))]; ok {
215+
prefix := strings.ToLower(nestedField.Name) + "."
216+
fieldNameWithoutPrefix := strings.ReplaceAll(field.Name, prefix, "")
217+
nestedField.Fields = append(nestedField.Fields, Field{
218+
Comment: descriptionToComment("\t", field.Description),
219+
Name: goTypeName(fieldNameWithoutPrefix),
220+
Type: dataType,
221+
JSONKey: fieldNameWithoutPrefix,
222+
})
223+
} else {
224+
t.Fields = append(t.Fields, Field{
225+
Comment: descriptionToComment("\t", field.Description),
226+
Name: goTypeName(field.Name),
227+
Type: dataType,
228+
JSONKey: field.Name,
229+
})
230+
}
176231
}
177-
178-
t.Fields = append(t.Fields, Field{
179-
Comment: descriptionToComment("\t", field.Description),
180-
Name: goTypeName(field.Name),
181-
Type: dataType,
182-
JSONKey: field.Name,
183-
})
184232
}
185233

186234
b := new(bytes.Buffer)
@@ -313,3 +361,11 @@ func goTypeName(name string) string {
313361
}
314362
return b.String()
315363
}
364+
365+
// trim strings after "." character
366+
func trimStringFromDot(s string) string {
367+
if idx := strings.Index(s, "."); idx != -1 {
368+
return s[:idx]
369+
}
370+
return s
371+
}

0 commit comments

Comments
 (0)