Skip to content

Commit

Permalink
generator-go-sdk: Base{Model}Impl structs should implement their ow…
Browse files Browse the repository at this point in the history
…n model interface
  • Loading branch information
manicminer committed Sep 4, 2024
1 parent 7a2417e commit bd2b9e8
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion tools/generator-go-sdk/internal/generator/templater_models.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ type %[1]s interface {
`, c.name, strings.Join(interfaceLines, "\n"))
}

// Output a model struct
// Format the model struct field lines
formattedStructLines := make([]string, 0)
for i, v := range structLines {
if strings.HasPrefix(strings.TrimSpace(v), "//") {
Expand All @@ -196,13 +196,29 @@ type %[1]s interface {
formattedStructLines = append(formattedStructLines, v)
}

// When the struct name doesn't match the model name, the struct should implement the model interface
if structName != c.name {
parentAssignmentInfo = fmt.Sprintf("var _ %[1]s = %[2]s{}", c.name, structName)
}

// Output the model struct
out += fmt.Sprintf(`
%[3]s
type %[1]s struct {
%[2]s
}
`, structName, strings.Join(formattedStructLines, "\n"), parentAssignmentInfo)

// When the struct name doesn't match the model name, output a method to satisfy the model interface
if structName != c.name {
out += fmt.Sprintf(`
func (s %[1]s) %[2]s() %[1]s {
return s
}
`, structName, c.name)
}

parentModelFunctions, err := c.codeForParentStructFunctions(data)
if err != nil {
return nil, fmt.Errorf("generating parent model functions: %+v", err)
Expand Down

0 comments on commit bd2b9e8

Please sign in to comment.