Skip to content

Commit

Permalink
rename
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-fbudzynski committed Dec 16, 2024
1 parent d610978 commit 72fc399
Show file tree
Hide file tree
Showing 12 changed files with 71 additions and 69 deletions.
1 change: 1 addition & 0 deletions pkg/sdk/api_integrations_def.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ var ApiIntegrationsDef = g.NewInterface(
Show().
SQL("API INTEGRATIONS").
OptionalLike(),
g.ResourceIDHelperMethod,
).
ShowByIdOperationWithFiltering(
g.ShowByIDLikeFiltering,
Expand Down
8 changes: 4 additions & 4 deletions pkg/sdk/api_integrations_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ type ApiIntegration struct {
CreatedOn time.Time
}

func (v *ApiIntegration) ID() AccountObjectIdentifier {
return NewAccountObjectIdentifier(v.Name)
}

// DescribeApiIntegrationOptions is based on https://docs.snowflake.com/en/sql-reference/sql/desc-integration.
type DescribeApiIntegrationOptions struct {
describe bool `ddl:"static" sql:"DESCRIBE"`
Expand All @@ -150,7 +154,3 @@ type ApiIntegrationProperty struct {
Value string
Default string
}

func (v *ApiIntegration) ID() AccountObjectIdentifier {
return NewAccountObjectIdentifier(v.Name)
}
1 change: 1 addition & 0 deletions pkg/sdk/notification_integrations_def.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ var NotificationIntegrationsDef = g.NewInterface(
Show().
SQL("NOTIFICATION INTEGRATIONS").
OptionalLike(),
g.ResourceIDHelperMethod,
).
ShowByIdOperationWithFiltering(
g.ShowByIDLikeFiltering,
Expand Down
8 changes: 4 additions & 4 deletions pkg/sdk/notification_integrations_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ type NotificationIntegration struct {
CreatedOn time.Time
}

func (v *NotificationIntegration) ID() AccountObjectIdentifier {
return NewAccountObjectIdentifier(v.Name)
}

// DescribeNotificationIntegrationOptions is based on https://docs.snowflake.com/en/sql-reference/sql/desc-integration.
type DescribeNotificationIntegrationOptions struct {
describe bool `ddl:"static" sql:"DESCRIBE"`
Expand All @@ -181,7 +185,3 @@ type NotificationIntegrationProperty struct {
Value string
Default string
}

func (v *NotificationIntegration) ID() AccountObjectIdentifier {
return NewAccountObjectIdentifier(v.Name)
}
74 changes: 36 additions & 38 deletions pkg/sdk/poc/generator/helper_methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,55 +30,29 @@ func identifierStringToObjectIdentifier(s string) objectIdentifier {
}
}

type ObjectHelperMethodKind uint
type ResourceHelperMethodKind uint

const (
ObjectHelperMethodID ObjectHelperMethodKind = iota
ObjectHelperMethodObjectType
ResourceIDHelperMethod ResourceHelperMethodKind = iota
ResourceObjectTypeHelperMethod
)

type HelperMethod struct {
type ResourceHelperMethod struct {
Name string
StructName string
ReturnValue string
ReturnType string
}

func newHelperMethod(name, structName, returnValue string, returnType string) *HelperMethod {
return &HelperMethod{
func newResourceHelperMethod(name, structName, returnValue string, returnType string) *ResourceHelperMethod {
return &ResourceHelperMethod{
Name: name,
StructName: structName,
ReturnValue: returnValue,
ReturnType: returnType,
}
}

func newObjectHelperMethodID(structName string, helperStructs []*Field, identifierString string) *HelperMethod {
objectIdentifier := identifierStringToObjectIdentifier(identifierString)
requiredFields, ok := requiredFieldsForIDMethodMapping[objectIdentifier]
if !ok {
log.Printf("WARNING: No required fields mapping defined for identifier %s", objectIdentifier)
return nil
}
if !hasRequiredFieldsForIDMethod(structName, helperStructs, requiredFields...) {
log.Printf("WARNING: Struct '%s' does not contain needed fields to build ID() helper method. Create the method manually in _ext file or add missing one of required fields: %v.\n", structName, requiredFields)
return nil
}

var args string
for _, field := range requiredFields {
args += fmt.Sprintf("v.%v, ", field)
}

returnValue := fmt.Sprintf("New%v(%v)", objectIdentifier, args)
return newHelperMethod("ID", structName, returnValue, string(objectIdentifier))
}

func newObjectHelperMethodObjectType(structName string) *HelperMethod {
returnValue := fmt.Sprintf("ObjectType%v", structName)
return newHelperMethod("ObjectType", structName, returnValue, "ObjectType")
}

var requiredFieldsForIDMethodMapping map[objectIdentifier][]string = map[objectIdentifier][]string{
AccountObjectIdentifier: {"Name"},
DatabaseObjectIdentifier: {"Name", "DatabaseName"},
Expand All @@ -105,17 +79,41 @@ func containsFieldNames(fields []*Field, names ...string) bool {
return false
}
}

return true
}

func (s *Operation) withObjectHelperMethods(structName string, helperMethods ...ObjectHelperMethodKind) *Operation {
func newResourceIDHelperMethod(structName string, helperStructs []*Field, identifierString string) *ResourceHelperMethod {
objectIdentifier := identifierStringToObjectIdentifier(identifierString)
requiredFields, ok := requiredFieldsForIDMethodMapping[objectIdentifier]
if !ok {
log.Printf("WARNING: No required fields mapping defined for identifier %s", objectIdentifier)
return nil
}
if !hasRequiredFieldsForIDMethod(structName, helperStructs, requiredFields...) {
log.Printf("WARNING: Struct '%s' does not contain needed fields to build ID() helper method. Create the method manually in _ext file or add missing one of required fields: %v.\n", structName, requiredFields)
return nil
}

var args string
for _, field := range requiredFields {
args += fmt.Sprintf("v.%v, ", field)
}

returnValue := fmt.Sprintf("New%v(%v)", objectIdentifier, args)
return newResourceHelperMethod("ID", structName, returnValue, string(objectIdentifier))
}

func newResourceObjectTypeHelperMethod(structName string) *ResourceHelperMethod {
return newResourceHelperMethod("ObjectType", structName, "ObjectType"+structName, "ObjectType")
}

func (s *Operation) withResourceHelperMethods(structName string, helperMethods ...ResourceHelperMethodKind) *Operation {
for _, helperMethod := range helperMethods {
switch helperMethod {
case ObjectHelperMethodID:
s.HelperMethods = append(s.HelperMethods, newObjectHelperMethodID(structName, s.HelperStructs, s.ObjectInterface.IdentifierKind))
case ObjectHelperMethodObjectType:
s.HelperMethods = append(s.HelperMethods, newObjectHelperMethodObjectType(structName))
case ResourceIDHelperMethod:
s.ResourceHelperMethods = append(s.ResourceHelperMethods, newResourceIDHelperMethod(structName, s.HelperStructs, s.ObjectInterface.IdentifierKind))
case ResourceObjectTypeHelperMethod:
s.ResourceHelperMethods = append(s.ResourceHelperMethods, newResourceObjectTypeHelperMethod(structName))
default:
log.Println("No object helper method found for kind:", helperMethod)
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/sdk/poc/generator/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ type Operation struct {
DescribeMapping *Mapping
// ShowByIDFiltering defines a kind of filterings performed in ShowByID operation
ShowByIDFiltering []ShowByIDFiltering
// HelperMethods contains helper methods for the Interface file (i.e. ID(), ObjectType())
HelperMethods []*HelperMethod
// ResourceHelperMethods contains helper methods for the Interface file (i.e. ID(), ObjectType())
ResourceHelperMethods []*ResourceHelperMethod
}

type Mapping struct {
Expand Down Expand Up @@ -124,7 +124,7 @@ func (i *Interface) newOperationWithDBMapping(
resourceRepresentation *plainStruct,
queryStruct *QueryStruct,
addMappingFunc func(op *Operation, from, to *Field),
objectHelperMethods ...ObjectHelperMethodKind,
objectHelperMethods ...ResourceHelperMethodKind,
) *Operation {
db := dbRepresentation.IntoField()
res := resourceRepresentation.IntoField()
Expand All @@ -136,7 +136,7 @@ func (i *Interface) newOperationWithDBMapping(
withHelperStruct(res).
withOptionsStruct(queryStruct.IntoField()).
withObjectInterface(i).
withObjectHelperMethods(res.Name, objectHelperMethods...)
withResourceHelperMethods(res.Name, objectHelperMethods...)

addMappingFunc(op, db, res)
i.Operations = append(i.Operations, op)
Expand Down Expand Up @@ -167,7 +167,7 @@ func (i *Interface) RevokeOperation(doc string, queryStruct *QueryStruct) *Inter
return i.newSimpleOperation(string(OperationKindRevoke), doc, queryStruct)
}

func (i *Interface) ShowOperation(doc string, dbRepresentation *dbStruct, resourceRepresentation *plainStruct, queryStruct *QueryStruct, helperMethods ...ObjectHelperMethodKind) *Interface {
func (i *Interface) ShowOperation(doc string, dbRepresentation *dbStruct, resourceRepresentation *plainStruct, queryStruct *QueryStruct, helperMethods ...ResourceHelperMethodKind) *Interface {
i.newOperationWithDBMapping(string(OperationKindShow), doc, dbRepresentation, resourceRepresentation, queryStruct, addShowMapping, helperMethods...)
return i
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/sdk/poc/generator/template_executors.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ func GenerateInterface(writer io.Writer, def *Interface) {
if o.OptsField != nil {
generateOptionsStruct(writer, o)
}
if o.HelperMethods != nil {
for _, m := range o.HelperMethods {
generateHelperMethods(writer, m)
if o.ResourceHelperMethods != nil {
for _, m := range o.ResourceHelperMethods {
generateHelperMethods(writer, m)
}
}
}
}

func generateHelperMethods(writer io.Writer, hm *HelperMethod) {
func generateHelperMethods(writer io.Writer, hm *ResourceHelperMethod) {
printTo(writer, HelperMethodTemplate, hm)
}

Expand Down
1 change: 1 addition & 0 deletions pkg/sdk/poc/generator/templates/helper_method.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
func (v *{{ .StructName }}) {{ .Name }}() {{ .ReturnType }} {
return {{ .ReturnValue }}
}

4 changes: 2 additions & 2 deletions pkg/sdk/secrets_def.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,8 @@ var SecretsDef = g.NewInterface(
SQL("SECRETS").
OptionalLike().
OptionalExtendedIn(),
g.ObjectHelperMethodID,
g.ObjectHelperMethodObjectType,
g.ResourceIDHelperMethod,
g.ResourceObjectTypeHelperMethod,
).ShowByIdOperationWithFiltering(
g.ShowByIDLikeFiltering,
g.ShowByIDExtendedInFiltering,
Expand Down
16 changes: 8 additions & 8 deletions pkg/sdk/secrets_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,14 @@ type Secret struct {
OwnerRoleType string
}

func (v *Secret) ID() SchemaObjectIdentifier {
return NewSchemaObjectIdentifier(v.Name, v.DatabaseName, v.SchemaName)
}

func (v *Secret) ObjectType() ObjectType {
return ObjectTypeSecret
}

// DescribeSecretOptions is based on https://docs.snowflake.com/en/sql-reference/sql/desc-secret.
type DescribeSecretOptions struct {
describe bool `ddl:"static" sql:"DESCRIBE"`
Expand Down Expand Up @@ -196,11 +204,3 @@ type SecretDetails struct {
OauthScopes []string
IntegrationName *string
}

func (v *Secret) ID() SchemaObjectIdentifier {
return NewSchemaObjectIdentifier(v.Name, v.DatabaseName, v.SchemaName)
}

func (v *Secret) ObjectType() ObjectType {
return ObjectTypeSecret
}
1 change: 1 addition & 0 deletions pkg/sdk/streamlits_def.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ var StreamlitsDef = g.NewInterface(
OptionalLike().
OptionalIn().
OptionalLimit(),
g.ResourceIDHelperMethod,
).ShowByIdOperationWithFiltering(
g.ShowByIDLikeFiltering,
g.ShowByIDInFiltering,
Expand Down
8 changes: 4 additions & 4 deletions pkg/sdk/streamlits_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ type Streamlit struct {
OwnerRoleType string
}

func (v *Streamlit) ID() SchemaObjectIdentifier {
return NewSchemaObjectIdentifier(v.Name, v.DatabaseName, v.SchemaName)
}

// DescribeStreamlitOptions is based on https://docs.snowflake.com/en/sql-reference/sql/desc-streamlit.
type DescribeStreamlitOptions struct {
describe bool `ddl:"static" sql:"DESCRIBE"`
Expand Down Expand Up @@ -137,7 +141,3 @@ type StreamlitDetail struct {
ExternalAccessIntegrations []string
ExternalAccessSecrets string
}

func (v *Streamlit) ID() SchemaObjectIdentifier {
return NewSchemaObjectIdentifier(v.DatabaseName, v.SchemaName, v.Name)
}

0 comments on commit 72fc399

Please sign in to comment.