Skip to content

Commit

Permalink
ref
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-fbudzynski committed Dec 18, 2024
1 parent a8ff21d commit a9d50cb
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 36 deletions.
12 changes: 6 additions & 6 deletions pkg/sdk/poc/generator/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
58 changes: 29 additions & 29 deletions pkg/sdk/poc/generator/show_object_methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/sdk/poc/generator/show_object_methods_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
func TestIdentifierStringToObjectIdentifier(t *testing.T) {
tests := []struct {
input string
expected objectIdentifier
expected objectIdentifierKind
}{
{"AccountObjectIdentifier", AccountObjectIdentifier},
{"DatabaseObjectIdentifier", DatabaseObjectIdentifier},
Expand Down

0 comments on commit a9d50cb

Please sign in to comment.