From 72fc39995f3d8e9489000bc0f492c2653a294b14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Budzy=C5=84ski?= Date: Mon, 16 Dec 2024 16:41:26 +0100 Subject: [PATCH] rename --- pkg/sdk/api_integrations_def.go | 1 + pkg/sdk/api_integrations_gen.go | 8 +- pkg/sdk/notification_integrations_def.go | 1 + pkg/sdk/notification_integrations_gen.go | 8 +- pkg/sdk/poc/generator/helper_methods.go | 74 +++++++++---------- pkg/sdk/poc/generator/operation.go | 10 +-- pkg/sdk/poc/generator/template_executors.go | 8 +- .../generator/templates/helper_method.tmpl | 1 + pkg/sdk/secrets_def.go | 4 +- pkg/sdk/secrets_gen.go | 16 ++-- pkg/sdk/streamlits_def.go | 1 + pkg/sdk/streamlits_gen.go | 8 +- 12 files changed, 71 insertions(+), 69 deletions(-) diff --git a/pkg/sdk/api_integrations_def.go b/pkg/sdk/api_integrations_def.go index 83b1c588e9..c03b05fb40 100644 --- a/pkg/sdk/api_integrations_def.go +++ b/pkg/sdk/api_integrations_def.go @@ -150,6 +150,7 @@ var ApiIntegrationsDef = g.NewInterface( Show(). SQL("API INTEGRATIONS"). OptionalLike(), + g.ResourceIDHelperMethod, ). ShowByIdOperationWithFiltering( g.ShowByIDLikeFiltering, diff --git a/pkg/sdk/api_integrations_gen.go b/pkg/sdk/api_integrations_gen.go index 83f2783259..7d85c5286b 100644 --- a/pkg/sdk/api_integrations_gen.go +++ b/pkg/sdk/api_integrations_gen.go @@ -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"` @@ -150,7 +154,3 @@ type ApiIntegrationProperty struct { Value string Default string } - -func (v *ApiIntegration) ID() AccountObjectIdentifier { - return NewAccountObjectIdentifier(v.Name) -} diff --git a/pkg/sdk/notification_integrations_def.go b/pkg/sdk/notification_integrations_def.go index 23b6a80e1d..3c504dc833 100644 --- a/pkg/sdk/notification_integrations_def.go +++ b/pkg/sdk/notification_integrations_def.go @@ -180,6 +180,7 @@ var NotificationIntegrationsDef = g.NewInterface( Show(). SQL("NOTIFICATION INTEGRATIONS"). OptionalLike(), + g.ResourceIDHelperMethod, ). ShowByIdOperationWithFiltering( g.ShowByIDLikeFiltering, diff --git a/pkg/sdk/notification_integrations_gen.go b/pkg/sdk/notification_integrations_gen.go index 50d3c7977c..633912c92b 100644 --- a/pkg/sdk/notification_integrations_gen.go +++ b/pkg/sdk/notification_integrations_gen.go @@ -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"` @@ -181,7 +185,3 @@ type NotificationIntegrationProperty struct { Value string Default string } - -func (v *NotificationIntegration) ID() AccountObjectIdentifier { - return NewAccountObjectIdentifier(v.Name) -} diff --git a/pkg/sdk/poc/generator/helper_methods.go b/pkg/sdk/poc/generator/helper_methods.go index c886e05907..d16042a4f8 100644 --- a/pkg/sdk/poc/generator/helper_methods.go +++ b/pkg/sdk/poc/generator/helper_methods.go @@ -30,22 +30,22 @@ 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, @@ -53,32 +53,6 @@ func newHelperMethod(name, structName, returnValue string, returnType string) *H } } -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"}, @@ -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) } diff --git a/pkg/sdk/poc/generator/operation.go b/pkg/sdk/poc/generator/operation.go index 67b58d8611..f553126679 100644 --- a/pkg/sdk/poc/generator/operation.go +++ b/pkg/sdk/poc/generator/operation.go @@ -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 { @@ -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() @@ -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) @@ -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 } diff --git a/pkg/sdk/poc/generator/template_executors.go b/pkg/sdk/poc/generator/template_executors.go index 418133e03b..365d0eb0db 100644 --- a/pkg/sdk/poc/generator/template_executors.go +++ b/pkg/sdk/poc/generator/template_executors.go @@ -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) } diff --git a/pkg/sdk/poc/generator/templates/helper_method.tmpl b/pkg/sdk/poc/generator/templates/helper_method.tmpl index dae590da1f..6a5de7535c 100644 --- a/pkg/sdk/poc/generator/templates/helper_method.tmpl +++ b/pkg/sdk/poc/generator/templates/helper_method.tmpl @@ -3,3 +3,4 @@ func (v *{{ .StructName }}) {{ .Name }}() {{ .ReturnType }} { return {{ .ReturnValue }} } + diff --git a/pkg/sdk/secrets_def.go b/pkg/sdk/secrets_def.go index 00d91d6f9e..0a8ef8f3b6 100644 --- a/pkg/sdk/secrets_def.go +++ b/pkg/sdk/secrets_def.go @@ -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, diff --git a/pkg/sdk/secrets_gen.go b/pkg/sdk/secrets_gen.go index e3e31523c2..dc572ff6af 100644 --- a/pkg/sdk/secrets_gen.go +++ b/pkg/sdk/secrets_gen.go @@ -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"` @@ -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 -} diff --git a/pkg/sdk/streamlits_def.go b/pkg/sdk/streamlits_def.go index e7bf14bb13..c93fa20342 100644 --- a/pkg/sdk/streamlits_def.go +++ b/pkg/sdk/streamlits_def.go @@ -103,6 +103,7 @@ var StreamlitsDef = g.NewInterface( OptionalLike(). OptionalIn(). OptionalLimit(), + g.ResourceIDHelperMethod, ).ShowByIdOperationWithFiltering( g.ShowByIDLikeFiltering, g.ShowByIDInFiltering, diff --git a/pkg/sdk/streamlits_gen.go b/pkg/sdk/streamlits_gen.go index 01c0370777..208224363a 100644 --- a/pkg/sdk/streamlits_gen.go +++ b/pkg/sdk/streamlits_gen.go @@ -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"` @@ -137,7 +141,3 @@ type StreamlitDetail struct { ExternalAccessIntegrations []string ExternalAccessSecrets string } - -func (v *Streamlit) ID() SchemaObjectIdentifier { - return NewSchemaObjectIdentifier(v.DatabaseName, v.SchemaName, v.Name) -}