diff --git a/pkg/sdk/poc/generator/interface.go b/pkg/sdk/poc/generator/interface.go index 36bdbd9ba9..22634d99af 100644 --- a/pkg/sdk/poc/generator/interface.go +++ b/pkg/sdk/poc/generator/interface.go @@ -2,16 +2,16 @@ package generator import "fmt" -type objectIdentifier string +type objectIdentifierKind string const ( - AccountObjectIdentifier objectIdentifier = "AccountObjectIdentifier" - DatabaseObjectIdentifier objectIdentifier = "DatabaseObjectIdentifier" - SchemaObjectIdentifier objectIdentifier = "SchemaObjectIdentifier" - SchemaObjectIdentifierWithArguments objectIdentifier = "SchemaObjectIdentifierWithArguments" + AccountObjectIdentifier objectIdentifierKind = "AccountObjectIdentifier" + DatabaseObjectIdentifier objectIdentifierKind = "DatabaseObjectIdentifier" + SchemaObjectIdentifier objectIdentifierKind = "SchemaObjectIdentifier" + SchemaObjectIdentifierWithArguments objectIdentifierKind = "SchemaObjectIdentifierWithArguments" ) -func identifierStringToObjectIdentifier(s string) (objectIdentifier, error) { +func identifierStringToObjectIdentifier(s string) (objectIdentifierKind, error) { switch s { case "AccountObjectIdentifier": return AccountObjectIdentifier, nil diff --git a/pkg/sdk/poc/generator/show_object_methods.go b/pkg/sdk/poc/generator/show_object_methods.go index 3bd857142b..b34d6eabc9 100644 --- a/pkg/sdk/poc/generator/show_object_methods.go +++ b/pkg/sdk/poc/generator/show_object_methods.go @@ -29,13 +29,30 @@ func newShowObjectMethod(name, structName, returnValue string, returnType string } } -var idTypeParts map[objectIdentifier][]string = map[objectIdentifier][]string{ - AccountObjectIdentifier: {"Name"}, - DatabaseObjectIdentifier: {"DatabaseName", "Name"}, - SchemaObjectIdentifier: {"DatabaseName", "SchemaName", "Name"}, +func (s *Operation) withShowObjectMethods(structName string, showObjectMethodsKind ...ShowObjectMethodType) *Operation { + for _, methodKind := range showObjectMethodsKind { + switch methodKind { + case ShowObjectIdMethod: + id, err := identifierStringToObjectIdentifier(s.ObjectInterface.IdentifierKind) + if err != nil { + log.Printf("[WARN]: %v, for showObjectIdMethod", err) + continue + } + if !hasRequiredFieldsForIDMethod(structName, s.HelperStructs, id) { + log.Printf("[WARN]: Struct '%s' does not contain needed fields to build ID() helper method. Create the method manually in _ext file or add missing fields: %v.\n", structName, idTypeParts[id]) + continue + } + s.ShowObjectMethods = append(s.ShowObjectMethods, newShowObjectIDMethod(structName, s.HelperStructs, id)) + case ShowObjectTypeMethod: + s.ShowObjectMethods = append(s.ShowObjectMethods, newShowObjectTypeMethod(structName)) + default: + log.Println("No showObjectMethod found for kind:", methodKind) + } + } + return s } -func hasRequiredFieldsForIDMethod(structName string, helperStructs []*Field, idType objectIdentifier) bool { +func hasRequiredFieldsForIDMethod(structName string, helperStructs []*Field, idType objectIdentifierKind) bool { if requiredFields, ok := idTypeParts[idType]; ok { for _, field := range helperStructs { if field.Name == structName { @@ -47,6 +64,12 @@ func hasRequiredFieldsForIDMethod(structName string, helperStructs []*Field, idT 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 { @@ -61,30 +84,7 @@ func containsFieldNames(fields []*Field, names ...string) bool { return true } -func (s *Operation) withShowObjectMethods(structName string, showObjectMethodsKind ...ShowObjectMethodType) *Operation { - for _, methodKind := range showObjectMethodsKind { - switch methodKind { - case ShowObjectIdMethod: - id, err := identifierStringToObjectIdentifier(s.ObjectInterface.IdentifierKind) - if err != nil { - log.Printf("[WARN]: %v, for showObjectIdMethod", err) - continue - } - if !hasRequiredFieldsForIDMethod(structName, s.HelperStructs, id) { - log.Printf("[WARN]: Struct '%s' does not contain needed fields to build ID() helper method. Create the method manually in _ext file or add missing fields: %v.\n", structName, idTypeParts[id]) - continue - } - s.ShowObjectMethods = append(s.ShowObjectMethods, newShowObjectIDMethod(structName, s.HelperStructs, id)) - case ShowObjectTypeMethod: - s.ShowObjectMethods = append(s.ShowObjectMethods, newShowObjectTypeMethod(structName)) - default: - log.Println("No showObjectMethod found for kind:", methodKind) - } - } - return s -} - -func newShowObjectIDMethod(structName string, helperStructs []*Field, idType objectIdentifier) *ShowObjectMethod { +func newShowObjectIDMethod(structName string, helperStructs []*Field, idType objectIdentifierKind) *ShowObjectMethod { requiredFields := idTypeParts[idType] var args string for _, field := range requiredFields { diff --git a/pkg/sdk/poc/generator/show_object_methods_test.go b/pkg/sdk/poc/generator/show_object_methods_test.go index ce7ff731cf..53d2856f46 100644 --- a/pkg/sdk/poc/generator/show_object_methods_test.go +++ b/pkg/sdk/poc/generator/show_object_methods_test.go @@ -9,7 +9,7 @@ import ( func TestIdentifierStringToObjectIdentifier(t *testing.T) { tests := []struct { input string - expected objectIdentifier + expected objectIdentifierKind }{ {"AccountObjectIdentifier", AccountObjectIdentifier}, {"DatabaseObjectIdentifier", DatabaseObjectIdentifier},