Skip to content

Commit

Permalink
Add nested type support to go code generator (#1254)
Browse files Browse the repository at this point in the history
  • Loading branch information
ebeahan authored Apr 8, 2021
1 parent 3873556 commit 33bce1b
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.next.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Thanks, you're awesome :-) -->

#### Improvements

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

Expand Down
76 changes: 66 additions & 10 deletions scripts/cmd/gocodegen/gocodegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,15 @@ type {{.Name}} struct {
{{$field.Name}} {{$field.Type}} \u0060ecs:"{{$field.JSONKey}}"\u0060
{{ end -}}
}
{{ range $nestedField := .NestedTypes }}
type {{$nestedField.Name}} struct {
{{- range $field := $nestedField.Fields}}
// {{$field.Comment}}
{{$field.Name}} {{$field.Type}} \u0060ecs:"{{$field.JSONKey}}"\u0060
{{ end -}}
}
{{ end -}}
`

const versionTmpl = `
Expand All @@ -101,9 +110,17 @@ type GoType struct {
Description string
Name string
Fields []Field
NestedTypes map[string]*NestedField
ImportTime bool
}

type NestedField struct {
Name string
Type string
Fields []Field
ImportTime bool
}

type Field struct {
Comment string
Name string
Expand Down Expand Up @@ -167,20 +184,51 @@ func main() {
License: license[1:],
Description: descriptionToComment("", group.Description),
Name: goTypeName(group.Name),
NestedTypes: make(map[string]*NestedField),
}

for _, field := range group.Fields {
dataType := goDataType(field.Name, field.Type)
if strings.HasPrefix(dataType, "time.") {
t.ImportTime = true
// handle `nested` fields
if field.Type == "nested" {
n := NestedField{
Name: goTypeName(field.Name),
Type: "nested",
}

t.NestedTypes[field.Name] = &n
fieldName := goTypeName(field.Name)
t.Fields = append(t.Fields, Field{
Comment: descriptionToComment("\t", field.Description),
Name: goTypeName(fieldName),
Type: "[]" + goTypeName(fieldName),
JSONKey: field.Name,
})

} else {
dataType := goDataType(field.Name, field.Type)
if strings.HasPrefix(dataType, "time.") {
t.ImportTime = true
}

// check if field belongs under a nested field
if nestedField, ok := t.NestedTypes[(trimStringFromDot(field.Name))]; ok {
prefix := strings.ToLower(nestedField.Name) + "."
fieldNameWithoutPrefix := strings.ReplaceAll(field.Name, prefix, "")
nestedField.Fields = append(nestedField.Fields, Field{
Comment: descriptionToComment("\t", field.Description),
Name: goTypeName(fieldNameWithoutPrefix),
Type: dataType,
JSONKey: fieldNameWithoutPrefix,
})
} else {
t.Fields = append(t.Fields, Field{
Comment: descriptionToComment("\t", field.Description),
Name: goTypeName(field.Name),
Type: dataType,
JSONKey: field.Name,
})
}
}

t.Fields = append(t.Fields, Field{
Comment: descriptionToComment("\t", field.Description),
Name: goTypeName(field.Name),
Type: dataType,
JSONKey: field.Name,
})
}

b := new(bytes.Buffer)
Expand Down Expand Up @@ -313,3 +361,11 @@ func goTypeName(name string) string {
}
return b.String()
}

// trim strings after "." character
func trimStringFromDot(s string) string {
if idx := strings.Index(s, "."); idx != -1 {
return s[:idx]
}
return s
}

0 comments on commit 33bce1b

Please sign in to comment.