From cea5dd3572fff162e1724a9f7524fd17107acf97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Budzy=C5=84ski?= Date: Mon, 9 Dec 2024 17:03:47 +0100 Subject: [PATCH 01/36] init for interface helper methods --- pkg/sdk/poc/generator/helper_methods.go | 52 +++++++++++++++++++ pkg/sdk/poc/generator/interface.go | 2 + pkg/sdk/poc/generator/template_executors.go | 9 ++++ pkg/sdk/poc/generator/templates.go | 4 ++ .../generator/templates/helper_method.tmpl | 5 ++ 5 files changed, 72 insertions(+) create mode 100644 pkg/sdk/poc/generator/helper_methods.go create mode 100644 pkg/sdk/poc/generator/templates/helper_method.tmpl diff --git a/pkg/sdk/poc/generator/helper_methods.go b/pkg/sdk/poc/generator/helper_methods.go new file mode 100644 index 0000000000..b03ebf6e59 --- /dev/null +++ b/pkg/sdk/poc/generator/helper_methods.go @@ -0,0 +1,52 @@ +package generator + +import "fmt" + +type HelperMethod struct { + Name string + StructName string + ReturnValue string + ReturnType string +} + +func newHelperMethod(name, structName, returnValue, returnType string) *HelperMethod { + return &HelperMethod{ + Name: name, + StructName: structName, + ReturnValue: returnValue, + ReturnType: returnType, + } +} + +func newIDHelperMethod(idKind idPrefix, structName, returnType string) *HelperMethod { + var returnValue string + switch idKind { + case AccountIdentifierPrefix: + returnValue = "NewAccountObjectIdentifier(v.Name)" + case DatabaseIdentifierPrefix: + returnValue = "NewDatabaseObjectIdentifier(v.DatabaseName, v.Name)" + case SchemaIdentifierPrefix: + returnValue = "NewSchemaObjectIdentifier(v.DatabaseName, v.SchemaName, v.Name)" + default: + return nil + } + return newHelperMethod("ID", structName, returnValue, returnType) +} + +func newObjectTypeHelperMethod(structName string) *HelperMethod { + return newHelperMethod("ObjectType", structName, fmt.Sprintf("ObjectType%v", structName), "ObjectType") +} + +func (i *Interface) ID() *Interface { + i.HelperMethods = append(i.HelperMethods, newIDHelperMethod(i.ObjectIdentifierPrefix(), i.NameSingular, i.IdentifierKind)) + return i +} + +func (i *Interface) ObjectType() *Interface { + i.HelperMethods = append(i.HelperMethods, newObjectTypeHelperMethod(i.NameSingular)) + return i +} + +func (i *Interface) ObjectHelperMethods() *Interface { + return i.ID().ObjectType() +} diff --git a/pkg/sdk/poc/generator/interface.go b/pkg/sdk/poc/generator/interface.go index 98c213367e..41a7b57704 100644 --- a/pkg/sdk/poc/generator/interface.go +++ b/pkg/sdk/poc/generator/interface.go @@ -10,6 +10,8 @@ type Interface struct { Operations []*Operation // IdentifierKind keeps identifier of the underlying object (e.g. DatabaseObjectIdentifier) IdentifierKind string + + HelperMethods []*HelperMethod } func NewInterface(name string, nameSingular string, identifierKind string, operations ...*Operation) *Interface { diff --git a/pkg/sdk/poc/generator/template_executors.go b/pkg/sdk/poc/generator/template_executors.go index 00b4678410..90c64f2252 100644 --- a/pkg/sdk/poc/generator/template_executors.go +++ b/pkg/sdk/poc/generator/template_executors.go @@ -22,6 +22,15 @@ func GenerateInterface(writer io.Writer, def *Interface) { generateOptionsStruct(writer, o) } } + for _, m := range def.HelperMethods { + if m != nil { + generateHelperMethods(writer, m) + } + } +} + +func generateHelperMethods(writer io.Writer, hm *HelperMethod) { + printTo(writer, HelperMethodTemplate, hm) } func generateOptionsStruct(writer io.Writer, operation *Operation) { diff --git a/pkg/sdk/poc/generator/templates.go b/pkg/sdk/poc/generator/templates.go index 8d7d6b52b4..1f4ee80128 100644 --- a/pkg/sdk/poc/generator/templates.go +++ b/pkg/sdk/poc/generator/templates.go @@ -24,6 +24,10 @@ var ( structTemplateContent string StructTemplate, _ = template.New("structTemplate").Parse(structTemplateContent) + //go:embed templates/helper_method.tmpl + helperMethodTemplateContent string + HelperMethodTemplate, _ = template.New("helperMethodTemplate").Parse(helperMethodTemplateContent) + //go:embed templates/dto_declarations.tmpl dtoDeclarationsTemplateContent string DtoTemplate, _ = template.New("dtoTemplate").Parse(dtoDeclarationsTemplateContent) diff --git a/pkg/sdk/poc/generator/templates/helper_method.tmpl b/pkg/sdk/poc/generator/templates/helper_method.tmpl new file mode 100644 index 0000000000..dae590da1f --- /dev/null +++ b/pkg/sdk/poc/generator/templates/helper_method.tmpl @@ -0,0 +1,5 @@ +{{- /*gotype: github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/poc/generator.HelperMethod*/ -}} + +func (v *{{ .StructName }}) {{ .Name }}() {{ .ReturnType }} { + return {{ .ReturnValue }} +} From 47506327250c4426b2febf406c0bf7272b89f888 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Budzy=C5=84ski?= Date: Tue, 10 Dec 2024 14:07:18 +0100 Subject: [PATCH 02/36] enum --- pkg/sdk/poc/generator/helper_methods.go | 51 +++++++++++++++++-------- pkg/sdk/poc/generator/interface.go | 4 +- 2 files changed, 38 insertions(+), 17 deletions(-) diff --git a/pkg/sdk/poc/generator/helper_methods.go b/pkg/sdk/poc/generator/helper_methods.go index b03ebf6e59..b3b114d9d1 100644 --- a/pkg/sdk/poc/generator/helper_methods.go +++ b/pkg/sdk/poc/generator/helper_methods.go @@ -2,6 +2,30 @@ package generator import "fmt" +type objectIdentifier string + +const ( + AccountObjectIdentifier objectIdentifier = "AccountObjectIdentifier" + DatabaseObjectIdentifier objectIdentifier = "DatabaseObjectIdentifier" + SchemaObjectIdentifier objectIdentifier = "SchemaObjectIdentifier" + SchemaObjectIdentifierWithArguments objectIdentifier = "SchemaObjectIdentifierWithArguments" +) + +func identifierStringToObjectIdentifier(s string) objectIdentifier { + switch s { + case "AccountObjectIdentifier": + return AccountObjectIdentifier + case "DatabaseObjectIdentifier": + return DatabaseObjectIdentifier + case "SchemaObjectIdentifier": + return SchemaObjectIdentifier + case "SchemaObjectIdentifierWithArguments": + return SchemaObjectIdentifierWithArguments + default: + return "" + } +} + type HelperMethod struct { Name string StructName string @@ -18,19 +42,20 @@ func newHelperMethod(name, structName, returnValue, returnType string) *HelperMe } } -func newIDHelperMethod(idKind idPrefix, structName, returnType string) *HelperMethod { - var returnValue string - switch idKind { - case AccountIdentifierPrefix: - returnValue = "NewAccountObjectIdentifier(v.Name)" - case DatabaseIdentifierPrefix: - returnValue = "NewDatabaseObjectIdentifier(v.DatabaseName, v.Name)" - case SchemaIdentifierPrefix: - returnValue = "NewSchemaObjectIdentifier(v.DatabaseName, v.SchemaName, v.Name)" +func newIDHelperMethod(structName, objectIdentifier string) *HelperMethod { + var args string + switch objectIdentifier { + case "AccountObjectIdentifier": + args = "v.Name" + case "DatabaseObjectIdentifier": + args = "v.DatabaseName, v.Name" + case "SchemaObjectIdentifier": + args = "v.DatabaseName, v.SchemaName, v.Name" default: return nil } - return newHelperMethod("ID", structName, returnValue, returnType) + returnValue := fmt.Sprintf("New%v(%v)", objectIdentifier, args) + return newHelperMethod("ID", structName, returnValue, objectIdentifier) } func newObjectTypeHelperMethod(structName string) *HelperMethod { @@ -38,7 +63,7 @@ func newObjectTypeHelperMethod(structName string) *HelperMethod { } func (i *Interface) ID() *Interface { - i.HelperMethods = append(i.HelperMethods, newIDHelperMethod(i.ObjectIdentifierPrefix(), i.NameSingular, i.IdentifierKind)) + i.HelperMethods = append(i.HelperMethods, newIDHelperMethod(i.NameSingular, i.IdentifierKind)) return i } @@ -46,7 +71,3 @@ func (i *Interface) ObjectType() *Interface { i.HelperMethods = append(i.HelperMethods, newObjectTypeHelperMethod(i.NameSingular)) return i } - -func (i *Interface) ObjectHelperMethods() *Interface { - return i.ID().ObjectType() -} diff --git a/pkg/sdk/poc/generator/interface.go b/pkg/sdk/poc/generator/interface.go index 41a7b57704..88f8713ac7 100644 --- a/pkg/sdk/poc/generator/interface.go +++ b/pkg/sdk/poc/generator/interface.go @@ -10,8 +10,8 @@ type Interface struct { Operations []*Operation // IdentifierKind keeps identifier of the underlying object (e.g. DatabaseObjectIdentifier) IdentifierKind string - - HelperMethods []*HelperMethod + // HelperMethods contains helper methods for the Interface file (i.e. ID(), ObjectType()) + HelperMethods []*HelperMethod } func NewInterface(name string, nameSingular string, identifierKind string, operations ...*Operation) *Interface { From e051890c1a84b40ba37c70b286e15d7d1dcb05e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Budzy=C5=84ski?= Date: Tue, 10 Dec 2024 14:16:51 +0100 Subject: [PATCH 03/36] refactor --- pkg/sdk/poc/generator/helper_methods.go | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/pkg/sdk/poc/generator/helper_methods.go b/pkg/sdk/poc/generator/helper_methods.go index b3b114d9d1..4606c64a0b 100644 --- a/pkg/sdk/poc/generator/helper_methods.go +++ b/pkg/sdk/poc/generator/helper_methods.go @@ -33,7 +33,7 @@ type HelperMethod struct { ReturnType string } -func newHelperMethod(name, structName, returnValue, returnType string) *HelperMethod { +func newHelperMethod(name, structName, returnValue string, returnType string) *HelperMethod { return &HelperMethod{ Name: name, StructName: structName, @@ -42,28 +42,31 @@ func newHelperMethod(name, structName, returnValue, returnType string) *HelperMe } } -func newIDHelperMethod(structName, objectIdentifier string) *HelperMethod { +func newIDHelperMethod(structName string, objectIdentifier objectIdentifier) *HelperMethod { var args string switch objectIdentifier { - case "AccountObjectIdentifier": + case AccountObjectIdentifier: args = "v.Name" - case "DatabaseObjectIdentifier": + case DatabaseObjectIdentifier: args = "v.DatabaseName, v.Name" - case "SchemaObjectIdentifier": + case SchemaObjectIdentifier: args = "v.DatabaseName, v.SchemaName, v.Name" default: return nil } + returnValue := fmt.Sprintf("New%v(%v)", objectIdentifier, args) - return newHelperMethod("ID", structName, returnValue, objectIdentifier) + return newHelperMethod("ID", structName, returnValue, string(objectIdentifier)) } func newObjectTypeHelperMethod(structName string) *HelperMethod { - return newHelperMethod("ObjectType", structName, fmt.Sprintf("ObjectType%v", structName), "ObjectType") + returnValue := fmt.Sprintf("ObjectType%v", structName) + return newHelperMethod("ObjectType", structName, returnValue, "ObjectType") } func (i *Interface) ID() *Interface { - i.HelperMethods = append(i.HelperMethods, newIDHelperMethod(i.NameSingular, i.IdentifierKind)) + idKind := identifierStringToObjectIdentifier(i.IdentifierKind) + i.HelperMethods = append(i.HelperMethods, newIDHelperMethod(i.NameSingular, idKind)) return i } From 86d55bd02e032225fc8be946a931af638876618e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Budzy=C5=84ski?= Date: Tue, 10 Dec 2024 14:25:14 +0100 Subject: [PATCH 04/36] doc to helper methods --- pkg/sdk/poc/generator/helper_methods.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pkg/sdk/poc/generator/helper_methods.go b/pkg/sdk/poc/generator/helper_methods.go index 4606c64a0b..8b1219cd35 100644 --- a/pkg/sdk/poc/generator/helper_methods.go +++ b/pkg/sdk/poc/generator/helper_methods.go @@ -64,13 +64,15 @@ func newObjectTypeHelperMethod(structName string) *HelperMethod { return newHelperMethod("ObjectType", structName, returnValue, "ObjectType") } -func (i *Interface) ID() *Interface { +// HelperMethodID adds a helper method "ID()" to the interface file that returns the ObjectIdentifier of the object +func (i *Interface) HelperMethodID() *Interface { idKind := identifierStringToObjectIdentifier(i.IdentifierKind) i.HelperMethods = append(i.HelperMethods, newIDHelperMethod(i.NameSingular, idKind)) return i } -func (i *Interface) ObjectType() *Interface { +// HelperMethodObjectType adds a helper method "ObjectType()" to the interface file that returns the ObjectType for the struct +func (i *Interface) HelperMethodObjectType() *Interface { i.HelperMethods = append(i.HelperMethods, newObjectTypeHelperMethod(i.NameSingular)) return i } From cf927efbb0d713b512ecb8c580152193288cdc8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Budzy=C5=84ski?= Date: Tue, 10 Dec 2024 14:28:19 +0100 Subject: [PATCH 05/36] generated for streamlits --- pkg/sdk/streamlits_def.go | 2 +- pkg/sdk/streamlits_gen.go | 11 +++++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/pkg/sdk/streamlits_def.go b/pkg/sdk/streamlits_def.go index e7bf14bb13..5559d8df12 100644 --- a/pkg/sdk/streamlits_def.go +++ b/pkg/sdk/streamlits_def.go @@ -138,4 +138,4 @@ var StreamlitsDef = g.NewInterface( SQL("STREAMLIT"). Name(). WithValidation(g.ValidIdentifier, "name"), -) +).HelperMethodID() diff --git a/pkg/sdk/streamlits_gen.go b/pkg/sdk/streamlits_gen.go index ce08a1c4c2..01c0370777 100644 --- a/pkg/sdk/streamlits_gen.go +++ b/pkg/sdk/streamlits_gen.go @@ -28,6 +28,7 @@ type CreateStreamlitOptions struct { Title *string `ddl:"parameter,single_quotes" sql:"TITLE"` Comment *string `ddl:"parameter,single_quotes" sql:"COMMENT"` } + type ExternalAccessIntegrations struct { ExternalAccessIntegrations []AccountObjectIdentifier `ddl:"list,must_parentheses"` } @@ -42,6 +43,7 @@ type AlterStreamlitOptions struct { Unset *StreamlitUnset `ddl:"list,no_parentheses" sql:"UNSET"` RenameTo *SchemaObjectIdentifier `ddl:"identifier" sql:"RENAME TO"` } + type StreamlitSet struct { RootLocation *string `ddl:"parameter,single_quotes" sql:"ROOT_LOCATION"` MainFile *string `ddl:"parameter,single_quotes" sql:"MAIN_FILE"` @@ -50,6 +52,7 @@ type StreamlitSet struct { Comment *string `ddl:"parameter,single_quotes" sql:"COMMENT"` Title *string `ddl:"parameter,single_quotes" sql:"TITLE"` } + type StreamlitUnset struct { QueryWarehouse *bool `ddl:"keyword" sql:"QUERY_WAREHOUSE"` Comment *bool `ddl:"keyword" sql:"COMMENT"` @@ -73,6 +76,7 @@ type ShowStreamlitOptions struct { In *In `ddl:"keyword" sql:"IN"` Limit *LimitFrom `ddl:"keyword" sql:"LIMIT"` } + type streamlitsRow struct { CreatedOn string `db:"created_on"` Name string `db:"name"` @@ -85,6 +89,7 @@ type streamlitsRow struct { UrlId string `db:"url_id"` OwnerRoleType string `db:"owner_role_type"` } + type Streamlit struct { CreatedOn string Name string @@ -104,6 +109,7 @@ type DescribeStreamlitOptions struct { streamlit bool `ddl:"static" sql:"STREAMLIT"` name SchemaObjectIdentifier `ddl:"identifier"` } + type streamlitsDetailRow struct { Name string `db:"name"` Title sql.NullString `db:"title"` @@ -117,6 +123,7 @@ type streamlitsDetailRow struct { ExternalAccessIntegrations string `db:"external_access_integrations"` ExternalAccessSecrets string `db:"external_access_secrets"` } + type StreamlitDetail struct { Name string Title string @@ -131,6 +138,6 @@ type StreamlitDetail struct { ExternalAccessSecrets string } -func (s *Streamlit) ID() SchemaObjectIdentifier { - return NewSchemaObjectIdentifier(s.DatabaseName, s.SchemaName, s.Name) +func (v *Streamlit) ID() SchemaObjectIdentifier { + return NewSchemaObjectIdentifier(v.DatabaseName, v.SchemaName, v.Name) } From 495e236b63108a91713862c91861a48ceded0426 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Budzy=C5=84ski?= Date: Tue, 10 Dec 2024 14:32:54 +0100 Subject: [PATCH 06/36] generated for notification integrations --- pkg/sdk/notification_integrations_def.go | 2 +- pkg/sdk/notification_integrations_gen.go | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/pkg/sdk/notification_integrations_def.go b/pkg/sdk/notification_integrations_def.go index 5af67bc681..c332f2af0a 100644 --- a/pkg/sdk/notification_integrations_def.go +++ b/pkg/sdk/notification_integrations_def.go @@ -200,4 +200,4 @@ var NotificationIntegrationsDef = g.NewInterface( SQL("NOTIFICATION INTEGRATION"). Name(). WithValidation(g.ValidIdentifier, "name"), - ) + ).HelperMethodID() diff --git a/pkg/sdk/notification_integrations_gen.go b/pkg/sdk/notification_integrations_gen.go index 633912c92b..50d3c7977c 100644 --- a/pkg/sdk/notification_integrations_gen.go +++ b/pkg/sdk/notification_integrations_gen.go @@ -161,10 +161,6 @@ 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"` @@ -185,3 +181,7 @@ type NotificationIntegrationProperty struct { Value string Default string } + +func (v *NotificationIntegration) ID() AccountObjectIdentifier { + return NewAccountObjectIdentifier(v.Name) +} From c9cb8d78c871f934d4a89f35d7b8c7ed2c5dabf1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Budzy=C5=84ski?= Date: Tue, 10 Dec 2024 15:59:06 +0100 Subject: [PATCH 07/36] checks for missing fields to create ID() method --- pkg/sdk/poc/generator/helper_methods.go | 44 ++++++++++++++++++++++++- pkg/sdk/streamlits_def.go | 2 +- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/pkg/sdk/poc/generator/helper_methods.go b/pkg/sdk/poc/generator/helper_methods.go index 8b1219cd35..479bfabd89 100644 --- a/pkg/sdk/poc/generator/helper_methods.go +++ b/pkg/sdk/poc/generator/helper_methods.go @@ -1,6 +1,9 @@ package generator -import "fmt" +import ( + "fmt" + "slices" +) type objectIdentifier string @@ -64,8 +67,47 @@ func newObjectTypeHelperMethod(structName string) *HelperMethod { return newHelperMethod("ObjectType", structName, returnValue, "ObjectType") } +func containsFieldNames(fields []*Field, names ...string) bool { + var fieldNames []string + for _, field := range fields { + fieldNames = append(fieldNames, field.Name) + } + for _, name := range names { + if !slices.Contains(fieldNames, name) { + return false + } + } + return true +} + +func checkRequiredFieldsForIDHelperMethod(operations []*Operation, name string, id objectIdentifier) bool { + for _, op := range operations { + if op.Name != string(OperationKindShow) { + continue + } + for _, field := range op.HelperStructs { + if field.Name != name { + continue + } + requiredFields := []string{"Name"} + switch id { + case DatabaseObjectIdentifier: + requiredFields = append(requiredFields, "DatabaseName") + case SchemaObjectIdentifier: + requiredFields = append(requiredFields, "DatabaseName", "SchemaName") + } + return containsFieldNames(field.Fields, requiredFields...) + } + } + return false +} + // HelperMethodID adds a helper method "ID()" to the interface file that returns the ObjectIdentifier of the object func (i *Interface) HelperMethodID() *Interface { + if !checkRequiredFieldsForIDHelperMethod(i.Operations, i.NameSingular, identifierStringToObjectIdentifier(i.IdentifierKind)) { + fmt.Println("WARNING: Does not contain needed fields for ID helper method. Create the method manually in _ext file or add missing fields.") + return i + } idKind := identifierStringToObjectIdentifier(i.IdentifierKind) i.HelperMethods = append(i.HelperMethods, newIDHelperMethod(i.NameSingular, idKind)) return i diff --git a/pkg/sdk/streamlits_def.go b/pkg/sdk/streamlits_def.go index 5559d8df12..e7bf14bb13 100644 --- a/pkg/sdk/streamlits_def.go +++ b/pkg/sdk/streamlits_def.go @@ -138,4 +138,4 @@ var StreamlitsDef = g.NewInterface( SQL("STREAMLIT"). Name(). WithValidation(g.ValidIdentifier, "name"), -).HelperMethodID() +) From 8fdc1dd38767adbec2291be9b5dd82e5857b2944 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Budzy=C5=84ski?= Date: Thu, 12 Dec 2024 08:29:26 +0100 Subject: [PATCH 08/36] small fix --- pkg/sdk/poc/generator/helper_methods.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pkg/sdk/poc/generator/helper_methods.go b/pkg/sdk/poc/generator/helper_methods.go index 479bfabd89..402c608efe 100644 --- a/pkg/sdk/poc/generator/helper_methods.go +++ b/pkg/sdk/poc/generator/helper_methods.go @@ -2,7 +2,6 @@ package generator import ( "fmt" - "slices" ) type objectIdentifier string @@ -68,12 +67,13 @@ func newObjectTypeHelperMethod(structName string) *HelperMethod { } func containsFieldNames(fields []*Field, names ...string) bool { - var fieldNames []string + fieldNames := map[string]bool{} for _, field := range fields { - fieldNames = append(fieldNames, field.Name) + fieldNames[field.Name] = true } + for _, name := range names { - if !slices.Contains(fieldNames, name) { + if _, ok := fieldNames[name]; !ok { return false } } From a3784c0471cf5855861357cc36c320bbae163326 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Budzy=C5=84ski?= Date: Thu, 12 Dec 2024 14:24:53 +0100 Subject: [PATCH 09/36] slight refactor and generated some examples --- pkg/sdk/api_integrations_gen.go | 8 +-- pkg/sdk/notification_integrations_def.go | 2 +- pkg/sdk/poc/generator/helper_methods.go | 63 ++++++++++-------------- pkg/sdk/secrets_gen.go | 27 +++++++--- 4 files changed, 51 insertions(+), 49 deletions(-) diff --git a/pkg/sdk/api_integrations_gen.go b/pkg/sdk/api_integrations_gen.go index 7d85c5286b..83f2783259 100644 --- a/pkg/sdk/api_integrations_gen.go +++ b/pkg/sdk/api_integrations_gen.go @@ -130,10 +130,6 @@ 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"` @@ -154,3 +150,7 @@ 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 c332f2af0a..5af67bc681 100644 --- a/pkg/sdk/notification_integrations_def.go +++ b/pkg/sdk/notification_integrations_def.go @@ -200,4 +200,4 @@ var NotificationIntegrationsDef = g.NewInterface( SQL("NOTIFICATION INTEGRATION"). Name(). WithValidation(g.ValidIdentifier, "name"), - ).HelperMethodID() + ) diff --git a/pkg/sdk/poc/generator/helper_methods.go b/pkg/sdk/poc/generator/helper_methods.go index 402c608efe..9375b8248a 100644 --- a/pkg/sdk/poc/generator/helper_methods.go +++ b/pkg/sdk/poc/generator/helper_methods.go @@ -44,32 +44,26 @@ func newHelperMethod(name, structName, returnValue string, returnType string) *H } } +var requiredFieldsForIDMethodMapping map[objectIdentifier][]string = map[objectIdentifier][]string{ + AccountObjectIdentifier: {"Name"}, + DatabaseObjectIdentifier: {"Name", "DatabaseName"}, + SchemaObjectIdentifier: {"Name", "DatabaseName", "SchemaName"}, +} + func newIDHelperMethod(structName string, objectIdentifier objectIdentifier) *HelperMethod { var args string - switch objectIdentifier { - case AccountObjectIdentifier: - args = "v.Name" - case DatabaseObjectIdentifier: - args = "v.DatabaseName, v.Name" - case SchemaObjectIdentifier: - args = "v.DatabaseName, v.SchemaName, v.Name" - default: - return nil + fields := requiredFieldsForIDMethodMapping[objectIdentifier] + for _, field := range fields { + args += fmt.Sprintf("v.%v, ", field) } - returnValue := fmt.Sprintf("New%v(%v)", objectIdentifier, args) return newHelperMethod("ID", structName, returnValue, string(objectIdentifier)) } -func newObjectTypeHelperMethod(structName string) *HelperMethod { - returnValue := fmt.Sprintf("ObjectType%v", structName) - return newHelperMethod("ObjectType", structName, returnValue, "ObjectType") -} - func containsFieldNames(fields []*Field, names ...string) bool { - fieldNames := map[string]bool{} + fieldNames := map[string]any{} for _, field := range fields { - fieldNames[field.Name] = true + fieldNames[field.Name] = nil } for _, name := range names { @@ -80,36 +74,33 @@ func containsFieldNames(fields []*Field, names ...string) bool { return true } -func checkRequiredFieldsForIDHelperMethod(operations []*Operation, name string, id objectIdentifier) bool { +func hasRequiredFields(operations []*Operation, structName string, requiredFields ...string) bool { for _, op := range operations { - if op.Name != string(OperationKindShow) { - continue - } - for _, field := range op.HelperStructs { - if field.Name != name { - continue + if op.Name == string(OperationKindShow) { + for _, field := range op.HelperStructs { + if field.Name == structName { + return containsFieldNames(field.Fields, requiredFields...) + } } - requiredFields := []string{"Name"} - switch id { - case DatabaseObjectIdentifier: - requiredFields = append(requiredFields, "DatabaseName") - case SchemaObjectIdentifier: - requiredFields = append(requiredFields, "DatabaseName", "SchemaName") - } - return containsFieldNames(field.Fields, requiredFields...) } } return false } +func newObjectTypeHelperMethod(structName string) *HelperMethod { + returnValue := fmt.Sprintf("ObjectType%v", structName) + return newHelperMethod("ObjectType", structName, returnValue, "ObjectType") +} + // HelperMethodID adds a helper method "ID()" to the interface file that returns the ObjectIdentifier of the object func (i *Interface) HelperMethodID() *Interface { - if !checkRequiredFieldsForIDHelperMethod(i.Operations, i.NameSingular, identifierStringToObjectIdentifier(i.IdentifierKind)) { - fmt.Println("WARNING: Does not contain needed fields for ID helper method. Create the method manually in _ext file or add missing fields.") + identifierKind := identifierStringToObjectIdentifier(i.IdentifierKind) + requiredFeilds := requiredFieldsForIDMethodMapping[identifierKind] + if !hasRequiredFields(i.Operations, i.NameSingular, requiredFeilds...) { + fmt.Sprintf("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.", i.NameSingular, requiredFeilds) return i } - idKind := identifierStringToObjectIdentifier(i.IdentifierKind) - i.HelperMethods = append(i.HelperMethods, newIDHelperMethod(i.NameSingular, idKind)) + i.HelperMethods = append(i.HelperMethods, newIDHelperMethod(i.NameSingular, identifierKind)) return i } diff --git a/pkg/sdk/secrets_gen.go b/pkg/sdk/secrets_gen.go index 30d467f25e..e3e31523c2 100644 --- a/pkg/sdk/secrets_gen.go +++ b/pkg/sdk/secrets_gen.go @@ -30,9 +30,11 @@ type CreateWithOAuthClientCredentialsFlowSecretOptions struct { OauthScopes *OauthScopesList `ddl:"parameter,parentheses" sql:"OAUTH_SCOPES"` Comment *string `ddl:"parameter,single_quotes" sql:"COMMENT"` } + type ApiIntegrationScope struct { Scope string `ddl:"keyword,single_quotes"` } + type OauthScopesList struct { OauthScopesList []ApiIntegrationScope `ddl:"list,must_parentheses"` } @@ -85,30 +87,37 @@ type AlterSecretOptions struct { Set *SecretSet `ddl:"keyword" sql:"SET"` Unset *SecretUnset `ddl:"keyword"` } + type SecretSet struct { Comment *string `ddl:"parameter,single_quotes" sql:"COMMENT"` SetForFlow *SetForFlow `ddl:"keyword"` } + type SetForFlow struct { SetForOAuthClientCredentials *SetForOAuthClientCredentials `ddl:"keyword"` SetForOAuthAuthorization *SetForOAuthAuthorization `ddl:"keyword"` SetForBasicAuthentication *SetForBasicAuthentication `ddl:"keyword"` SetForGenericString *SetForGenericString `ddl:"keyword"` } + type SetForOAuthClientCredentials struct { OauthScopes *OauthScopesList `ddl:"parameter,parentheses" sql:"OAUTH_SCOPES"` } + type SetForOAuthAuthorization struct { OauthRefreshToken *string `ddl:"parameter,single_quotes" sql:"OAUTH_REFRESH_TOKEN"` OauthRefreshTokenExpiryTime *string `ddl:"parameter,single_quotes" sql:"OAUTH_REFRESH_TOKEN_EXPIRY_TIME"` } + type SetForBasicAuthentication struct { Username *string `ddl:"parameter,single_quotes" sql:"USERNAME"` Password *string `ddl:"parameter,single_quotes" sql:"PASSWORD"` } + type SetForGenericString struct { SecretString *string `ddl:"parameter,single_quotes" sql:"SECRET_STRING"` } + type SecretUnset struct { Comment *bool `ddl:"keyword" sql:"SET COMMENT = NULL"` } @@ -128,6 +137,7 @@ type ShowSecretOptions struct { Like *Like `ddl:"keyword" sql:"LIKE"` In *ExtendedIn `ddl:"keyword" sql:"IN"` } + type secretDBRow struct { CreatedOn time.Time `db:"created_on"` Name string `db:"name"` @@ -139,6 +149,7 @@ type secretDBRow struct { OauthScopes sql.NullString `db:"oauth_scopes"` OwnerRoleType string `db:"owner_role_type"` } + type Secret struct { CreatedOn time.Time Name string @@ -151,14 +162,6 @@ type Secret struct { OwnerRoleType string } -func (s *Secret) ID() SchemaObjectIdentifier { - return NewSchemaObjectIdentifier(s.DatabaseName, s.SchemaName, s.Name) -} - -func (s *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"` @@ -193,3 +196,11 @@ 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 +} From 590cb483abd4ca7841937db58f34abac47abbd64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Budzy=C5=84ski?= Date: Thu, 12 Dec 2024 15:13:22 +0100 Subject: [PATCH 10/36] ff --- pkg/sdk/api_integrations_def.go | 2 +- pkg/sdk/notification_integrations_def.go | 2 +- pkg/sdk/secrets_def.go | 3 ++- pkg/sdk/streamlits_def.go | 2 +- 4 files changed, 5 insertions(+), 4 deletions(-) diff --git a/pkg/sdk/api_integrations_def.go b/pkg/sdk/api_integrations_def.go index 9625b23e72..376fd2c81c 100644 --- a/pkg/sdk/api_integrations_def.go +++ b/pkg/sdk/api_integrations_def.go @@ -170,4 +170,4 @@ var ApiIntegrationsDef = g.NewInterface( SQL("API INTEGRATION"). Name(). WithValidation(g.ValidIdentifier, "name"), - ) + ).HelperMethodID() diff --git a/pkg/sdk/notification_integrations_def.go b/pkg/sdk/notification_integrations_def.go index 5af67bc681..c332f2af0a 100644 --- a/pkg/sdk/notification_integrations_def.go +++ b/pkg/sdk/notification_integrations_def.go @@ -200,4 +200,4 @@ var NotificationIntegrationsDef = g.NewInterface( SQL("NOTIFICATION INTEGRATION"). Name(). WithValidation(g.ValidIdentifier, "name"), - ) + ).HelperMethodID() diff --git a/pkg/sdk/secrets_def.go b/pkg/sdk/secrets_def.go index 2598c8f497..11ee23c32f 100644 --- a/pkg/sdk/secrets_def.go +++ b/pkg/sdk/secrets_def.go @@ -254,4 +254,5 @@ var SecretsDef = g.NewInterface( SQL("SECRET"). Name(). WithValidation(g.ValidIdentifier, "name"), -) +).HelperMethodID(). + HelperMethodObjectType() diff --git a/pkg/sdk/streamlits_def.go b/pkg/sdk/streamlits_def.go index e7bf14bb13..5559d8df12 100644 --- a/pkg/sdk/streamlits_def.go +++ b/pkg/sdk/streamlits_def.go @@ -138,4 +138,4 @@ var StreamlitsDef = g.NewInterface( SQL("STREAMLIT"). Name(). WithValidation(g.ValidIdentifier, "name"), -) +).HelperMethodID() From 78a01e3b3f31304b5a1454d5bddec9dfe9cccde8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Budzy=C5=84ski?= Date: Thu, 12 Dec 2024 15:33:21 +0100 Subject: [PATCH 11/36] log change --- pkg/sdk/poc/generator/helper_methods.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pkg/sdk/poc/generator/helper_methods.go b/pkg/sdk/poc/generator/helper_methods.go index 9375b8248a..b363e1e2fb 100644 --- a/pkg/sdk/poc/generator/helper_methods.go +++ b/pkg/sdk/poc/generator/helper_methods.go @@ -2,6 +2,7 @@ package generator import ( "fmt" + "log" ) type objectIdentifier string @@ -97,7 +98,7 @@ func (i *Interface) HelperMethodID() *Interface { identifierKind := identifierStringToObjectIdentifier(i.IdentifierKind) requiredFeilds := requiredFieldsForIDMethodMapping[identifierKind] if !hasRequiredFields(i.Operations, i.NameSingular, requiredFeilds...) { - fmt.Sprintf("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.", i.NameSingular, requiredFeilds) + 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", i.NameSingular, requiredFeilds) return i } i.HelperMethods = append(i.HelperMethods, newIDHelperMethod(i.NameSingular, identifierKind)) From d5d1aa6ae93b94ab79aecf221566eb0e92f96016 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Budzy=C5=84ski?= Date: Thu, 12 Dec 2024 15:45:16 +0100 Subject: [PATCH 12/36] ref --- pkg/sdk/poc/generator/helper_methods.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/pkg/sdk/poc/generator/helper_methods.go b/pkg/sdk/poc/generator/helper_methods.go index b363e1e2fb..daf5b3a206 100644 --- a/pkg/sdk/poc/generator/helper_methods.go +++ b/pkg/sdk/poc/generator/helper_methods.go @@ -77,11 +77,12 @@ func containsFieldNames(fields []*Field, names ...string) bool { func hasRequiredFields(operations []*Operation, structName string, requiredFields ...string) bool { for _, op := range operations { - if op.Name == string(OperationKindShow) { - for _, field := range op.HelperStructs { - if field.Name == structName { - return containsFieldNames(field.Fields, requiredFields...) - } + if op.Name != string(OperationKindShow) { + continue + } + for _, field := range op.HelperStructs { + if field.Name == structName { + return containsFieldNames(field.Fields, requiredFields...) } } } From e1a27df996c5ab88079f7e73f5af3b9988f3d283 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Budzy=C5=84ski?= Date: Thu, 12 Dec 2024 15:54:30 +0100 Subject: [PATCH 13/36] ref --- pkg/sdk/poc/generator/helper_methods.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/pkg/sdk/poc/generator/helper_methods.go b/pkg/sdk/poc/generator/helper_methods.go index daf5b3a206..cd7388cd7f 100644 --- a/pkg/sdk/poc/generator/helper_methods.go +++ b/pkg/sdk/poc/generator/helper_methods.go @@ -75,7 +75,7 @@ func containsFieldNames(fields []*Field, names ...string) bool { return true } -func hasRequiredFields(operations []*Operation, structName string, requiredFields ...string) bool { +func hasRequiredFieldsForIDMethod(operations []*Operation, structName string, requiredFields ...string) bool { for _, op := range operations { if op.Name != string(OperationKindShow) { continue @@ -85,7 +85,9 @@ func hasRequiredFields(operations []*Operation, structName string, requiredField return containsFieldNames(field.Fields, requiredFields...) } } + log.Printf("WARNING: Struct '%s' not found in '%s' operation. Couldn't generate ID() helper method.", structName, OperationKindShow) } + log.Printf("WARNING: '%s' not found. Couldn't generate ID() helper method.", OperationKindShow) return false } @@ -97,9 +99,9 @@ func newObjectTypeHelperMethod(structName string) *HelperMethod { // HelperMethodID adds a helper method "ID()" to the interface file that returns the ObjectIdentifier of the object func (i *Interface) HelperMethodID() *Interface { identifierKind := identifierStringToObjectIdentifier(i.IdentifierKind) - requiredFeilds := requiredFieldsForIDMethodMapping[identifierKind] - if !hasRequiredFields(i.Operations, i.NameSingular, requiredFeilds...) { - 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", i.NameSingular, requiredFeilds) + requiredFields := requiredFieldsForIDMethodMapping[identifierKind] + if !hasRequiredFieldsForIDMethod(i.Operations, i.NameSingular, 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", i.NameSingular, requiredFields) return i } i.HelperMethods = append(i.HelperMethods, newIDHelperMethod(i.NameSingular, identifierKind)) From 9f29c422c3fb2175fcdcd555a6b47c010a02a993 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Budzy=C5=84ski?= Date: Mon, 16 Dec 2024 10:36:10 +0100 Subject: [PATCH 14/36] ref --- pkg/sdk/poc/generator/helper_methods.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/pkg/sdk/poc/generator/helper_methods.go b/pkg/sdk/poc/generator/helper_methods.go index cd7388cd7f..320a966816 100644 --- a/pkg/sdk/poc/generator/helper_methods.go +++ b/pkg/sdk/poc/generator/helper_methods.go @@ -61,6 +61,11 @@ func newIDHelperMethod(structName string, objectIdentifier objectIdentifier) *He return newHelperMethod("ID", structName, returnValue, string(objectIdentifier)) } +func newObjectTypeHelperMethod(structName string) *HelperMethod { + returnValue := fmt.Sprintf("ObjectType%v", structName) + return newHelperMethod("ObjectType", structName, returnValue, "ObjectType") +} + func containsFieldNames(fields []*Field, names ...string) bool { fieldNames := map[string]any{} for _, field := range fields { @@ -85,17 +90,12 @@ func hasRequiredFieldsForIDMethod(operations []*Operation, structName string, re return containsFieldNames(field.Fields, requiredFields...) } } - log.Printf("WARNING: Struct '%s' not found in '%s' operation. Couldn't generate ID() helper method.", structName, OperationKindShow) + log.Printf("WARNING: Struct: '%s' not found in '%s' operation. Couldn't generate ID() helper method.", structName, OperationKindShow) } - log.Printf("WARNING: '%s' not found. Couldn't generate ID() helper method.", OperationKindShow) + log.Printf("WARNING: Operation: '%s' not found. Couldn't generate ID() helper method.", OperationKindShow) return false } -func newObjectTypeHelperMethod(structName string) *HelperMethod { - returnValue := fmt.Sprintf("ObjectType%v", structName) - return newHelperMethod("ObjectType", structName, returnValue, "ObjectType") -} - // HelperMethodID adds a helper method "ID()" to the interface file that returns the ObjectIdentifier of the object func (i *Interface) HelperMethodID() *Interface { identifierKind := identifierStringToObjectIdentifier(i.IdentifierKind) From d610978fb6d972c8966d8ae2b8f362022412fada Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Budzy=C5=84ski?= Date: Mon, 16 Dec 2024 16:24:22 +0100 Subject: [PATCH 15/36] refacotred to be on Operatin level --- pkg/sdk/api_integrations_def.go | 2 +- pkg/sdk/notification_integrations_def.go | 2 +- pkg/sdk/poc/generator/helper_methods.go | 93 +++++++++++---------- pkg/sdk/poc/generator/interface.go | 3 +- pkg/sdk/poc/generator/operation.go | 17 +++- pkg/sdk/poc/generator/template_executors.go | 8 +- pkg/sdk/secrets_def.go | 5 +- pkg/sdk/streamlits_def.go | 2 +- 8 files changed, 76 insertions(+), 56 deletions(-) diff --git a/pkg/sdk/api_integrations_def.go b/pkg/sdk/api_integrations_def.go index 4e1426b98e..83b1c588e9 100644 --- a/pkg/sdk/api_integrations_def.go +++ b/pkg/sdk/api_integrations_def.go @@ -172,4 +172,4 @@ var ApiIntegrationsDef = g.NewInterface( SQL("API INTEGRATION"). Name(). WithValidation(g.ValidIdentifier, "name"), - ).HelperMethodID() + ) diff --git a/pkg/sdk/notification_integrations_def.go b/pkg/sdk/notification_integrations_def.go index b6fac7bec0..23b6a80e1d 100644 --- a/pkg/sdk/notification_integrations_def.go +++ b/pkg/sdk/notification_integrations_def.go @@ -202,4 +202,4 @@ var NotificationIntegrationsDef = g.NewInterface( SQL("NOTIFICATION INTEGRATION"). Name(). WithValidation(g.ValidIdentifier, "name"), - ).HelperMethodID() + ) diff --git a/pkg/sdk/poc/generator/helper_methods.go b/pkg/sdk/poc/generator/helper_methods.go index 320a966816..c886e05907 100644 --- a/pkg/sdk/poc/generator/helper_methods.go +++ b/pkg/sdk/poc/generator/helper_methods.go @@ -3,6 +3,7 @@ package generator import ( "fmt" "log" + "slices" ) type objectIdentifier string @@ -29,6 +30,13 @@ func identifierStringToObjectIdentifier(s string) objectIdentifier { } } +type ObjectHelperMethodKind uint + +const ( + ObjectHelperMethodID ObjectHelperMethodKind = iota + ObjectHelperMethodObjectType +) + type HelperMethod struct { Name string StructName string @@ -45,71 +53,72 @@ func newHelperMethod(name, structName, returnValue string, returnType string) *H } } -var requiredFieldsForIDMethodMapping map[objectIdentifier][]string = map[objectIdentifier][]string{ - AccountObjectIdentifier: {"Name"}, - DatabaseObjectIdentifier: {"Name", "DatabaseName"}, - SchemaObjectIdentifier: {"Name", "DatabaseName", "SchemaName"}, -} +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 + } -func newIDHelperMethod(structName string, objectIdentifier objectIdentifier) *HelperMethod { var args string - fields := requiredFieldsForIDMethodMapping[objectIdentifier] - for _, field := range fields { + 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 newObjectTypeHelperMethod(structName string) *HelperMethod { +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"}, + SchemaObjectIdentifier: {"Name", "DatabaseName", "SchemaName"}, +} + +func hasRequiredFieldsForIDMethod(structName string, helperStructs []*Field, requiredFields ...string) bool { + for _, field := range helperStructs { + if field.Name == structName { + return containsFieldNames(field.Fields, requiredFields...) + } + } + return false +} + func containsFieldNames(fields []*Field, names ...string) bool { - fieldNames := map[string]any{} + fieldNames := []string{} for _, field := range fields { - fieldNames[field.Name] = nil + fieldNames = append(fieldNames, field.Name) } for _, name := range names { - if _, ok := fieldNames[name]; !ok { + if !slices.Contains(names, name) { return false } } + return true } -func hasRequiredFieldsForIDMethod(operations []*Operation, structName string, requiredFields ...string) bool { - for _, op := range operations { - if op.Name != string(OperationKindShow) { - continue - } - for _, field := range op.HelperStructs { - if field.Name == structName { - return containsFieldNames(field.Fields, requiredFields...) - } +func (s *Operation) withObjectHelperMethods(structName string, helperMethods ...ObjectHelperMethodKind) *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)) + default: + log.Println("No object helper method found for kind:", helperMethod) } - log.Printf("WARNING: Struct: '%s' not found in '%s' operation. Couldn't generate ID() helper method.", structName, OperationKindShow) - } - log.Printf("WARNING: Operation: '%s' not found. Couldn't generate ID() helper method.", OperationKindShow) - return false -} - -// HelperMethodID adds a helper method "ID()" to the interface file that returns the ObjectIdentifier of the object -func (i *Interface) HelperMethodID() *Interface { - identifierKind := identifierStringToObjectIdentifier(i.IdentifierKind) - requiredFields := requiredFieldsForIDMethodMapping[identifierKind] - if !hasRequiredFieldsForIDMethod(i.Operations, i.NameSingular, 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", i.NameSingular, requiredFields) - return i } - i.HelperMethods = append(i.HelperMethods, newIDHelperMethod(i.NameSingular, identifierKind)) - return i -} - -// HelperMethodObjectType adds a helper method "ObjectType()" to the interface file that returns the ObjectType for the struct -func (i *Interface) HelperMethodObjectType() *Interface { - i.HelperMethods = append(i.HelperMethods, newObjectTypeHelperMethod(i.NameSingular)) - return i + return s } diff --git a/pkg/sdk/poc/generator/interface.go b/pkg/sdk/poc/generator/interface.go index 88f8713ac7..99789e5618 100644 --- a/pkg/sdk/poc/generator/interface.go +++ b/pkg/sdk/poc/generator/interface.go @@ -10,8 +10,6 @@ type Interface struct { Operations []*Operation // IdentifierKind keeps identifier of the underlying object (e.g. DatabaseObjectIdentifier) IdentifierKind string - // HelperMethods contains helper methods for the Interface file (i.e. ID(), ObjectType()) - HelperMethods []*HelperMethod } func NewInterface(name string, nameSingular string, identifierKind string, operations ...*Operation) *Interface { @@ -33,3 +31,4 @@ func (i *Interface) ObjectIdentifierPrefix() idPrefix { // return strings.Replace(i.IdentifierKind, "ObjectIdentifier", "", 1) return identifierStringToPrefix(i.IdentifierKind) } + diff --git a/pkg/sdk/poc/generator/operation.go b/pkg/sdk/poc/generator/operation.go index dfc20ca034..67b58d8611 100644 --- a/pkg/sdk/poc/generator/operation.go +++ b/pkg/sdk/poc/generator/operation.go @@ -40,6 +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 } type Mapping struct { @@ -79,6 +81,11 @@ func (s *Operation) withHelperStructs(helperStructs ...*Field) *Operation { return s } +func (i *Operation) withObjectInterface(objectInterface *Interface) *Operation { + i.ObjectInterface = objectInterface + return i +} + func addShowMapping(op *Operation, from, to *Field) { op.ShowMapping = newMapping("convert", from, to) } @@ -117,6 +124,7 @@ func (i *Interface) newOperationWithDBMapping( resourceRepresentation *plainStruct, queryStruct *QueryStruct, addMappingFunc func(op *Operation, from, to *Field), + objectHelperMethods ...ObjectHelperMethodKind, ) *Operation { db := dbRepresentation.IntoField() res := resourceRepresentation.IntoField() @@ -126,7 +134,10 @@ func (i *Interface) newOperationWithDBMapping( op := newOperation(kind, doc). withHelperStruct(db). withHelperStruct(res). - withOptionsStruct(queryStruct.IntoField()) + withOptionsStruct(queryStruct.IntoField()). + withObjectInterface(i). + withObjectHelperMethods(res.Name, objectHelperMethods...) + addMappingFunc(op, db, res) i.Operations = append(i.Operations, op) return op @@ -156,8 +167,8 @@ 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) *Interface { - i.newOperationWithDBMapping(string(OperationKindShow), doc, dbRepresentation, resourceRepresentation, queryStruct, addShowMapping) +func (i *Interface) ShowOperation(doc string, dbRepresentation *dbStruct, resourceRepresentation *plainStruct, queryStruct *QueryStruct, helperMethods ...ObjectHelperMethodKind) *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 f051172219..418133e03b 100644 --- a/pkg/sdk/poc/generator/template_executors.go +++ b/pkg/sdk/poc/generator/template_executors.go @@ -21,10 +21,10 @@ func GenerateInterface(writer io.Writer, def *Interface) { if o.OptsField != nil { generateOptionsStruct(writer, o) } - } - for _, m := range def.HelperMethods { - if m != nil { - generateHelperMethods(writer, m) + if o.HelperMethods != nil { + for _, m := range o.HelperMethods { + generateHelperMethods(writer, m) + } } } } diff --git a/pkg/sdk/secrets_def.go b/pkg/sdk/secrets_def.go index 11ee23c32f..00d91d6f9e 100644 --- a/pkg/sdk/secrets_def.go +++ b/pkg/sdk/secrets_def.go @@ -241,6 +241,8 @@ var SecretsDef = g.NewInterface( SQL("SECRETS"). OptionalLike(). OptionalExtendedIn(), + g.ObjectHelperMethodID, + g.ObjectHelperMethodObjectType, ).ShowByIdOperationWithFiltering( g.ShowByIDLikeFiltering, g.ShowByIDExtendedInFiltering, @@ -254,5 +256,4 @@ var SecretsDef = g.NewInterface( SQL("SECRET"). Name(). WithValidation(g.ValidIdentifier, "name"), -).HelperMethodID(). - HelperMethodObjectType() +) diff --git a/pkg/sdk/streamlits_def.go b/pkg/sdk/streamlits_def.go index 5559d8df12..e7bf14bb13 100644 --- a/pkg/sdk/streamlits_def.go +++ b/pkg/sdk/streamlits_def.go @@ -138,4 +138,4 @@ var StreamlitsDef = g.NewInterface( SQL("STREAMLIT"). Name(). WithValidation(g.ValidIdentifier, "name"), -).HelperMethodID() +) 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 16/36] 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) -} From 475a570186ae6d6d8e7e7487b14cf5f065db40f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Budzy=C5=84ski?= Date: Mon, 16 Dec 2024 16:43:15 +0100 Subject: [PATCH 17/36] small ref --- pkg/sdk/poc/generator/operation.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/sdk/poc/generator/operation.go b/pkg/sdk/poc/generator/operation.go index f553126679..a4f6ffb7dd 100644 --- a/pkg/sdk/poc/generator/operation.go +++ b/pkg/sdk/poc/generator/operation.go @@ -181,9 +181,9 @@ func (i *Interface) ShowByIdOperationWithNoFiltering() *Interface { // ShowByIdOperationWithFiltering adds a ShowByID operation to the interface with filtering. Should be used for objects that implement filtering options e.g. Like or In. func (i *Interface) ShowByIdOperationWithFiltering(filter ShowByIDFilteringKind, filtering ...ShowByIDFilteringKind) *Interface { - op := newNoSqlOperation(string(OperationKindShowByID)) - op.ObjectInterface = i - op.withFiltering(append([]ShowByIDFilteringKind{filter}, filtering...)...) + op := newNoSqlOperation(string(OperationKindShowByID)). + withObjectInterface(i). + withFiltering(append(filtering, filter)...) i.Operations = append(i.Operations, op) return i } From 388abfc10e22677863cdff6dfc3ac49c7e847899 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Budzy=C5=84ski?= Date: Mon, 16 Dec 2024 16:47:31 +0100 Subject: [PATCH 18/36] rename --- .../generator/{helper_methods.go => resource_helper_methods.go} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename pkg/sdk/poc/generator/{helper_methods.go => resource_helper_methods.go} (100%) diff --git a/pkg/sdk/poc/generator/helper_methods.go b/pkg/sdk/poc/generator/resource_helper_methods.go similarity index 100% rename from pkg/sdk/poc/generator/helper_methods.go rename to pkg/sdk/poc/generator/resource_helper_methods.go From 546e12552d3d9ea2073e37971756085c51fe6391 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Budzy=C5=84ski?= Date: Mon, 16 Dec 2024 16:48:44 +0100 Subject: [PATCH 19/36] self rev --- pkg/sdk/poc/generator/operation.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/sdk/poc/generator/operation.go b/pkg/sdk/poc/generator/operation.go index a4f6ffb7dd..9c97850fdd 100644 --- a/pkg/sdk/poc/generator/operation.go +++ b/pkg/sdk/poc/generator/operation.go @@ -81,9 +81,9 @@ func (s *Operation) withHelperStructs(helperStructs ...*Field) *Operation { return s } -func (i *Operation) withObjectInterface(objectInterface *Interface) *Operation { - i.ObjectInterface = objectInterface - return i +func (s *Operation) withObjectInterface(objectInterface *Interface) *Operation { + s.ObjectInterface = objectInterface + return s } func addShowMapping(op *Operation, from, to *Field) { From 64fea516ee59c34b6ec9081674ec4ff8fc295191 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Budzy=C5=84ski?= Date: Mon, 16 Dec 2024 16:49:30 +0100 Subject: [PATCH 20/36] self rev --- pkg/sdk/poc/generator/operation.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/sdk/poc/generator/operation.go b/pkg/sdk/poc/generator/operation.go index 9c97850fdd..8377b79179 100644 --- a/pkg/sdk/poc/generator/operation.go +++ b/pkg/sdk/poc/generator/operation.go @@ -124,7 +124,7 @@ func (i *Interface) newOperationWithDBMapping( resourceRepresentation *plainStruct, queryStruct *QueryStruct, addMappingFunc func(op *Operation, from, to *Field), - objectHelperMethods ...ResourceHelperMethodKind, + resourceHelperMethods ...ResourceHelperMethodKind, ) *Operation { db := dbRepresentation.IntoField() res := resourceRepresentation.IntoField() @@ -136,7 +136,7 @@ func (i *Interface) newOperationWithDBMapping( withHelperStruct(res). withOptionsStruct(queryStruct.IntoField()). withObjectInterface(i). - withResourceHelperMethods(res.Name, objectHelperMethods...) + withResourceHelperMethods(res.Name, resourceHelperMethods...) addMappingFunc(op, db, res) i.Operations = append(i.Operations, op) From 3f4429928ac8465e78c808d0d5822a7c62e52b79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Budzy=C5=84ski?= Date: Mon, 16 Dec 2024 16:52:07 +0100 Subject: [PATCH 21/36] self rev --- pkg/sdk/poc/generator/resource_helper_methods.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/sdk/poc/generator/resource_helper_methods.go b/pkg/sdk/poc/generator/resource_helper_methods.go index d16042a4f8..4fc8d66b75 100644 --- a/pkg/sdk/poc/generator/resource_helper_methods.go +++ b/pkg/sdk/poc/generator/resource_helper_methods.go @@ -115,7 +115,7 @@ func (s *Operation) withResourceHelperMethods(structName string, helperMethods . case ResourceObjectTypeHelperMethod: s.ResourceHelperMethods = append(s.ResourceHelperMethods, newResourceObjectTypeHelperMethod(structName)) default: - log.Println("No object helper method found for kind:", helperMethod) + log.Println("No resourceHelperMethod found for kind:", helperMethod) } } return s From 89876035c5ca299ff39b58ac321972b4651ad1b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Budzy=C5=84ski?= Date: Mon, 16 Dec 2024 16:54:47 +0100 Subject: [PATCH 22/36] self rev --- pkg/sdk/poc/generator/template_executors.go | 2 +- pkg/sdk/poc/generator/templates.go | 6 +++--- .../{helper_method.tmpl => resource_helper_method.tmpl} | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) rename pkg/sdk/poc/generator/templates/{helper_method.tmpl => resource_helper_method.tmpl} (74%) diff --git a/pkg/sdk/poc/generator/template_executors.go b/pkg/sdk/poc/generator/template_executors.go index 365d0eb0db..2304177830 100644 --- a/pkg/sdk/poc/generator/template_executors.go +++ b/pkg/sdk/poc/generator/template_executors.go @@ -30,7 +30,7 @@ func GenerateInterface(writer io.Writer, def *Interface) { } func generateHelperMethods(writer io.Writer, hm *ResourceHelperMethod) { - printTo(writer, HelperMethodTemplate, hm) + printTo(writer, ResourceHelperMethodTemplate, hm) } func generateOptionsStruct(writer io.Writer, operation *Operation) { diff --git a/pkg/sdk/poc/generator/templates.go b/pkg/sdk/poc/generator/templates.go index cfd6087d63..a4e8e505f8 100644 --- a/pkg/sdk/poc/generator/templates.go +++ b/pkg/sdk/poc/generator/templates.go @@ -24,9 +24,9 @@ var ( structTemplateContent string StructTemplate, _ = template.New("structTemplate").Parse(structTemplateContent) - //go:embed templates/helper_method.tmpl - helperMethodTemplateContent string - HelperMethodTemplate, _ = template.New("helperMethodTemplate").Parse(helperMethodTemplateContent) + //go:embed templates/resource_helper_method.tmpl + resourceHelperMethodTemplateContent string + ResourceHelperMethodTemplate, _ = template.New("helperMethodTemplate").Parse(resourceHelperMethodTemplateContent) //go:embed templates/dto_declarations.tmpl dtoDeclarationsTemplateContent string diff --git a/pkg/sdk/poc/generator/templates/helper_method.tmpl b/pkg/sdk/poc/generator/templates/resource_helper_method.tmpl similarity index 74% rename from pkg/sdk/poc/generator/templates/helper_method.tmpl rename to pkg/sdk/poc/generator/templates/resource_helper_method.tmpl index 6a5de7535c..b573de755f 100644 --- a/pkg/sdk/poc/generator/templates/helper_method.tmpl +++ b/pkg/sdk/poc/generator/templates/resource_helper_method.tmpl @@ -1,4 +1,4 @@ -{{- /*gotype: github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/poc/generator.HelperMethod*/ -}} +{{- /*gotype: github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/poc/generator.ResourceHelperMethod*/ -}} func (v *{{ .StructName }}) {{ .Name }}() {{ .ReturnType }} { return {{ .ReturnValue }} From 05914c304c19eb9003b8e365d06f13e15e5728e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Budzy=C5=84ski?= Date: Tue, 17 Dec 2024 00:08:40 +0100 Subject: [PATCH 23/36] self rev --- pkg/sdk/poc/generator/resource_helper_methods.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/sdk/poc/generator/resource_helper_methods.go b/pkg/sdk/poc/generator/resource_helper_methods.go index 4fc8d66b75..dd66de9446 100644 --- a/pkg/sdk/poc/generator/resource_helper_methods.go +++ b/pkg/sdk/poc/generator/resource_helper_methods.go @@ -75,7 +75,7 @@ func containsFieldNames(fields []*Field, names ...string) bool { } for _, name := range names { - if !slices.Contains(names, name) { + if !slices.Contains(fieldNames, name) { return false } } From 39542322ea9a7a19ddbf07d0d122aed43077cced Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Budzy=C5=84ski?= Date: Wed, 18 Dec 2024 11:22:54 +0100 Subject: [PATCH 24/36] rev --- pkg/sdk/poc/generator/operation.go | 10 ++-- ...lper_methods.go => show_object_methods.go} | 60 +++++++++---------- pkg/sdk/poc/generator/template_executors.go | 8 +-- 3 files changed, 38 insertions(+), 40 deletions(-) rename pkg/sdk/poc/generator/{resource_helper_methods.go => show_object_methods.go} (51%) diff --git a/pkg/sdk/poc/generator/operation.go b/pkg/sdk/poc/generator/operation.go index 8377b79179..870b47db0d 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 - // ResourceHelperMethods contains helper methods for the Interface file (i.e. ID(), ObjectType()) - ResourceHelperMethods []*ResourceHelperMethod + // ShowObjectMethods contains helper methods for the Interface file (i.e. ID(), ObjectType()) + ShowObjectMethods []*ShowObjectMethod } type Mapping struct { @@ -124,7 +124,7 @@ func (i *Interface) newOperationWithDBMapping( resourceRepresentation *plainStruct, queryStruct *QueryStruct, addMappingFunc func(op *Operation, from, to *Field), - resourceHelperMethods ...ResourceHelperMethodKind, + resourceHelperMethods ...ShowObjectMethodKind, ) *Operation { db := dbRepresentation.IntoField() res := resourceRepresentation.IntoField() @@ -136,7 +136,7 @@ func (i *Interface) newOperationWithDBMapping( withHelperStruct(res). withOptionsStruct(queryStruct.IntoField()). withObjectInterface(i). - withResourceHelperMethods(res.Name, resourceHelperMethods...) + withShowObjectMethods(res.Name, resourceHelperMethods...) 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 ...ResourceHelperMethodKind) *Interface { +func (i *Interface) ShowOperation(doc string, dbRepresentation *dbStruct, resourceRepresentation *plainStruct, queryStruct *QueryStruct, helperMethods ...ShowObjectMethodKind) *Interface { i.newOperationWithDBMapping(string(OperationKindShow), doc, dbRepresentation, resourceRepresentation, queryStruct, addShowMapping, helperMethods...) return i } diff --git a/pkg/sdk/poc/generator/resource_helper_methods.go b/pkg/sdk/poc/generator/show_object_methods.go similarity index 51% rename from pkg/sdk/poc/generator/resource_helper_methods.go rename to pkg/sdk/poc/generator/show_object_methods.go index dd66de9446..332b2c84c6 100644 --- a/pkg/sdk/poc/generator/resource_helper_methods.go +++ b/pkg/sdk/poc/generator/show_object_methods.go @@ -30,22 +30,22 @@ func identifierStringToObjectIdentifier(s string) objectIdentifier { } } -type ResourceHelperMethodKind uint +type ShowObjectMethodKind uint const ( - ResourceIDHelperMethod ResourceHelperMethodKind = iota - ResourceObjectTypeHelperMethod + ShowObjectIdMethod ShowObjectMethodKind = iota + ShowObjectTypeMethod ) -type ResourceHelperMethod struct { +type ShowObjectMethod struct { Name string StructName string ReturnValue string ReturnType string } -func newResourceHelperMethod(name, structName, returnValue string, returnType string) *ResourceHelperMethod { - return &ResourceHelperMethod{ +func newShowObjectMethod(name, structName, returnValue string, returnType string) *ShowObjectMethod { + return &ShowObjectMethod{ Name: name, StructName: structName, ReturnValue: returnValue, @@ -53,10 +53,10 @@ func newResourceHelperMethod(name, structName, returnValue string, returnType st } } -var requiredFieldsForIDMethodMapping map[objectIdentifier][]string = map[objectIdentifier][]string{ +var idTypeParts map[objectIdentifier][]string = map[objectIdentifier][]string{ AccountObjectIdentifier: {"Name"}, - DatabaseObjectIdentifier: {"Name", "DatabaseName"}, - SchemaObjectIdentifier: {"Name", "DatabaseName", "SchemaName"}, + DatabaseObjectIdentifier: {"DatabaseName", "Name"}, + SchemaObjectIdentifier: {"DatabaseName", "SchemaName", "Name"}, } func hasRequiredFieldsForIDMethod(structName string, helperStructs []*Field, requiredFields ...string) bool { @@ -82,15 +82,29 @@ func containsFieldNames(fields []*Field, names ...string) bool { return true } -func newResourceIDHelperMethod(structName string, helperStructs []*Field, identifierString string) *ResourceHelperMethod { +func (s *Operation) withShowObjectMethods(structName string, showObjectMethodsKind ...ShowObjectMethodKind) *Operation { + for _, methodKind := range showObjectMethodsKind { + switch methodKind { + case ShowObjectIdMethod: + s.ShowObjectMethods = append(s.ShowObjectMethods, newShowObjectIDMethod(structName, s.HelperStructs, s.ObjectInterface.IdentifierKind)) + 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, identifierString string) *ShowObjectMethod { objectIdentifier := identifierStringToObjectIdentifier(identifierString) - requiredFields, ok := requiredFieldsForIDMethodMapping[objectIdentifier] + requiredFields, ok := idTypeParts[objectIdentifier] if !ok { - log.Printf("WARNING: No required fields mapping defined for identifier %s", objectIdentifier) + log.Printf("[WARN]: 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) + 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 one of required fields: %v.\n", structName, requiredFields) return nil } @@ -100,23 +114,9 @@ func newResourceIDHelperMethod(structName string, helperStructs []*Field, identi } returnValue := fmt.Sprintf("New%v(%v)", objectIdentifier, args) - return newResourceHelperMethod("ID", structName, returnValue, string(objectIdentifier)) + return newShowObjectMethod("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 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 resourceHelperMethod found for kind:", helperMethod) - } - } - return s +func newShowObjectTypeMethod(structName string) *ShowObjectMethod { + return newShowObjectMethod("ObjectType", structName, "ObjectType"+structName, "ObjectType") } diff --git a/pkg/sdk/poc/generator/template_executors.go b/pkg/sdk/poc/generator/template_executors.go index 2304177830..b23084b4f4 100644 --- a/pkg/sdk/poc/generator/template_executors.go +++ b/pkg/sdk/poc/generator/template_executors.go @@ -21,15 +21,13 @@ func GenerateInterface(writer io.Writer, def *Interface) { if o.OptsField != nil { generateOptionsStruct(writer, o) } - if o.ResourceHelperMethods != nil { - for _, m := range o.ResourceHelperMethods { - generateHelperMethods(writer, m) - } + for _, m := range o.ShowObjectMethods { + generateShowObjectMethods(writer, m) } } } -func generateHelperMethods(writer io.Writer, hm *ResourceHelperMethod) { +func generateShowObjectMethods(writer io.Writer, hm *ShowObjectMethod) { printTo(writer, ResourceHelperMethodTemplate, hm) } From 05c44e52635cac862edd35c3d78507cfa49196ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Budzy=C5=84ski?= Date: Wed, 18 Dec 2024 11:24:54 +0100 Subject: [PATCH 25/36] def fixes --- pkg/sdk/api_integrations_def.go | 2 +- pkg/sdk/notification_integrations_def.go | 2 +- pkg/sdk/secrets_def.go | 4 ++-- pkg/sdk/secrets_gen.go | 2 +- pkg/sdk/streamlits_def.go | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/pkg/sdk/api_integrations_def.go b/pkg/sdk/api_integrations_def.go index c03b05fb40..baf4ba51e4 100644 --- a/pkg/sdk/api_integrations_def.go +++ b/pkg/sdk/api_integrations_def.go @@ -150,7 +150,7 @@ var ApiIntegrationsDef = g.NewInterface( Show(). SQL("API INTEGRATIONS"). OptionalLike(), - g.ResourceIDHelperMethod, + g.ShowObjectIdMethod, ). ShowByIdOperationWithFiltering( g.ShowByIDLikeFiltering, diff --git a/pkg/sdk/notification_integrations_def.go b/pkg/sdk/notification_integrations_def.go index 3c504dc833..37b9db6cda 100644 --- a/pkg/sdk/notification_integrations_def.go +++ b/pkg/sdk/notification_integrations_def.go @@ -180,7 +180,7 @@ var NotificationIntegrationsDef = g.NewInterface( Show(). SQL("NOTIFICATION INTEGRATIONS"). OptionalLike(), - g.ResourceIDHelperMethod, + g.ShowObjectIdMethod, ). ShowByIdOperationWithFiltering( g.ShowByIDLikeFiltering, diff --git a/pkg/sdk/secrets_def.go b/pkg/sdk/secrets_def.go index 0a8ef8f3b6..4c2f0482b1 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.ResourceIDHelperMethod, - g.ResourceObjectTypeHelperMethod, + g.ShowObjectIdMethod, + g.ShowObjectTypeMethod, ).ShowByIdOperationWithFiltering( g.ShowByIDLikeFiltering, g.ShowByIDExtendedInFiltering, diff --git a/pkg/sdk/secrets_gen.go b/pkg/sdk/secrets_gen.go index dc572ff6af..17e2f0a110 100644 --- a/pkg/sdk/secrets_gen.go +++ b/pkg/sdk/secrets_gen.go @@ -163,7 +163,7 @@ type Secret struct { } func (v *Secret) ID() SchemaObjectIdentifier { - return NewSchemaObjectIdentifier(v.Name, v.DatabaseName, v.SchemaName) + return NewSchemaObjectIdentifier(v.DatabaseName, v.SchemaName, v.Name) } func (v *Secret) ObjectType() ObjectType { diff --git a/pkg/sdk/streamlits_def.go b/pkg/sdk/streamlits_def.go index c93fa20342..f3a27fc1cb 100644 --- a/pkg/sdk/streamlits_def.go +++ b/pkg/sdk/streamlits_def.go @@ -103,7 +103,7 @@ var StreamlitsDef = g.NewInterface( OptionalLike(). OptionalIn(). OptionalLimit(), - g.ResourceIDHelperMethod, + g.ShowObjectIdMethod, ).ShowByIdOperationWithFiltering( g.ShowByIDLikeFiltering, g.ShowByIDInFiltering, From 9e757be8daa86a0ea39820a52d7e183208c76a74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Budzy=C5=84ski?= Date: Wed, 18 Dec 2024 11:27:49 +0100 Subject: [PATCH 26/36] order fixed --- pkg/sdk/streamlits_gen.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/sdk/streamlits_gen.go b/pkg/sdk/streamlits_gen.go index 208224363a..8f90b6a6a4 100644 --- a/pkg/sdk/streamlits_gen.go +++ b/pkg/sdk/streamlits_gen.go @@ -104,7 +104,7 @@ type Streamlit struct { } func (v *Streamlit) ID() SchemaObjectIdentifier { - return NewSchemaObjectIdentifier(v.Name, v.DatabaseName, v.SchemaName) + return NewSchemaObjectIdentifier(v.DatabaseName, v.SchemaName, v.Name) } // DescribeStreamlitOptions is based on https://docs.snowflake.com/en/sql-reference/sql/desc-streamlit. From 8183ff7b8005c7a581f4c141523adf257c34c8b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Budzy=C5=84ski?= Date: Wed, 18 Dec 2024 11:29:54 +0100 Subject: [PATCH 27/36] rename... --- pkg/sdk/poc/generator/template_executors.go | 2 +- pkg/sdk/poc/generator/templates.go | 6 +++--- ...{resource_helper_method.tmpl => show_object_method.tmpl} | 0 3 files changed, 4 insertions(+), 4 deletions(-) rename pkg/sdk/poc/generator/templates/{resource_helper_method.tmpl => show_object_method.tmpl} (100%) diff --git a/pkg/sdk/poc/generator/template_executors.go b/pkg/sdk/poc/generator/template_executors.go index b23084b4f4..cbe68fd120 100644 --- a/pkg/sdk/poc/generator/template_executors.go +++ b/pkg/sdk/poc/generator/template_executors.go @@ -28,7 +28,7 @@ func GenerateInterface(writer io.Writer, def *Interface) { } func generateShowObjectMethods(writer io.Writer, hm *ShowObjectMethod) { - printTo(writer, ResourceHelperMethodTemplate, hm) + printTo(writer, ShowObjectMethodTemplate, hm) } func generateOptionsStruct(writer io.Writer, operation *Operation) { diff --git a/pkg/sdk/poc/generator/templates.go b/pkg/sdk/poc/generator/templates.go index a4e8e505f8..c10b3cc157 100644 --- a/pkg/sdk/poc/generator/templates.go +++ b/pkg/sdk/poc/generator/templates.go @@ -24,9 +24,9 @@ var ( structTemplateContent string StructTemplate, _ = template.New("structTemplate").Parse(structTemplateContent) - //go:embed templates/resource_helper_method.tmpl - resourceHelperMethodTemplateContent string - ResourceHelperMethodTemplate, _ = template.New("helperMethodTemplate").Parse(resourceHelperMethodTemplateContent) + //go:embed templates/show_object_method.tmpl + showObjectMethodTemplateContent string + ShowObjectMethodTemplate, _ = template.New("helperMethodTemplate").Parse(showObjectMethodTemplateContent) //go:embed templates/dto_declarations.tmpl dtoDeclarationsTemplateContent string diff --git a/pkg/sdk/poc/generator/templates/resource_helper_method.tmpl b/pkg/sdk/poc/generator/templates/show_object_method.tmpl similarity index 100% rename from pkg/sdk/poc/generator/templates/resource_helper_method.tmpl rename to pkg/sdk/poc/generator/templates/show_object_method.tmpl From 268e2d8590a4659e699c66f75ca75841698daac8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Budzy=C5=84ski?= Date: Wed, 18 Dec 2024 11:31:36 +0100 Subject: [PATCH 28/36] lint --- pkg/sdk/poc/generator/interface.go | 2 -- pkg/sdk/streamlits_def.go | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/pkg/sdk/poc/generator/interface.go b/pkg/sdk/poc/generator/interface.go index 99789e5618..1268b7b1d0 100644 --- a/pkg/sdk/poc/generator/interface.go +++ b/pkg/sdk/poc/generator/interface.go @@ -28,7 +28,5 @@ func (i *Interface) NameLowerCased() string { // ObjectIdentifierKind returns the level of the object identifier (e.g. for DatabaseObjectIdentifier, it returns the prefix "Database") func (i *Interface) ObjectIdentifierPrefix() idPrefix { - // return strings.Replace(i.IdentifierKind, "ObjectIdentifier", "", 1) return identifierStringToPrefix(i.IdentifierKind) } - diff --git a/pkg/sdk/streamlits_def.go b/pkg/sdk/streamlits_def.go index f3a27fc1cb..6ae01153e2 100644 --- a/pkg/sdk/streamlits_def.go +++ b/pkg/sdk/streamlits_def.go @@ -103,7 +103,7 @@ var StreamlitsDef = g.NewInterface( OptionalLike(). OptionalIn(). OptionalLimit(), - g.ShowObjectIdMethod, + g.ShowObjectIdMethod, ).ShowByIdOperationWithFiltering( g.ShowByIDLikeFiltering, g.ShowByIDInFiltering, From c60dec58a0f43da3e9bf4126eb358889ca0c101e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Budzy=C5=84ski?= Date: Wed, 18 Dec 2024 14:13:47 +0100 Subject: [PATCH 29/36] ref --- pkg/sdk/poc/generator/interface.go | 26 +++++++ pkg/sdk/poc/generator/operation.go | 8 +-- pkg/sdk/poc/generator/show_object_methods.go | 68 +++++++------------ .../poc/generator/show_object_methods_test.go | 50 ++++++++++++++ 4 files changed, 103 insertions(+), 49 deletions(-) create mode 100644 pkg/sdk/poc/generator/show_object_methods_test.go diff --git a/pkg/sdk/poc/generator/interface.go b/pkg/sdk/poc/generator/interface.go index 1268b7b1d0..36bdbd9ba9 100644 --- a/pkg/sdk/poc/generator/interface.go +++ b/pkg/sdk/poc/generator/interface.go @@ -1,5 +1,31 @@ package generator +import "fmt" + +type objectIdentifier string + +const ( + AccountObjectIdentifier objectIdentifier = "AccountObjectIdentifier" + DatabaseObjectIdentifier objectIdentifier = "DatabaseObjectIdentifier" + SchemaObjectIdentifier objectIdentifier = "SchemaObjectIdentifier" + SchemaObjectIdentifierWithArguments objectIdentifier = "SchemaObjectIdentifierWithArguments" +) + +func identifierStringToObjectIdentifier(s string) (objectIdentifier, error) { + switch s { + case "AccountObjectIdentifier": + return AccountObjectIdentifier, nil + case "DatabaseObjectIdentifier": + return DatabaseObjectIdentifier, nil + case "SchemaObjectIdentifier": + return SchemaObjectIdentifier, nil + case "SchemaObjectIdentifierWithArguments": + return SchemaObjectIdentifierWithArguments, nil + default: + return "", fmt.Errorf("invalid string identifier type: %s", s) + } +} + // Interface groups operations for particular object or objects family (e.g. DATABASE ROLE) type Interface struct { // Name is the interface's name, e.g. "DatabaseRoles" diff --git a/pkg/sdk/poc/generator/operation.go b/pkg/sdk/poc/generator/operation.go index 870b47db0d..6ce64b5a8f 100644 --- a/pkg/sdk/poc/generator/operation.go +++ b/pkg/sdk/poc/generator/operation.go @@ -124,7 +124,7 @@ func (i *Interface) newOperationWithDBMapping( resourceRepresentation *plainStruct, queryStruct *QueryStruct, addMappingFunc func(op *Operation, from, to *Field), - resourceHelperMethods ...ShowObjectMethodKind, + showObjectMethods ...ShowObjectMethodType, ) *Operation { db := dbRepresentation.IntoField() res := resourceRepresentation.IntoField() @@ -136,7 +136,7 @@ func (i *Interface) newOperationWithDBMapping( withHelperStruct(res). withOptionsStruct(queryStruct.IntoField()). withObjectInterface(i). - withShowObjectMethods(res.Name, resourceHelperMethods...) + withShowObjectMethods(res.Name, showObjectMethods...) addMappingFunc(op, db, res) i.Operations = append(i.Operations, op) @@ -167,8 +167,8 @@ 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 ...ShowObjectMethodKind) *Interface { - i.newOperationWithDBMapping(string(OperationKindShow), doc, dbRepresentation, resourceRepresentation, queryStruct, addShowMapping, helperMethods...) +func (i *Interface) ShowOperation(doc string, dbRepresentation *dbStruct, resourceRepresentation *plainStruct, queryStruct *QueryStruct, showObjectMethods ...ShowObjectMethodType) *Interface { + i.newOperationWithDBMapping(string(OperationKindShow), doc, dbRepresentation, resourceRepresentation, queryStruct, addShowMapping, showObjectMethods...) return i } diff --git a/pkg/sdk/poc/generator/show_object_methods.go b/pkg/sdk/poc/generator/show_object_methods.go index 332b2c84c6..5048f89c83 100644 --- a/pkg/sdk/poc/generator/show_object_methods.go +++ b/pkg/sdk/poc/generator/show_object_methods.go @@ -6,34 +6,10 @@ import ( "slices" ) -type objectIdentifier string +type ShowObjectMethodType uint const ( - AccountObjectIdentifier objectIdentifier = "AccountObjectIdentifier" - DatabaseObjectIdentifier objectIdentifier = "DatabaseObjectIdentifier" - SchemaObjectIdentifier objectIdentifier = "SchemaObjectIdentifier" - SchemaObjectIdentifierWithArguments objectIdentifier = "SchemaObjectIdentifierWithArguments" -) - -func identifierStringToObjectIdentifier(s string) objectIdentifier { - switch s { - case "AccountObjectIdentifier": - return AccountObjectIdentifier - case "DatabaseObjectIdentifier": - return DatabaseObjectIdentifier - case "SchemaObjectIdentifier": - return SchemaObjectIdentifier - case "SchemaObjectIdentifierWithArguments": - return SchemaObjectIdentifierWithArguments - default: - return "" - } -} - -type ShowObjectMethodKind uint - -const ( - ShowObjectIdMethod ShowObjectMethodKind = iota + ShowObjectIdMethod ShowObjectMethodType = iota ShowObjectTypeMethod ) @@ -59,12 +35,15 @@ var idTypeParts map[objectIdentifier][]string = map[objectIdentifier][]string{ SchemaObjectIdentifier: {"DatabaseName", "SchemaName", "Name"}, } -func hasRequiredFieldsForIDMethod(structName string, helperStructs []*Field, requiredFields ...string) bool { - for _, field := range helperStructs { - if field.Name == structName { - return containsFieldNames(field.Fields, requiredFields...) +func hasRequiredFieldsForIDMethod(structName string, helperStructs []*Field, idType objectIdentifier) bool { + if requiredFields, ok := idTypeParts[idType]; 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", idType) return false } @@ -82,11 +61,16 @@ func containsFieldNames(fields []*Field, names ...string) bool { return true } -func (s *Operation) withShowObjectMethods(structName string, showObjectMethodsKind ...ShowObjectMethodKind) *Operation { +func (s *Operation) withShowObjectMethods(structName string, showObjectMethodsKind ...ShowObjectMethodType) *Operation { for _, methodKind := range showObjectMethodsKind { switch methodKind { case ShowObjectIdMethod: - s.ShowObjectMethods = append(s.ShowObjectMethods, newShowObjectIDMethod(structName, s.HelperStructs, s.ObjectInterface.IdentifierKind)) + id, err := identifierStringToObjectIdentifier(s.ObjectInterface.IdentifierKind) + if err != nil { + log.Printf("[WARN]: %v, for showObjectIdMethod", err) + continue + } + s.ShowObjectMethods = append(s.ShowObjectMethods, newShowObjectIDMethod(structName, s.HelperStructs, id)) case ShowObjectTypeMethod: s.ShowObjectMethods = append(s.ShowObjectMethods, newShowObjectTypeMethod(structName)) default: @@ -96,25 +80,19 @@ func (s *Operation) withShowObjectMethods(structName string, showObjectMethodsKi return s } -func newShowObjectIDMethod(structName string, helperStructs []*Field, identifierString string) *ShowObjectMethod { - objectIdentifier := identifierStringToObjectIdentifier(identifierString) - requiredFields, ok := idTypeParts[objectIdentifier] - if !ok { - log.Printf("[WARN]: No required fields mapping defined for identifier %s", objectIdentifier) - return nil - } - if !hasRequiredFieldsForIDMethod(structName, helperStructs, requiredFields...) { - 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 one of required fields: %v.\n", structName, requiredFields) +func newShowObjectIDMethod(structName string, helperStructs []*Field, idType objectIdentifier) *ShowObjectMethod { + if !hasRequiredFieldsForIDMethod(structName, helperStructs, idType) { + 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[idType]) return nil } - + fields := idTypeParts[idType] var args string - for _, field := range requiredFields { + for _, field := range fields { args += fmt.Sprintf("v.%v, ", field) } - returnValue := fmt.Sprintf("New%v(%v)", objectIdentifier, args) - return newShowObjectMethod("ID", structName, returnValue, string(objectIdentifier)) + returnValue := fmt.Sprintf("New%v(%v)", idType, args) + return newShowObjectMethod("ID", structName, returnValue, string(idType)) } func newShowObjectTypeMethod(structName string) *ShowObjectMethod { diff --git a/pkg/sdk/poc/generator/show_object_methods_test.go b/pkg/sdk/poc/generator/show_object_methods_test.go new file mode 100644 index 0000000000..ce7ff731cf --- /dev/null +++ b/pkg/sdk/poc/generator/show_object_methods_test.go @@ -0,0 +1,50 @@ +package generator + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestIdentifierStringToObjectIdentifier(t *testing.T) { + tests := []struct { + input string + expected objectIdentifier + }{ + {"AccountObjectIdentifier", AccountObjectIdentifier}, + {"DatabaseObjectIdentifier", DatabaseObjectIdentifier}, + {"SchemaObjectIdentifier", SchemaObjectIdentifier}, + {"SchemaObjectIdentifierWithArguments", SchemaObjectIdentifierWithArguments}, + } + + for _, test := range tests { + t.Run(test.input, func(t *testing.T) { + result, err := identifierStringToObjectIdentifier(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 := identifierStringToObjectIdentifier(tc.input) + require.ErrorContains(t, err, tc.err) + }) + } +} From df0563b7866a9dfdf8c3b45aa3eaa743b9b53d0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Budzy=C5=84ski?= Date: Wed, 18 Dec 2024 14:50:59 +0100 Subject: [PATCH 30/36] ref --- pkg/sdk/poc/generator/show_object_methods.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/pkg/sdk/poc/generator/show_object_methods.go b/pkg/sdk/poc/generator/show_object_methods.go index 5048f89c83..489f98eb14 100644 --- a/pkg/sdk/poc/generator/show_object_methods.go +++ b/pkg/sdk/poc/generator/show_object_methods.go @@ -70,6 +70,10 @@ func (s *Operation) withShowObjectMethods(structName string, showObjectMethodsKi 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]) + return nil + } s.ShowObjectMethods = append(s.ShowObjectMethods, newShowObjectIDMethod(structName, s.HelperStructs, id)) case ShowObjectTypeMethod: s.ShowObjectMethods = append(s.ShowObjectMethods, newShowObjectTypeMethod(structName)) @@ -81,13 +85,9 @@ func (s *Operation) withShowObjectMethods(structName string, showObjectMethodsKi } func newShowObjectIDMethod(structName string, helperStructs []*Field, idType objectIdentifier) *ShowObjectMethod { - if !hasRequiredFieldsForIDMethod(structName, helperStructs, idType) { - 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[idType]) - return nil - } - fields := idTypeParts[idType] + requiredFields := idTypeParts[idType] var args string - for _, field := range fields { + for _, field := range requiredFields { args += fmt.Sprintf("v.%v, ", field) } From a8ff21d2675d3cb0a37723cb89bbc7398ae2d503 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Budzy=C5=84ski?= Date: Wed, 18 Dec 2024 14:52:12 +0100 Subject: [PATCH 31/36] ref --- pkg/sdk/poc/generator/show_object_methods.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/sdk/poc/generator/show_object_methods.go b/pkg/sdk/poc/generator/show_object_methods.go index 489f98eb14..3bd857142b 100644 --- a/pkg/sdk/poc/generator/show_object_methods.go +++ b/pkg/sdk/poc/generator/show_object_methods.go @@ -72,7 +72,7 @@ func (s *Operation) withShowObjectMethods(structName string, showObjectMethodsKi } 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]) - return nil + continue } s.ShowObjectMethods = append(s.ShowObjectMethods, newShowObjectIDMethod(structName, s.HelperStructs, id)) case ShowObjectTypeMethod: From a9d50cb6fd168a1818d4e2038ff268f7edc1c413 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Budzy=C5=84ski?= Date: Wed, 18 Dec 2024 15:02:17 +0100 Subject: [PATCH 32/36] ref --- pkg/sdk/poc/generator/interface.go | 12 ++-- pkg/sdk/poc/generator/show_object_methods.go | 58 +++++++++---------- .../poc/generator/show_object_methods_test.go | 2 +- 3 files changed, 36 insertions(+), 36 deletions(-) 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}, From 57ebbc34fb17754d7e0e1ab7df9a62ad02ea4dfe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Budzy=C5=84ski?= Date: Wed, 18 Dec 2024 15:08:39 +0100 Subject: [PATCH 33/36] ref --- pkg/sdk/poc/generator/show_object_methods.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pkg/sdk/poc/generator/show_object_methods.go b/pkg/sdk/poc/generator/show_object_methods.go index b34d6eabc9..102f5eb59a 100644 --- a/pkg/sdk/poc/generator/show_object_methods.go +++ b/pkg/sdk/poc/generator/show_object_methods.go @@ -35,18 +35,18 @@ func (s *Operation) withShowObjectMethods(structName string, showObjectMethodsKi case ShowObjectIdMethod: id, err := identifierStringToObjectIdentifier(s.ObjectInterface.IdentifierKind) if err != nil { - log.Printf("[WARN]: %v, for showObjectIdMethod", err) + log.Printf("[WARN] for showObjectIdMethod: %v", 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]) + 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) + log.Println("[WARN] no showObjectMethod found for kind:", methodKind) } } return s @@ -60,7 +60,7 @@ func hasRequiredFieldsForIDMethod(structName string, helperStructs []*Field, idT } } } - log.Printf("[WARN]: No required fields mapping defined for identifier %s", idType) + log.Printf("[WARN] no required fields mapping defined for identifier %s", idType) return false } From 560cc8b84fbd42697bf5d898308ea91743b01918 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Budzy=C5=84ski?= Date: Thu, 19 Dec 2024 11:07:48 +0100 Subject: [PATCH 34/36] small ref --- pkg/sdk/poc/generator/show_object_methods.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/sdk/poc/generator/show_object_methods.go b/pkg/sdk/poc/generator/show_object_methods.go index 102f5eb59a..67e00f026f 100644 --- a/pkg/sdk/poc/generator/show_object_methods.go +++ b/pkg/sdk/poc/generator/show_object_methods.go @@ -42,7 +42,7 @@ func (s *Operation) withShowObjectMethods(structName string, showObjectMethodsKi 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)) + s.ShowObjectMethods = append(s.ShowObjectMethods, newShowObjectIDMethod(structName, id)) case ShowObjectTypeMethod: s.ShowObjectMethods = append(s.ShowObjectMethods, newShowObjectTypeMethod(structName)) default: @@ -84,7 +84,7 @@ func containsFieldNames(fields []*Field, names ...string) bool { return true } -func newShowObjectIDMethod(structName string, helperStructs []*Field, idType objectIdentifierKind) *ShowObjectMethod { +func newShowObjectIDMethod(structName string, idType objectIdentifierKind) *ShowObjectMethod { requiredFields := idTypeParts[idType] var args string for _, field := range requiredFields { From 891ce2b96624475d2f4e9b9727d62d9ed0d9d12a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Budzy=C5=84ski?= Date: Thu, 19 Dec 2024 14:45:04 +0100 Subject: [PATCH 35/36] removed methods from operation --- pkg/sdk/api_integrations_def.go | 1 - pkg/sdk/notification_integrations_def.go | 1 - pkg/sdk/poc/generator/interface.go | 2 +- pkg/sdk/poc/generator/operation.go | 12 ++---- pkg/sdk/poc/generator/show_object_methods.go | 39 ++++--------------- .../poc/generator/show_object_methods_test.go | 4 +- pkg/sdk/poc/generator/template_executors.go | 14 ++++++- .../templates/show_object_id_method.tmpl | 5 +++ .../templates/show_object_method.tmpl | 6 +-- .../templates/show_object_type_method.tmpl | 0 pkg/sdk/streamlits_def.go | 1 - 11 files changed, 33 insertions(+), 52 deletions(-) create mode 100644 pkg/sdk/poc/generator/templates/show_object_id_method.tmpl create mode 100644 pkg/sdk/poc/generator/templates/show_object_type_method.tmpl diff --git a/pkg/sdk/api_integrations_def.go b/pkg/sdk/api_integrations_def.go index baf4ba51e4..83b1c588e9 100644 --- a/pkg/sdk/api_integrations_def.go +++ b/pkg/sdk/api_integrations_def.go @@ -150,7 +150,6 @@ var ApiIntegrationsDef = g.NewInterface( Show(). SQL("API INTEGRATIONS"). OptionalLike(), - g.ShowObjectIdMethod, ). ShowByIdOperationWithFiltering( g.ShowByIDLikeFiltering, diff --git a/pkg/sdk/notification_integrations_def.go b/pkg/sdk/notification_integrations_def.go index 37b9db6cda..23b6a80e1d 100644 --- a/pkg/sdk/notification_integrations_def.go +++ b/pkg/sdk/notification_integrations_def.go @@ -180,7 +180,6 @@ var NotificationIntegrationsDef = g.NewInterface( Show(). SQL("NOTIFICATION INTEGRATIONS"). OptionalLike(), - g.ShowObjectIdMethod, ). ShowByIdOperationWithFiltering( g.ShowByIDLikeFiltering, diff --git a/pkg/sdk/poc/generator/interface.go b/pkg/sdk/poc/generator/interface.go index 22634d99af..faffb3dabf 100644 --- a/pkg/sdk/poc/generator/interface.go +++ b/pkg/sdk/poc/generator/interface.go @@ -11,7 +11,7 @@ const ( SchemaObjectIdentifierWithArguments objectIdentifierKind = "SchemaObjectIdentifierWithArguments" ) -func identifierStringToObjectIdentifier(s string) (objectIdentifierKind, error) { +func toObjectIdentifierKind(s string) (objectIdentifierKind, error) { switch s { case "AccountObjectIdentifier": return AccountObjectIdentifier, nil diff --git a/pkg/sdk/poc/generator/operation.go b/pkg/sdk/poc/generator/operation.go index 6ce64b5a8f..07a7b25641 100644 --- a/pkg/sdk/poc/generator/operation.go +++ b/pkg/sdk/poc/generator/operation.go @@ -40,8 +40,6 @@ type Operation struct { DescribeMapping *Mapping // ShowByIDFiltering defines a kind of filterings performed in ShowByID operation ShowByIDFiltering []ShowByIDFiltering - // ShowObjectMethods contains helper methods for the Interface file (i.e. ID(), ObjectType()) - ShowObjectMethods []*ShowObjectMethod } type Mapping struct { @@ -124,7 +122,6 @@ func (i *Interface) newOperationWithDBMapping( resourceRepresentation *plainStruct, queryStruct *QueryStruct, addMappingFunc func(op *Operation, from, to *Field), - showObjectMethods ...ShowObjectMethodType, ) *Operation { db := dbRepresentation.IntoField() res := resourceRepresentation.IntoField() @@ -134,10 +131,7 @@ func (i *Interface) newOperationWithDBMapping( op := newOperation(kind, doc). withHelperStruct(db). withHelperStruct(res). - withOptionsStruct(queryStruct.IntoField()). - withObjectInterface(i). - withShowObjectMethods(res.Name, showObjectMethods...) - + withOptionsStruct(queryStruct.IntoField()) addMappingFunc(op, db, res) i.Operations = append(i.Operations, op) return op @@ -167,8 +161,8 @@ 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, showObjectMethods ...ShowObjectMethodType) *Interface { - i.newOperationWithDBMapping(string(OperationKindShow), doc, dbRepresentation, resourceRepresentation, queryStruct, addShowMapping, showObjectMethods...) +func (i *Interface) ShowOperation(doc string, dbRepresentation *dbStruct, resourceRepresentation *plainStruct, queryStruct *QueryStruct) *Interface { + i.newOperationWithDBMapping(string(OperationKindShow), doc, dbRepresentation, resourceRepresentation, queryStruct, addShowMapping) return i } diff --git a/pkg/sdk/poc/generator/show_object_methods.go b/pkg/sdk/poc/generator/show_object_methods.go index 67e00f026f..705e02be2f 100644 --- a/pkg/sdk/poc/generator/show_object_methods.go +++ b/pkg/sdk/poc/generator/show_object_methods.go @@ -6,12 +6,6 @@ import ( "slices" ) -type ShowObjectMethodType uint - -const ( - ShowObjectIdMethod ShowObjectMethodType = iota - ShowObjectTypeMethod -) type ShowObjectMethod struct { Name string @@ -20,6 +14,10 @@ type ShowObjectMethod struct { ReturnType string } +type ShowObjectIdMethod struct { + +} + func newShowObjectMethod(name, structName, returnValue string, returnType string) *ShowObjectMethod { return &ShowObjectMethod{ Name: name, @@ -29,38 +27,15 @@ func newShowObjectMethod(name, structName, returnValue string, returnType string } } -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] for showObjectIdMethod: %v", 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, id)) - case ShowObjectTypeMethod: - s.ShowObjectMethods = append(s.ShowObjectMethods, newShowObjectTypeMethod(structName)) - default: - log.Println("[WARN] no showObjectMethod found for kind:", methodKind) - } - } - return s -} - -func hasRequiredFieldsForIDMethod(structName string, helperStructs []*Field, idType objectIdentifierKind) bool { - if requiredFields, ok := idTypeParts[idType]; ok { +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", idType) + log.Printf("[WARN] no required fields mapping defined for identifier %s", idKind) return false } diff --git a/pkg/sdk/poc/generator/show_object_methods_test.go b/pkg/sdk/poc/generator/show_object_methods_test.go index 53d2856f46..806834bdd0 100644 --- a/pkg/sdk/poc/generator/show_object_methods_test.go +++ b/pkg/sdk/poc/generator/show_object_methods_test.go @@ -19,7 +19,7 @@ func TestIdentifierStringToObjectIdentifier(t *testing.T) { for _, test := range tests { t.Run(test.input, func(t *testing.T) { - result, err := identifierStringToObjectIdentifier(test.input) + result, err := toObjectIdentifierKind(test.input) require.NoError(t, err) require.Equal(t, test.expected, result) }) @@ -43,7 +43,7 @@ func TestIdentifierStringToObjectIdentifier_Invalid(t *testing.T) { for _, tc := range tests { t.Run(tc.input, func(t *testing.T) { - _, err := identifierStringToObjectIdentifier(tc.input) + _, err := toObjectIdentifierKind(tc.input) require.ErrorContains(t, err, tc.err) }) } diff --git a/pkg/sdk/poc/generator/template_executors.go b/pkg/sdk/poc/generator/template_executors.go index cbe68fd120..9d46842145 100644 --- a/pkg/sdk/poc/generator/template_executors.go +++ b/pkg/sdk/poc/generator/template_executors.go @@ -21,9 +21,19 @@ func GenerateInterface(writer io.Writer, def *Interface) { if o.OptsField != nil { generateOptionsStruct(writer, o) } - for _, m := range o.ShowObjectMethods { - generateShowObjectMethods(writer, m) + + if o.Name == string(OperationKindShow) { + idKind, err := toObjectIdentifierKind(def.IdentifierKind) + if err != nil { + log.Printf("[WARN] for showObjectIdMethod: %v", err) + } + if checkRequiredFieldsForIDMethod(def.NameSingular, o.HelperStructs, idKind) { + generateShowObjectMethods(writer, newShowObjectIDMethod(def.NameSingular, idKind)) + } + + generateShowObjectMethods(writer, newShowObjectTypeMethod(def.NameSingular)) } + } } diff --git a/pkg/sdk/poc/generator/templates/show_object_id_method.tmpl b/pkg/sdk/poc/generator/templates/show_object_id_method.tmpl new file mode 100644 index 0000000000..9d93f32162 --- /dev/null +++ b/pkg/sdk/poc/generator/templates/show_object_id_method.tmpl @@ -0,0 +1,5 @@ +{{- /*gotype: github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/poc/generator.ShowObjectMethod*/ -}} + +func (v *{{ .StructName }}) ID() {{ .IdentifierKind }} { + return New{{ .IdentifierKind }}({{ range .Args }}v.{{ .Name }}, {{ end }}) +} diff --git a/pkg/sdk/poc/generator/templates/show_object_method.tmpl b/pkg/sdk/poc/generator/templates/show_object_method.tmpl index b573de755f..1447d2e2a2 100644 --- a/pkg/sdk/poc/generator/templates/show_object_method.tmpl +++ b/pkg/sdk/poc/generator/templates/show_object_method.tmpl @@ -1,6 +1,6 @@ -{{- /*gotype: github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/poc/generator.ResourceHelperMethod*/ -}} +{{- /*gotype: github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/poc/generator.ShowObjectMethod*/ -}} -func (v *{{ .StructName }}) {{ .Name }}() {{ .ReturnType }} { - return {{ .ReturnValue }} +func (v *{{ .StructName }}) ObjectType() ObjectType { + return ObjectType{{ .StructName }} } diff --git a/pkg/sdk/poc/generator/templates/show_object_type_method.tmpl b/pkg/sdk/poc/generator/templates/show_object_type_method.tmpl new file mode 100644 index 0000000000..e69de29bb2 diff --git a/pkg/sdk/streamlits_def.go b/pkg/sdk/streamlits_def.go index 6ae01153e2..e7bf14bb13 100644 --- a/pkg/sdk/streamlits_def.go +++ b/pkg/sdk/streamlits_def.go @@ -103,7 +103,6 @@ var StreamlitsDef = g.NewInterface( OptionalLike(). OptionalIn(). OptionalLimit(), - g.ShowObjectIdMethod, ).ShowByIdOperationWithFiltering( g.ShowByIDLikeFiltering, g.ShowByIDInFiltering, From 5f5e26326b475687677b11f7a579267e8b173113 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Budzy=C5=84ski?= Date: Thu, 19 Dec 2024 16:43:45 +0100 Subject: [PATCH 36/36] merge --- pkg/sdk/poc/generator/show_object_methods.go | 53 +++++++------------ pkg/sdk/poc/generator/template_executors.go | 15 +++--- pkg/sdk/poc/generator/templates.go | 8 +++ .../templates/show_object_id_method.tmpl | 4 +- .../templates/show_object_type_method.tmpl | 5 ++ pkg/sdk/secrets_def.go | 2 - 6 files changed, 43 insertions(+), 44 deletions(-) diff --git a/pkg/sdk/poc/generator/show_object_methods.go b/pkg/sdk/poc/generator/show_object_methods.go index 705e02be2f..0b263de958 100644 --- a/pkg/sdk/poc/generator/show_object_methods.go +++ b/pkg/sdk/poc/generator/show_object_methods.go @@ -1,33 +1,31 @@ package generator import ( - "fmt" "log" "slices" ) - -type ShowObjectMethod struct { - Name string - StructName string - ReturnValue string - ReturnType string -} - type ShowObjectIdMethod struct { - + StructName string + IdentifierKind objectIdentifierKind + Args []string } -func newShowObjectMethod(name, structName, returnValue string, returnType string) *ShowObjectMethod { - return &ShowObjectMethod{ - Name: name, - StructName: structName, - ReturnValue: returnValue, - ReturnType: returnType, +func newShowObjectIDMethod(structName string, idType objectIdentifierKind) *ShowObjectIdMethod { + return &ShowObjectIdMethod{ + StructName: structName, + IdentifierKind: idType, + Args: idTypeParts[idType], } } -func checkRequiredFieldsForIDMethod(structName string, helperStructs []*Field, idKind objectIdentifierKind) bool { +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 { @@ -39,12 +37,6 @@ func checkRequiredFieldsForIDMethod(structName string, helperStructs []*Field, i 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 { @@ -59,17 +51,10 @@ func containsFieldNames(fields []*Field, names ...string) bool { return true } -func newShowObjectIDMethod(structName string, idType objectIdentifierKind) *ShowObjectMethod { - requiredFields := idTypeParts[idType] - var args string - for _, field := range requiredFields { - args += fmt.Sprintf("v.%v, ", field) - } - - returnValue := fmt.Sprintf("New%v(%v)", idType, args) - return newShowObjectMethod("ID", structName, returnValue, string(idType)) +type ShowObjectTypeMethod struct { + StructName string } -func newShowObjectTypeMethod(structName string) *ShowObjectMethod { - return newShowObjectMethod("ObjectType", structName, "ObjectType"+structName, "ObjectType") +func newShowObjectTypeMethod(structName string) *ShowObjectTypeMethod { + return &ShowObjectTypeMethod{StructName: structName} } diff --git a/pkg/sdk/poc/generator/template_executors.go b/pkg/sdk/poc/generator/template_executors.go index 9d46842145..d2d5455055 100644 --- a/pkg/sdk/poc/generator/template_executors.go +++ b/pkg/sdk/poc/generator/template_executors.go @@ -27,18 +27,21 @@ func GenerateInterface(writer io.Writer, def *Interface) { if err != nil { log.Printf("[WARN] for showObjectIdMethod: %v", err) } - if checkRequiredFieldsForIDMethod(def.NameSingular, o.HelperStructs, idKind) { - generateShowObjectMethods(writer, newShowObjectIDMethod(def.NameSingular, idKind)) + if checkRequiredFieldsForIdMethod(def.NameSingular, o.HelperStructs, idKind) { + generateShowObjectIdMethod(writer, newShowObjectIDMethod(def.NameSingular, idKind)) } - generateShowObjectMethods(writer, newShowObjectTypeMethod(def.NameSingular)) + generateShowObjectTypeMethod(writer, newShowObjectTypeMethod(def.NameSingular)) } - } } -func generateShowObjectMethods(writer io.Writer, hm *ShowObjectMethod) { - printTo(writer, ShowObjectMethodTemplate, hm) +func generateShowObjectIdMethod(writer io.Writer, m *ShowObjectIdMethod) { + printTo(writer, ShowObjectIdMethodTemplate, m) +} + +func generateShowObjectTypeMethod(writer io.Writer, m *ShowObjectTypeMethod) { + printTo(writer, ShowObjectTypeMethodTemplate, m) } func generateOptionsStruct(writer io.Writer, operation *Operation) { diff --git a/pkg/sdk/poc/generator/templates.go b/pkg/sdk/poc/generator/templates.go index c10b3cc157..7e50da1777 100644 --- a/pkg/sdk/poc/generator/templates.go +++ b/pkg/sdk/poc/generator/templates.go @@ -28,6 +28,14 @@ var ( showObjectMethodTemplateContent string ShowObjectMethodTemplate, _ = template.New("helperMethodTemplate").Parse(showObjectMethodTemplateContent) + //go:embed templates/show_object_id_method.tmpl + showObjectIdMethodTemplateContent string + ShowObjectIdMethodTemplate, _ = template.New("showObjectIdMethodTemplate").Parse(showObjectIdMethodTemplateContent) + + //go:embed templates/show_object_type_method.tmpl + showObjectTypeMethodTemplateContent string + ShowObjectTypeMethodTemplate, _ = template.New("showObjectTypeMethodTemplate").Parse(showObjectTypeMethodTemplateContent) + //go:embed templates/dto_declarations.tmpl dtoDeclarationsTemplateContent string DtoTemplate, _ = template.New("dtoTemplate").Parse(dtoDeclarationsTemplateContent) diff --git a/pkg/sdk/poc/generator/templates/show_object_id_method.tmpl b/pkg/sdk/poc/generator/templates/show_object_id_method.tmpl index 9d93f32162..5c8aedf177 100644 --- a/pkg/sdk/poc/generator/templates/show_object_id_method.tmpl +++ b/pkg/sdk/poc/generator/templates/show_object_id_method.tmpl @@ -1,5 +1,5 @@ -{{- /*gotype: github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/poc/generator.ShowObjectMethod*/ -}} +{{- /*gotype: github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/poc/generator.ShowObjectIdMethod*/ -}} func (v *{{ .StructName }}) ID() {{ .IdentifierKind }} { - return New{{ .IdentifierKind }}({{ range .Args }}v.{{ .Name }}, {{ end }}) + return New{{ .IdentifierKind }}({{ range .Args }}v.{{ . }}, {{ end }}) } diff --git a/pkg/sdk/poc/generator/templates/show_object_type_method.tmpl b/pkg/sdk/poc/generator/templates/show_object_type_method.tmpl index e69de29bb2..3b1f15d353 100644 --- a/pkg/sdk/poc/generator/templates/show_object_type_method.tmpl +++ b/pkg/sdk/poc/generator/templates/show_object_type_method.tmpl @@ -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 }} +} diff --git a/pkg/sdk/secrets_def.go b/pkg/sdk/secrets_def.go index 4c2f0482b1..2598c8f497 100644 --- a/pkg/sdk/secrets_def.go +++ b/pkg/sdk/secrets_def.go @@ -241,8 +241,6 @@ var SecretsDef = g.NewInterface( SQL("SECRETS"). OptionalLike(). OptionalExtendedIn(), - g.ShowObjectIdMethod, - g.ShowObjectTypeMethod, ).ShowByIdOperationWithFiltering( g.ShowByIDLikeFiltering, g.ShowByIDExtendedInFiltering,