Skip to content

Commit e088e47

Browse files
sfc-gh-fbudzynskisfc-gh-asawicki
authored andcommitted
merge
1 parent e1cffd3 commit e088e47

File tree

6 files changed

+43
-44
lines changed

6 files changed

+43
-44
lines changed
Lines changed: 19 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,31 @@
11
package generator
22

33
import (
4-
"fmt"
54
"log"
65
"slices"
76
)
87

9-
10-
type ShowObjectMethod struct {
11-
Name string
12-
StructName string
13-
ReturnValue string
14-
ReturnType string
15-
}
16-
178
type ShowObjectIdMethod struct {
18-
9+
StructName string
10+
IdentifierKind objectIdentifierKind
11+
Args []string
1912
}
2013

21-
func newShowObjectMethod(name, structName, returnValue string, returnType string) *ShowObjectMethod {
22-
return &ShowObjectMethod{
23-
Name: name,
24-
StructName: structName,
25-
ReturnValue: returnValue,
26-
ReturnType: returnType,
14+
func newShowObjectIDMethod(structName string, idType objectIdentifierKind) *ShowObjectIdMethod {
15+
return &ShowObjectIdMethod{
16+
StructName: structName,
17+
IdentifierKind: idType,
18+
Args: idTypeParts[idType],
2719
}
2820
}
2921

30-
func checkRequiredFieldsForIDMethod(structName string, helperStructs []*Field, idKind objectIdentifierKind) bool {
22+
var idTypeParts map[objectIdentifierKind][]string = map[objectIdentifierKind][]string{
23+
AccountObjectIdentifier: {"Name"},
24+
DatabaseObjectIdentifier: {"DatabaseName", "Name"},
25+
SchemaObjectIdentifier: {"DatabaseName", "SchemaName", "Name"},
26+
}
27+
28+
func checkRequiredFieldsForIdMethod(structName string, helperStructs []*Field, idKind objectIdentifierKind) bool {
3129
if requiredFields, ok := idTypeParts[idKind]; ok {
3230
for _, field := range helperStructs {
3331
if field.Name == structName {
@@ -39,12 +37,6 @@ func checkRequiredFieldsForIDMethod(structName string, helperStructs []*Field, i
3937
return false
4038
}
4139

42-
var idTypeParts map[objectIdentifierKind][]string = map[objectIdentifierKind][]string{
43-
AccountObjectIdentifier: {"Name"},
44-
DatabaseObjectIdentifier: {"DatabaseName", "Name"},
45-
SchemaObjectIdentifier: {"DatabaseName", "SchemaName", "Name"},
46-
}
47-
4840
func containsFieldNames(fields []*Field, names ...string) bool {
4941
fieldNames := []string{}
5042
for _, field := range fields {
@@ -59,17 +51,10 @@ func containsFieldNames(fields []*Field, names ...string) bool {
5951
return true
6052
}
6153

62-
func newShowObjectIDMethod(structName string, idType objectIdentifierKind) *ShowObjectMethod {
63-
requiredFields := idTypeParts[idType]
64-
var args string
65-
for _, field := range requiredFields {
66-
args += fmt.Sprintf("v.%v, ", field)
67-
}
68-
69-
returnValue := fmt.Sprintf("New%v(%v)", idType, args)
70-
return newShowObjectMethod("ID", structName, returnValue, string(idType))
54+
type ShowObjectTypeMethod struct {
55+
StructName string
7156
}
7257

73-
func newShowObjectTypeMethod(structName string) *ShowObjectMethod {
74-
return newShowObjectMethod("ObjectType", structName, "ObjectType"+structName, "ObjectType")
58+
func newShowObjectTypeMethod(structName string) *ShowObjectTypeMethod {
59+
return &ShowObjectTypeMethod{StructName: structName}
7560
}

pkg/sdk/poc/generator/template_executors.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,21 @@ func GenerateInterface(writer io.Writer, def *Interface) {
2727
if err != nil {
2828
log.Printf("[WARN] for showObjectIdMethod: %v", err)
2929
}
30-
if checkRequiredFieldsForIDMethod(def.NameSingular, o.HelperStructs, idKind) {
31-
generateShowObjectMethods(writer, newShowObjectIDMethod(def.NameSingular, idKind))
30+
if checkRequiredFieldsForIdMethod(def.NameSingular, o.HelperStructs, idKind) {
31+
generateShowObjectIdMethod(writer, newShowObjectIDMethod(def.NameSingular, idKind))
3232
}
3333

34-
generateShowObjectMethods(writer, newShowObjectTypeMethod(def.NameSingular))
34+
generateShowObjectTypeMethod(writer, newShowObjectTypeMethod(def.NameSingular))
3535
}
36-
3736
}
3837
}
3938

40-
func generateShowObjectMethods(writer io.Writer, hm *ShowObjectMethod) {
41-
printTo(writer, ShowObjectMethodTemplate, hm)
39+
func generateShowObjectIdMethod(writer io.Writer, m *ShowObjectIdMethod) {
40+
printTo(writer, ShowObjectIdMethodTemplate, m)
41+
}
42+
43+
func generateShowObjectTypeMethod(writer io.Writer, m *ShowObjectTypeMethod) {
44+
printTo(writer, ShowObjectTypeMethodTemplate, m)
4245
}
4346

4447
func generateOptionsStruct(writer io.Writer, operation *Operation) {

pkg/sdk/poc/generator/templates.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ var (
2828
showObjectMethodTemplateContent string
2929
ShowObjectMethodTemplate, _ = template.New("helperMethodTemplate").Parse(showObjectMethodTemplateContent)
3030

31+
//go:embed templates/show_object_id_method.tmpl
32+
showObjectIdMethodTemplateContent string
33+
ShowObjectIdMethodTemplate, _ = template.New("showObjectIdMethodTemplate").Parse(showObjectIdMethodTemplateContent)
34+
35+
//go:embed templates/show_object_type_method.tmpl
36+
showObjectTypeMethodTemplateContent string
37+
ShowObjectTypeMethodTemplate, _ = template.New("showObjectTypeMethodTemplate").Parse(showObjectTypeMethodTemplateContent)
38+
3139
//go:embed templates/dto_declarations.tmpl
3240
dtoDeclarationsTemplateContent string
3341
DtoTemplate, _ = template.New("dtoTemplate").Parse(dtoDeclarationsTemplateContent)
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
{{- /*gotype: github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/poc/generator.ShowObjectMethod*/ -}}
1+
{{- /*gotype: github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/poc/generator.ShowObjectIdMethod*/ -}}
22

33
func (v *{{ .StructName }}) ID() {{ .IdentifierKind }} {
4-
return New{{ .IdentifierKind }}({{ range .Args }}v.{{ .Name }}, {{ end }})
4+
return New{{ .IdentifierKind }}({{ range .Args }}v.{{ . }}, {{ end }})
55
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{{- /*gotype: github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/poc/generator.ShowObjectTypeMethod*/ -}}
2+
3+
func (v *{{ .StructName }}) ObjectType() ObjectType {
4+
return ObjectType{{ .StructName }}
5+
}

pkg/sdk/secrets_def.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,6 @@ var SecretsDef = g.NewInterface(
241241
SQL("SECRETS").
242242
OptionalLike().
243243
OptionalExtendedIn(),
244-
g.ShowObjectIdMethod,
245-
g.ShowObjectTypeMethod,
246244
).ShowByIdOperationWithFiltering(
247245
g.ShowByIDLikeFiltering,
248246
g.ShowByIDExtendedInFiltering,

0 commit comments

Comments
 (0)