Skip to content

Commit

Permalink
init for interface helper methods
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-fbudzynski committed Dec 9, 2024
1 parent 548ec42 commit cea5dd3
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 0 deletions.
52 changes: 52 additions & 0 deletions pkg/sdk/poc/generator/helper_methods.go
Original file line number Diff line number Diff line change
@@ -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()
}
2 changes: 2 additions & 0 deletions pkg/sdk/poc/generator/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 9 additions & 0 deletions pkg/sdk/poc/generator/template_executors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 4 additions & 0 deletions pkg/sdk/poc/generator/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions pkg/sdk/poc/generator/templates/helper_method.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{{- /*gotype: github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/poc/generator.HelperMethod*/ -}}

func (v *{{ .StructName }}) {{ .Name }}() {{ .ReturnType }} {
return {{ .ReturnValue }}
}

0 comments on commit cea5dd3

Please sign in to comment.