Skip to content

Commit

Permalink
merge
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-fbudzynski authored and sfc-gh-asawicki committed Jan 9, 2025
1 parent e1cffd3 commit e088e47
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 44 deletions.
53 changes: 19 additions & 34 deletions pkg/sdk/poc/generator/show_object_methods.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,31 @@
package generator

import (
"fmt"
"log"
"slices"
)


type ShowObjectMethod struct {
Name string
StructName string
ReturnValue string
ReturnType string
}

type ShowObjectIdMethod struct {

StructName string
IdentifierKind objectIdentifierKind
Args []string
}

func newShowObjectMethod(name, structName, returnValue string, returnType string) *ShowObjectMethod {
return &ShowObjectMethod{
Name: name,
StructName: structName,
ReturnValue: returnValue,
ReturnType: returnType,
func newShowObjectIDMethod(structName string, idType objectIdentifierKind) *ShowObjectIdMethod {
return &ShowObjectIdMethod{
StructName: structName,
IdentifierKind: idType,
Args: idTypeParts[idType],
}
}

func checkRequiredFieldsForIDMethod(structName string, helperStructs []*Field, idKind objectIdentifierKind) bool {
var idTypeParts map[objectIdentifierKind][]string = map[objectIdentifierKind][]string{
AccountObjectIdentifier: {"Name"},
DatabaseObjectIdentifier: {"DatabaseName", "Name"},
SchemaObjectIdentifier: {"DatabaseName", "SchemaName", "Name"},
}

func checkRequiredFieldsForIdMethod(structName string, helperStructs []*Field, idKind objectIdentifierKind) bool {
if requiredFields, ok := idTypeParts[idKind]; ok {
for _, field := range helperStructs {
if field.Name == structName {
Expand All @@ -39,12 +37,6 @@ func checkRequiredFieldsForIDMethod(structName string, helperStructs []*Field, i
return false
}

var idTypeParts map[objectIdentifierKind][]string = map[objectIdentifierKind][]string{
AccountObjectIdentifier: {"Name"},
DatabaseObjectIdentifier: {"DatabaseName", "Name"},
SchemaObjectIdentifier: {"DatabaseName", "SchemaName", "Name"},
}

func containsFieldNames(fields []*Field, names ...string) bool {
fieldNames := []string{}
for _, field := range fields {
Expand All @@ -59,17 +51,10 @@ func containsFieldNames(fields []*Field, names ...string) bool {
return true
}

func newShowObjectIDMethod(structName string, idType objectIdentifierKind) *ShowObjectMethod {
requiredFields := idTypeParts[idType]
var args string
for _, field := range requiredFields {
args += fmt.Sprintf("v.%v, ", field)
}

returnValue := fmt.Sprintf("New%v(%v)", idType, args)
return newShowObjectMethod("ID", structName, returnValue, string(idType))
type ShowObjectTypeMethod struct {
StructName string
}

func newShowObjectTypeMethod(structName string) *ShowObjectMethod {
return newShowObjectMethod("ObjectType", structName, "ObjectType"+structName, "ObjectType")
func newShowObjectTypeMethod(structName string) *ShowObjectTypeMethod {
return &ShowObjectTypeMethod{StructName: structName}
}
15 changes: 9 additions & 6 deletions pkg/sdk/poc/generator/template_executors.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,21 @@ func GenerateInterface(writer io.Writer, def *Interface) {
if err != nil {
log.Printf("[WARN] for showObjectIdMethod: %v", err)
}
if checkRequiredFieldsForIDMethod(def.NameSingular, o.HelperStructs, idKind) {
generateShowObjectMethods(writer, newShowObjectIDMethod(def.NameSingular, idKind))
if checkRequiredFieldsForIdMethod(def.NameSingular, o.HelperStructs, idKind) {
generateShowObjectIdMethod(writer, newShowObjectIDMethod(def.NameSingular, idKind))
}

generateShowObjectMethods(writer, newShowObjectTypeMethod(def.NameSingular))
generateShowObjectTypeMethod(writer, newShowObjectTypeMethod(def.NameSingular))
}

}
}

func generateShowObjectMethods(writer io.Writer, hm *ShowObjectMethod) {
printTo(writer, ShowObjectMethodTemplate, hm)
func generateShowObjectIdMethod(writer io.Writer, m *ShowObjectIdMethod) {
printTo(writer, ShowObjectIdMethodTemplate, m)
}

func generateShowObjectTypeMethod(writer io.Writer, m *ShowObjectTypeMethod) {
printTo(writer, ShowObjectTypeMethodTemplate, m)
}

func generateOptionsStruct(writer io.Writer, operation *Operation) {
Expand Down
8 changes: 8 additions & 0 deletions pkg/sdk/poc/generator/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ var (
showObjectMethodTemplateContent string
ShowObjectMethodTemplate, _ = template.New("helperMethodTemplate").Parse(showObjectMethodTemplateContent)

//go:embed templates/show_object_id_method.tmpl
showObjectIdMethodTemplateContent string
ShowObjectIdMethodTemplate, _ = template.New("showObjectIdMethodTemplate").Parse(showObjectIdMethodTemplateContent)

//go:embed templates/show_object_type_method.tmpl
showObjectTypeMethodTemplateContent string
ShowObjectTypeMethodTemplate, _ = template.New("showObjectTypeMethodTemplate").Parse(showObjectTypeMethodTemplateContent)

//go:embed templates/dto_declarations.tmpl
dtoDeclarationsTemplateContent string
DtoTemplate, _ = template.New("dtoTemplate").Parse(dtoDeclarationsTemplateContent)
Expand Down
4 changes: 2 additions & 2 deletions pkg/sdk/poc/generator/templates/show_object_id_method.tmpl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{{- /*gotype: github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/poc/generator.ShowObjectMethod*/ -}}
{{- /*gotype: github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/poc/generator.ShowObjectIdMethod*/ -}}

func (v *{{ .StructName }}) ID() {{ .IdentifierKind }} {
return New{{ .IdentifierKind }}({{ range .Args }}v.{{ .Name }}, {{ end }})
return New{{ .IdentifierKind }}({{ range .Args }}v.{{ . }}, {{ end }})
}
5 changes: 5 additions & 0 deletions pkg/sdk/poc/generator/templates/show_object_type_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.ShowObjectTypeMethod*/ -}}

func (v *{{ .StructName }}) ObjectType() ObjectType {
return ObjectType{{ .StructName }}
}
2 changes: 0 additions & 2 deletions pkg/sdk/secrets_def.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,6 @@ var SecretsDef = g.NewInterface(
SQL("SECRETS").
OptionalLike().
OptionalExtendedIn(),
g.ShowObjectIdMethod,
g.ShowObjectTypeMethod,
).ShowByIdOperationWithFiltering(
g.ShowByIDLikeFiltering,
g.ShowByIDExtendedInFiltering,
Expand Down

0 comments on commit e088e47

Please sign in to comment.