From bd2b9e88cb569e049fc0adf2a62fc60a2b923935 Mon Sep 17 00:00:00 2001 From: Tom Bamford Date: Thu, 5 Sep 2024 00:44:49 +0100 Subject: [PATCH] generator-go-sdk: `Base{Model}Impl` structs should implement their own model interface --- .../internal/generator/templater_models.go | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/tools/generator-go-sdk/internal/generator/templater_models.go b/tools/generator-go-sdk/internal/generator/templater_models.go index 8e978702802..0fcf91b2aab 100644 --- a/tools/generator-go-sdk/internal/generator/templater_models.go +++ b/tools/generator-go-sdk/internal/generator/templater_models.go @@ -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), "//") { @@ -196,6 +196,12 @@ 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 { @@ -203,6 +209,16 @@ type %[1]s struct { } `, 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)