@@ -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
8089const 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+
107124type 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