Skip to content

Commit

Permalink
generator-go-sdk: populate inherited model struct fields from all par…
Browse files Browse the repository at this point in the history
…ents, only set ancestor-specific fields when implementing its interface
  • Loading branch information
manicminer committed Aug 19, 2024
1 parent 906cf84 commit 6f3f1a5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
2 changes: 1 addition & 1 deletion tools/generator-go-sdk/internal/cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (g GenerateCommand) Run(args []string) int {
f := flag.NewFlagSet("generator-go-sdk", flag.ExitOnError)
f.StringVar(&input.apiServerEndpoint, "data-api", "http://localhost:8080", "-data-api=http://localhost:8080")
f.StringVar(&input.outputDirectory, "output-dir", "", "-output-dir=../generated-sdk-dev")
f.StringVar(&serviceNames, "services", "", "A list of comma separated Service named from the Data API to import")
f.StringVar(&serviceNames, "services", "", "A list of comma separated Service named from the Data API to generate")
if err := f.Parse(args); err != nil {
log.Fatalf("parsing arguments: %+v", err)
}
Expand Down
38 changes: 28 additions & 10 deletions tools/generator-go-sdk/internal/generator/templater_models.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,21 +112,38 @@ func (c modelsTemplater) structCode(data GeneratorData) (*string, error) {
}
parentAssignmentInfo = fmt.Sprintf("var _ %[1]s = %[2]s{}", parentTypeName, structName)

parentFields := make(map[string]models.SDKField)

parent, ok := data.models[*c.model.ParentTypeName]
if !ok {
return nil, fmt.Errorf("couldn't find Parent Model %q for Model %q", *c.model.ParentTypeName, c.name)
}
for fieldName, fieldDetails := range parent.Fields {
parentFields[fieldName] = fieldDetails
}

// Also include fields from the grandparent model
// Related to: https://github.com/hashicorp/pandora/issues/1235
if parentTypeName != *c.model.ParentTypeName {
grandParent, ok := data.models[parentTypeName]
if !ok {
return nil, fmt.Errorf("couldn't find [Grand]Parent Model %q for Model %q", parentTypeName, c.name)
}
for fieldName, fieldDetails := range grandParent.Fields {
parentFields[fieldName] = fieldDetails
}
}

parentFields := make([]string, 0)
for fieldName := range parent.Fields {
parentFields = append(parentFields, fieldName)
parentFieldNames := make([]string, 0, len(parentFields))
for fieldName := range parentFields {
parentFieldNames = append(parentFieldNames, fieldName)
}
sort.Strings(parentFields)
sort.Strings(parentFieldNames)

if len(parentFields) > 0 {
if len(parentFieldNames) > 0 {
structLines = append(structLines, fmt.Sprintf("\n// Fields inherited from %s", *c.model.ParentTypeName))
for _, fieldName := range parentFields {
fieldDetails := parent.Fields[fieldName]
for _, fieldName := range parentFieldNames {
fieldDetails := parentFields[fieldName]
fieldTypeName := "FIXME"
fieldTypeVal, err := helpers.GolangTypeForSDKObjectDefinition(fieldDetails.ObjectDefinition, nil, data.commonTypesPackageName)
if err != nil {
Expand Down Expand Up @@ -393,7 +410,8 @@ func (c modelsTemplater) codeForParentStructFunctions(data GeneratorData) (*stri
parentTypeName = *c.model.ParentTypeName
}

parent, ok := data.models[*c.model.ParentTypeName]
// Intentionally only setting fields from the outermost parent model
parent, ok := data.models[parentTypeName]
if !ok {
return nil, fmt.Errorf("couldn't find Parent Model %q for Model %q", *c.model.ParentTypeName, c.name)
}
Expand Down Expand Up @@ -449,7 +467,7 @@ func (c modelsTemplater) codeForMarshalFunctions(data GeneratorData) (*string, e

structName := c.name

// parent models get a {model}Parent struct
// parent models get a {model}Base struct
if c.model.IsDiscriminatedParentType() {
structName = fmt.Sprintf("%sBase", c.name)
}
Expand Down Expand Up @@ -623,7 +641,7 @@ func Unmarshal%[1]sImplementation(input []byte) (%[1]s, error) {
func (c modelsTemplater) codeForUnmarshalStructFunction(data GeneratorData) (*string, error) {
structName := c.name

// parent models get a {model}Parent struct
// parent models get a {model}Base struct
if c.model.IsDiscriminatedParentType() {
structName = fmt.Sprintf("%sBase", c.name)
}
Expand Down

0 comments on commit 6f3f1a5

Please sign in to comment.