diff --git a/pkg/sdk/poc/generator/helper_methods.go b/pkg/sdk/poc/generator/helper_methods.go new file mode 100644 index 0000000000..b03ebf6e59 --- /dev/null +++ b/pkg/sdk/poc/generator/helper_methods.go @@ -0,0 +1,52 @@ +package generator + +import "fmt" + +type HelperMethod struct { + Name string + StructName string + ReturnValue string + ReturnType string +} + +func newHelperMethod(name, structName, returnValue, returnType string) *HelperMethod { + return &HelperMethod{ + Name: name, + StructName: structName, + ReturnValue: returnValue, + ReturnType: returnType, + } +} + +func newIDHelperMethod(idKind idPrefix, structName, returnType string) *HelperMethod { + var returnValue string + switch idKind { + case AccountIdentifierPrefix: + returnValue = "NewAccountObjectIdentifier(v.Name)" + case DatabaseIdentifierPrefix: + returnValue = "NewDatabaseObjectIdentifier(v.DatabaseName, v.Name)" + case SchemaIdentifierPrefix: + returnValue = "NewSchemaObjectIdentifier(v.DatabaseName, v.SchemaName, v.Name)" + default: + return nil + } + return newHelperMethod("ID", structName, returnValue, returnType) +} + +func newObjectTypeHelperMethod(structName string) *HelperMethod { + return newHelperMethod("ObjectType", structName, fmt.Sprintf("ObjectType%v", structName), "ObjectType") +} + +func (i *Interface) ID() *Interface { + i.HelperMethods = append(i.HelperMethods, newIDHelperMethod(i.ObjectIdentifierPrefix(), i.NameSingular, i.IdentifierKind)) + return i +} + +func (i *Interface) ObjectType() *Interface { + i.HelperMethods = append(i.HelperMethods, newObjectTypeHelperMethod(i.NameSingular)) + return i +} + +func (i *Interface) ObjectHelperMethods() *Interface { + return i.ID().ObjectType() +} diff --git a/pkg/sdk/poc/generator/interface.go b/pkg/sdk/poc/generator/interface.go index 98c213367e..41a7b57704 100644 --- a/pkg/sdk/poc/generator/interface.go +++ b/pkg/sdk/poc/generator/interface.go @@ -10,6 +10,8 @@ type Interface struct { Operations []*Operation // IdentifierKind keeps identifier of the underlying object (e.g. DatabaseObjectIdentifier) IdentifierKind string + + HelperMethods []*HelperMethod } func NewInterface(name string, nameSingular string, identifierKind string, operations ...*Operation) *Interface { diff --git a/pkg/sdk/poc/generator/template_executors.go b/pkg/sdk/poc/generator/template_executors.go index 00b4678410..90c64f2252 100644 --- a/pkg/sdk/poc/generator/template_executors.go +++ b/pkg/sdk/poc/generator/template_executors.go @@ -22,6 +22,15 @@ func GenerateInterface(writer io.Writer, def *Interface) { generateOptionsStruct(writer, o) } } + for _, m := range def.HelperMethods { + if m != nil { + generateHelperMethods(writer, m) + } + } +} + +func generateHelperMethods(writer io.Writer, hm *HelperMethod) { + printTo(writer, HelperMethodTemplate, hm) } func generateOptionsStruct(writer io.Writer, operation *Operation) { diff --git a/pkg/sdk/poc/generator/templates.go b/pkg/sdk/poc/generator/templates.go index 8d7d6b52b4..1f4ee80128 100644 --- a/pkg/sdk/poc/generator/templates.go +++ b/pkg/sdk/poc/generator/templates.go @@ -24,6 +24,10 @@ var ( structTemplateContent string StructTemplate, _ = template.New("structTemplate").Parse(structTemplateContent) + //go:embed templates/helper_method.tmpl + helperMethodTemplateContent string + HelperMethodTemplate, _ = template.New("helperMethodTemplate").Parse(helperMethodTemplateContent) + //go:embed templates/dto_declarations.tmpl dtoDeclarationsTemplateContent string DtoTemplate, _ = template.New("dtoTemplate").Parse(dtoDeclarationsTemplateContent) diff --git a/pkg/sdk/poc/generator/templates/helper_method.tmpl b/pkg/sdk/poc/generator/templates/helper_method.tmpl new file mode 100644 index 0000000000..dae590da1f --- /dev/null +++ b/pkg/sdk/poc/generator/templates/helper_method.tmpl @@ -0,0 +1,5 @@ +{{- /*gotype: github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/poc/generator.HelperMethod*/ -}} + +func (v *{{ .StructName }}) {{ .Name }}() {{ .ReturnType }} { + return {{ .ReturnValue }} +}