-
Notifications
You must be signed in to change notification settings - Fork 428
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Generate ID and ObjectType Show Object Methods (#3292)
<!-- Feel free to delete comments as you fill this in --> <!-- summary of changes --> ## Changes * added generating ID() and ObjectType() helper methods ## Examples Generated examples for: **SchemaObjectIdentifier** * secrets * streamlits **AccountObjectIdentifier** * api_integrations * notificatoin_integrations
- Loading branch information
1 parent
acb93de
commit 8dbf002
Showing
10 changed files
with
207 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package generator | ||
|
||
import ( | ||
"log" | ||
"slices" | ||
) | ||
|
||
type ShowObjectIdMethod struct { | ||
StructName string | ||
IdentifierKind objectIdentifierKind | ||
Args []string | ||
} | ||
|
||
func newShowObjectIDMethod(structName string, idType objectIdentifierKind) *ShowObjectIdMethod { | ||
return &ShowObjectIdMethod{ | ||
StructName: structName, | ||
IdentifierKind: idType, | ||
Args: idTypeParts[idType], | ||
} | ||
} | ||
|
||
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 { | ||
return containsFieldNames(field.Fields, requiredFields...) | ||
} | ||
} | ||
} | ||
log.Printf("[WARN] no required fields mapping defined for identifier %s", idKind) | ||
return false | ||
} | ||
|
||
func containsFieldNames(fields []*Field, names ...string) bool { | ||
fieldNames := []string{} | ||
for _, field := range fields { | ||
fieldNames = append(fieldNames, field.Name) | ||
} | ||
|
||
for _, name := range names { | ||
if !slices.Contains(fieldNames, name) { | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
|
||
type ShowObjectTypeMethod struct { | ||
StructName string | ||
} | ||
|
||
func newShowObjectTypeMethod(structName string) *ShowObjectTypeMethod { | ||
return &ShowObjectTypeMethod{StructName: structName} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package generator | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestIdentifierStringToObjectIdentifier(t *testing.T) { | ||
tests := []struct { | ||
input string | ||
expected objectIdentifierKind | ||
}{ | ||
{"AccountObjectIdentifier", AccountObjectIdentifier}, | ||
{"DatabaseObjectIdentifier", DatabaseObjectIdentifier}, | ||
{"SchemaObjectIdentifier", SchemaObjectIdentifier}, | ||
{"SchemaObjectIdentifierWithArguments", SchemaObjectIdentifierWithArguments}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.input, func(t *testing.T) { | ||
result, err := toObjectIdentifierKind(test.input) | ||
require.NoError(t, err) | ||
require.Equal(t, test.expected, result) | ||
}) | ||
} | ||
} | ||
|
||
func TestIdentifierStringToObjectIdentifier_Invalid(t *testing.T) { | ||
tests := []struct { | ||
input string | ||
err string | ||
}{ | ||
{"accountobjectidentifier", "invalid string identifier type: accountobjectidentifier"}, | ||
{"Account", "invalid string identifier type: Account"}, | ||
{"databaseobjectidentifier", "invalid string identifier type: databaseobjectidentifier"}, | ||
{"Database", "invalid string identifier type: Database"}, | ||
{"schemaobjectidentifier", "invalid string identifier type: schemaobjectidentifier"}, | ||
{"Schema", "invalid string identifier type: Schema"}, | ||
{"schemaobjectidentifierwitharguments", "invalid string identifier type: schemaobjectidentifierwitharguments"}, | ||
{"schemawitharguemnts", "invalid string identifier type: schemawitharguemnts"}, | ||
} | ||
|
||
for _, tc := range tests { | ||
t.Run(tc.input, func(t *testing.T) { | ||
_, err := toObjectIdentifierKind(tc.input) | ||
require.ErrorContains(t, err, tc.err) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.ShowObjectIdMethod*/ -}} | ||
|
||
func (v *{{ .StructName }}) ID() {{ .IdentifierKind }} { | ||
return New{{ .IdentifierKind }}({{ range .Args }}v.{{ . }}, {{ end }}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters