From f4d01dd41c2e9afa79f0edc6ef041f13d695cd7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Budzy=C5=84ski?= Date: Mon, 25 Nov 2024 16:55:53 +0100 Subject: [PATCH 01/15] Adding generating ShowByID filtering options from def file --- pkg/sdk/poc/generator/interface.go | 7 +++ pkg/sdk/poc/generator/operation.go | 51 +++++++++++++++++-- .../implementation_functions.tmpl | 18 ++++++- 3 files changed, 70 insertions(+), 6 deletions(-) diff --git a/pkg/sdk/poc/generator/interface.go b/pkg/sdk/poc/generator/interface.go index e5674b1e00..e14e12665b 100644 --- a/pkg/sdk/poc/generator/interface.go +++ b/pkg/sdk/poc/generator/interface.go @@ -1,5 +1,7 @@ package generator +import "strings" + // 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" @@ -25,3 +27,8 @@ func NewInterface(name string, nameSingular string, identifierKind string, opera func (i *Interface) NameLowerCased() string { return startingWithLowerCase(i.Name) } + +// ObjectIdentifierKind returns the level of the object identifier (e.g. for DatabaseObjectIdentifier, it returns "Database") +func (i *Interface) ObjectIdentifierKind() string { + return strings.Replace(i.IdentifierKind, "ObjectIdentifier", "", 1) +} diff --git a/pkg/sdk/poc/generator/operation.go b/pkg/sdk/poc/generator/operation.go index 8c8bd84342..d64ba18004 100644 --- a/pkg/sdk/poc/generator/operation.go +++ b/pkg/sdk/poc/generator/operation.go @@ -20,6 +20,22 @@ const ( DescriptionMappingKindSlice DescriptionMappingKind = "slice" ) +type ShowByIDFiltering struct { + Kind string + Args string +} + +type ShowByIDFilteringKind uint + +const ( + // Enables filtering with: Like + ShowByIDLikeFiltering ShowByIDFilteringKind = iota + // Enables filtering with: In + ShowByIDInFiltering + // Enables filtering with: ExtendedIn + ShowByIDExtendedInFiltering +) + // Operation defines a single operation for given object or objects family (e.g. CREATE DATABASE ROLE) type Operation struct { // Name is the operation's name, e.g. "Create" @@ -38,6 +54,8 @@ type Operation struct { DescribeKind *DescriptionMappingKind // DescribeMapping is a definition of mapping needed by Operation kind of OperationKindDescribe DescribeMapping *Mapping + // ShowByIDFiltering defines a kind of filterings performed in ShowByID operation + ShowByIDFiltering []ShowByIDFiltering } type Mapping struct { @@ -77,6 +95,29 @@ func (s *Operation) withHelperStructs(helperStructs ...*Field) *Operation { return s } +func (s *Operation) withFiltering(filtering ...ShowByIDFilteringKind) *Operation { + for _, f := range filtering { + switch f { + case ShowByIDLikeFiltering: + s.ShowByIDFiltering = append(s.ShowByIDFiltering, ShowByIDFiltering{ + Kind: "Like", + Args: "Like{Pattern: String(id.Name())}", + }) + case ShowByIDInFiltering: + s.ShowByIDFiltering = append(s.ShowByIDFiltering, ShowByIDFiltering{ + Kind: "In", + Args: "&In{%[1]v: id.%[1]vId()}", + }) + case ShowByIDExtendedInFiltering: + s.ShowByIDFiltering = append(s.ShowByIDFiltering, ShowByIDFiltering{ + Kind: "In", + Args: "ExtendedIn{In: In{%[1]v: id.%[1]vId()}}", + }) + } + } + return s +} + func addShowMapping(op *Operation, from, to *Field) { op.ShowMapping = newMapping("convert", from, to) } @@ -85,11 +126,11 @@ func addDescriptionMapping(op *Operation, from, to *Field) { op.DescribeMapping = newMapping("convert", from, to) } -func (i *Interface) newNoSqlOperation(kind string) *Interface { +func (i *Interface) newNoSqlOperation(kind string) *Operation { operation := newOperation(kind, "placeholder"). withOptionsStruct(nil) i.Operations = append(i.Operations, operation) - return i + return operation } func (i *Interface) newSimpleOperation(kind string, doc string, queryStruct *QueryStruct, helperStructs ...IntoField) *Interface { @@ -160,8 +201,10 @@ func (i *Interface) ShowOperation(doc string, dbRepresentation *dbStruct, resour return i } -func (i *Interface) ShowByIdOperation() *Interface { - return i.newNoSqlOperation(string(OperationKindShowByID)) +func (i *Interface) ShowByIdOperation(filtering ...ShowByIDFilteringKind) *Interface { + op := i.newNoSqlOperation(string(OperationKindShowByID)) + op.withFiltering(filtering...) + return i } func (i *Interface) DescribeOperation(describeKind DescriptionMappingKind, doc string, dbRepresentation *dbStruct, resourceRepresentation *plainStruct, queryStruct *QueryStruct) *Interface { diff --git a/pkg/sdk/poc/generator/templates/sub_templates/implementation_functions.tmpl b/pkg/sdk/poc/generator/templates/sub_templates/implementation_functions.tmpl index 5b79463dbb..cd17dc1586 100644 --- a/pkg/sdk/poc/generator/templates/sub_templates/implementation_functions.tmpl +++ b/pkg/sdk/poc/generator/templates/sub_templates/implementation_functions.tmpl @@ -1,6 +1,7 @@ {{- /*gotype: github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/poc/generator.Interface*/ -}} {{ $impl := .NameLowerCased }} + {{ range .Operations }} {{ if and (eq .Name "Show") .ShowMapping }} func (v *{{ $impl }}) Show(ctx context.Context, request *{{ .OptsField.DtoDecl }}) ([]{{ .ShowMapping.To.Name }}, error) { @@ -13,9 +14,17 @@ return resultList, nil } {{ else if eq .Name "ShowByID" }} + {{ $idkind := .ObjectInterface.ObjectIdentifierKind }} func (v *{{ $impl }}) ShowByID(ctx context.Context, id {{ .ObjectInterface.IdentifierKind }}) (*{{ .ObjectInterface.NameSingular }}, error) { - // TODO: adjust request if e.g. LIKE is supported for the resource - {{ $impl }}, err := v.Show(ctx, NewShow{{ .ObjectInterface.NameSingular }}Request()) + request := NewShow{{ .ObjectInterface.NameSingular }}Request() + {{- range .ShowByIDFiltering -}} + .{{- if eq .Kind "In" }} + With{{ .Kind }}( {{ printf .Args $idkind }} ) + {{- else }} + With{{ .Kind }}( {{ .Args }} ) + {{- end }} + {{- end }} + {{ $impl }}, err := v.Show(ctx, request) if err != nil { return nil, err } @@ -54,3 +63,8 @@ } {{ end }} {{ end }} + +{{ define "ShowByIDFiltering" }} + {{- range $k, $v := .ShowByIDFiltering -}} + {{- end }} +{{ end }} From a7366ec85cd3c174531e65b5155905a14e2a91d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Budzy=C5=84ski?= Date: Tue, 26 Nov 2024 15:02:25 +0100 Subject: [PATCH 02/15] Moved show by id filtering to another file --- pkg/sdk/poc/generator/keyword_builders.go | 2 +- pkg/sdk/poc/generator/operation.go | 39 ------------------ pkg/sdk/poc/generator/show_by_id_filtering.go | 40 +++++++++++++++++++ .../implementation_functions.tmpl | 14 ++++--- 4 files changed, 50 insertions(+), 45 deletions(-) create mode 100644 pkg/sdk/poc/generator/show_by_id_filtering.go diff --git a/pkg/sdk/poc/generator/keyword_builders.go b/pkg/sdk/poc/generator/keyword_builders.go index 556c5fbbcc..9945292936 100644 --- a/pkg/sdk/poc/generator/keyword_builders.go +++ b/pkg/sdk/poc/generator/keyword_builders.go @@ -107,7 +107,7 @@ func (v *QueryStruct) OptionalIn() *QueryStruct { } func (v *QueryStruct) OptionalExtendedIn() *QueryStruct { - return v.PredefinedQueryStructField("In", "*ExtendedIn", KeywordOptions().SQL("IN")) + return v.PredefinedQueryStructField("ExtendedIn", "*ExtendedIn", KeywordOptions().SQL("IN")) } func (v *QueryStruct) OptionalStartsWith() *QueryStruct { diff --git a/pkg/sdk/poc/generator/operation.go b/pkg/sdk/poc/generator/operation.go index d64ba18004..0be525ad8e 100644 --- a/pkg/sdk/poc/generator/operation.go +++ b/pkg/sdk/poc/generator/operation.go @@ -20,22 +20,6 @@ const ( DescriptionMappingKindSlice DescriptionMappingKind = "slice" ) -type ShowByIDFiltering struct { - Kind string - Args string -} - -type ShowByIDFilteringKind uint - -const ( - // Enables filtering with: Like - ShowByIDLikeFiltering ShowByIDFilteringKind = iota - // Enables filtering with: In - ShowByIDInFiltering - // Enables filtering with: ExtendedIn - ShowByIDExtendedInFiltering -) - // Operation defines a single operation for given object or objects family (e.g. CREATE DATABASE ROLE) type Operation struct { // Name is the operation's name, e.g. "Create" @@ -95,29 +79,6 @@ func (s *Operation) withHelperStructs(helperStructs ...*Field) *Operation { return s } -func (s *Operation) withFiltering(filtering ...ShowByIDFilteringKind) *Operation { - for _, f := range filtering { - switch f { - case ShowByIDLikeFiltering: - s.ShowByIDFiltering = append(s.ShowByIDFiltering, ShowByIDFiltering{ - Kind: "Like", - Args: "Like{Pattern: String(id.Name())}", - }) - case ShowByIDInFiltering: - s.ShowByIDFiltering = append(s.ShowByIDFiltering, ShowByIDFiltering{ - Kind: "In", - Args: "&In{%[1]v: id.%[1]vId()}", - }) - case ShowByIDExtendedInFiltering: - s.ShowByIDFiltering = append(s.ShowByIDFiltering, ShowByIDFiltering{ - Kind: "In", - Args: "ExtendedIn{In: In{%[1]v: id.%[1]vId()}}", - }) - } - } - return s -} - func addShowMapping(op *Operation, from, to *Field) { op.ShowMapping = newMapping("convert", from, to) } diff --git a/pkg/sdk/poc/generator/show_by_id_filtering.go b/pkg/sdk/poc/generator/show_by_id_filtering.go new file mode 100644 index 0000000000..edd647431d --- /dev/null +++ b/pkg/sdk/poc/generator/show_by_id_filtering.go @@ -0,0 +1,40 @@ +package generator + +type ShowByIDFiltering struct { + Kind string + Args string +} + +type ShowByIDFilteringKind uint + +const ( + // Enables filtering with: Like + ShowByIDLikeFiltering ShowByIDFilteringKind = iota + // Enables filtering with: In + ShowByIDInFiltering + // Enables filtering with: ExtendedIn + ShowByIDExtendedInFiltering +) + +func (s *Operation) withFiltering(filtering ...ShowByIDFilteringKind) *Operation { + for _, f := range filtering { + switch f { + case ShowByIDLikeFiltering: + s.ShowByIDFiltering = append(s.ShowByIDFiltering, ShowByIDFiltering{ + Kind: "Like", + Args: "Pattern: String(id.Name())", + }) + case ShowByIDInFiltering: + s.ShowByIDFiltering = append(s.ShowByIDFiltering, ShowByIDFiltering{ + Kind: "In", + Args: "%[1]v: id.%[1]vId()", + }) + case ShowByIDExtendedInFiltering: + s.ShowByIDFiltering = append(s.ShowByIDFiltering, ShowByIDFiltering{ + Kind: "ExtendedIn", + Args: "In: In{%[1]v: id.%[1]vId()}", + }) + } + } + return s +} diff --git a/pkg/sdk/poc/generator/templates/sub_templates/implementation_functions.tmpl b/pkg/sdk/poc/generator/templates/sub_templates/implementation_functions.tmpl index cd17dc1586..f971c933e0 100644 --- a/pkg/sdk/poc/generator/templates/sub_templates/implementation_functions.tmpl +++ b/pkg/sdk/poc/generator/templates/sub_templates/implementation_functions.tmpl @@ -17,11 +17,15 @@ {{ $idkind := .ObjectInterface.ObjectIdentifierKind }} func (v *{{ $impl }}) ShowByID(ctx context.Context, id {{ .ObjectInterface.IdentifierKind }}) (*{{ .ObjectInterface.NameSingular }}, error) { request := NewShow{{ .ObjectInterface.NameSingular }}Request() - {{- range .ShowByIDFiltering -}} - .{{- if eq .Kind "In" }} - With{{ .Kind }}( {{ printf .Args $idkind }} ) - {{- else }} - With{{ .Kind }}( {{ .Args }} ) + {{- if not .ShowByIDFiltering }} + // TODO: adjust request if e.g. LIKE is supported for the resource + {{ else }} + {{- range .ShowByIDFiltering -}} + .{{- if or (eq .Kind "In") (eq .Kind "ExtendedIn") }} + With{{ .Kind }}( {{ .Kind }} { {{ printf .Args $idkind }} }) + {{- else }} + With{{ .Kind }}( {{ .Kind }} { {{ .Args }} }) + {{- end }} {{- end }} {{- end }} {{ $impl }}, err := v.Show(ctx, request) From 5cb03d1cb031dbc68d3fba6a57e5abb919a4f2f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Budzy=C5=84ski?= Date: Tue, 26 Nov 2024 15:06:20 +0100 Subject: [PATCH 03/15] Remove unused definition in template --- .../templates/sub_templates/implementation_functions.tmpl | 5 ----- 1 file changed, 5 deletions(-) diff --git a/pkg/sdk/poc/generator/templates/sub_templates/implementation_functions.tmpl b/pkg/sdk/poc/generator/templates/sub_templates/implementation_functions.tmpl index f971c933e0..11f71b5907 100644 --- a/pkg/sdk/poc/generator/templates/sub_templates/implementation_functions.tmpl +++ b/pkg/sdk/poc/generator/templates/sub_templates/implementation_functions.tmpl @@ -67,8 +67,3 @@ } {{ end }} {{ end }} - -{{ define "ShowByIDFiltering" }} - {{- range $k, $v := .ShowByIDFiltering -}} - {{- end }} -{{ end }} From 8f2a30143c3e2cac86fb1d29fa8bcaf1af8ed6b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Budzy=C5=84ski?= Date: Tue, 26 Nov 2024 15:50:17 +0100 Subject: [PATCH 04/15] change showByID Filtering to diference types of filtering based on needed Identifier value --- pkg/datasources/secrets.go | 2 +- pkg/sdk/poc/generator/show_by_id_filtering.go | 10 +++++-- .../implementation_functions.tmpl | 2 +- pkg/sdk/secrets_def.go | 26 +++++++++-------- pkg/sdk/secrets_dto_builders_gen.go | 4 +-- pkg/sdk/secrets_dto_gen.go | 4 +-- pkg/sdk/secrets_gen.go | 10 +++---- pkg/sdk/secrets_impl_gen.go | 28 ++++++++++--------- .../testint/secrets_gen_integration_test.go | 24 ++++++++-------- 9 files changed, 60 insertions(+), 50 deletions(-) diff --git a/pkg/datasources/secrets.go b/pkg/datasources/secrets.go index 41101c1ae4..d82141b14c 100644 --- a/pkg/datasources/secrets.go +++ b/pkg/datasources/secrets.go @@ -107,7 +107,7 @@ func ReadSecrets(ctx context.Context, d *schema.ResourceData, meta any) diag.Dia req := sdk.NewShowSecretRequest() handleLike(d, &req.Like) - err := handleExtendedIn(d, &req.In) + err := handleExtendedIn(d, &req.ExtendedIn) if err != nil { return diag.FromErr(err) } diff --git a/pkg/sdk/poc/generator/show_by_id_filtering.go b/pkg/sdk/poc/generator/show_by_id_filtering.go index edd647431d..32558e24c0 100644 --- a/pkg/sdk/poc/generator/show_by_id_filtering.go +++ b/pkg/sdk/poc/generator/show_by_id_filtering.go @@ -1,8 +1,9 @@ package generator type ShowByIDFiltering struct { - Kind string - Args string + Kind string + Args string + IdentifierBased bool } type ShowByIDFilteringKind uint @@ -11,8 +12,10 @@ const ( // Enables filtering with: Like ShowByIDLikeFiltering ShowByIDFilteringKind = iota // Enables filtering with: In + // Based on the identifier Kind ShowByIDInFiltering // Enables filtering with: ExtendedIn + // Based on the identifier Kind ShowByIDExtendedInFiltering ) @@ -23,16 +26,19 @@ func (s *Operation) withFiltering(filtering ...ShowByIDFilteringKind) *Operation s.ShowByIDFiltering = append(s.ShowByIDFiltering, ShowByIDFiltering{ Kind: "Like", Args: "Pattern: String(id.Name())", + IdentifierBased: false, }) case ShowByIDInFiltering: s.ShowByIDFiltering = append(s.ShowByIDFiltering, ShowByIDFiltering{ Kind: "In", Args: "%[1]v: id.%[1]vId()", + IdentifierBased: true, }) case ShowByIDExtendedInFiltering: s.ShowByIDFiltering = append(s.ShowByIDFiltering, ShowByIDFiltering{ Kind: "ExtendedIn", Args: "In: In{%[1]v: id.%[1]vId()}", + IdentifierBased: true, }) } } diff --git a/pkg/sdk/poc/generator/templates/sub_templates/implementation_functions.tmpl b/pkg/sdk/poc/generator/templates/sub_templates/implementation_functions.tmpl index 11f71b5907..a647f71339 100644 --- a/pkg/sdk/poc/generator/templates/sub_templates/implementation_functions.tmpl +++ b/pkg/sdk/poc/generator/templates/sub_templates/implementation_functions.tmpl @@ -21,7 +21,7 @@ // TODO: adjust request if e.g. LIKE is supported for the resource {{ else }} {{- range .ShowByIDFiltering -}} - .{{- if or (eq .Kind "In") (eq .Kind "ExtendedIn") }} + .{{- if .IdentifierBased }} With{{ .Kind }}( {{ .Kind }} { {{ printf .Args $idkind }} }) {{- else }} With{{ .Kind }}( {{ .Kind }} { {{ .Args }} }) diff --git a/pkg/sdk/secrets_def.go b/pkg/sdk/secrets_def.go index 5ac89932a4..24ca5641b3 100644 --- a/pkg/sdk/secrets_def.go +++ b/pkg/sdk/secrets_def.go @@ -241,15 +241,17 @@ var SecretsDef = g.NewInterface( SQL("SECRETS"). OptionalLike(). OptionalExtendedIn(), -).ShowByIdOperation(). - DescribeOperation( - g.DescriptionMappingKindSingleValue, - "https://docs.snowflake.com/en/sql-reference/sql/desc-secret", - secretDetailsDbRow, - secretDetails, - g.NewQueryStruct("DescribeSecret"). - Describe(). - SQL("SECRET"). - Name(). - WithValidation(g.ValidIdentifier, "name"), - ) +).DescribeOperation( + g.DescriptionMappingKindSingleValue, + "https://docs.snowflake.com/en/sql-reference/sql/desc-secret", + secretDetailsDbRow, + secretDetails, + g.NewQueryStruct("DescribeSecret"). + Describe(). + SQL("SECRET"). + Name(). + WithValidation(g.ValidIdentifier, "name"), +).ShowByIdOperation( + g.ShowByIDLikeFiltering, + g.ShowByIDExtendedInFiltering, +) diff --git a/pkg/sdk/secrets_dto_builders_gen.go b/pkg/sdk/secrets_dto_builders_gen.go index 08982900a0..4577ac7edc 100644 --- a/pkg/sdk/secrets_dto_builders_gen.go +++ b/pkg/sdk/secrets_dto_builders_gen.go @@ -261,8 +261,8 @@ func (s *ShowSecretRequest) WithLike(Like Like) *ShowSecretRequest { return s } -func (s *ShowSecretRequest) WithIn(In ExtendedIn) *ShowSecretRequest { - s.In = &In +func (s *ShowSecretRequest) WithExtendedIn(ExtendedIn ExtendedIn) *ShowSecretRequest { + s.ExtendedIn = &ExtendedIn return s } diff --git a/pkg/sdk/secrets_dto_gen.go b/pkg/sdk/secrets_dto_gen.go index dd7ccb7d6a..690ce414d2 100644 --- a/pkg/sdk/secrets_dto_gen.go +++ b/pkg/sdk/secrets_dto_gen.go @@ -100,8 +100,8 @@ type DropSecretRequest struct { } type ShowSecretRequest struct { - Like *Like - In *ExtendedIn + Like *Like + ExtendedIn *ExtendedIn } type DescribeSecretRequest struct { diff --git a/pkg/sdk/secrets_gen.go b/pkg/sdk/secrets_gen.go index 30d467f25e..bc3800cdcd 100644 --- a/pkg/sdk/secrets_gen.go +++ b/pkg/sdk/secrets_gen.go @@ -14,8 +14,8 @@ type Secrets interface { Alter(ctx context.Context, request *AlterSecretRequest) error Drop(ctx context.Context, request *DropSecretRequest) error Show(ctx context.Context, request *ShowSecretRequest) ([]Secret, error) - ShowByID(ctx context.Context, id SchemaObjectIdentifier) (*Secret, error) Describe(ctx context.Context, id SchemaObjectIdentifier) (*SecretDetails, error) + ShowByID(ctx context.Context, id SchemaObjectIdentifier) (*Secret, error) } // CreateWithOAuthClientCredentialsFlowSecretOptions is based on https://docs.snowflake.com/en/sql-reference/sql/create-secret. @@ -123,10 +123,10 @@ type DropSecretOptions struct { // ShowSecretOptions is based on https://docs.snowflake.com/en/sql-reference/sql/show-secrets. type ShowSecretOptions struct { - show bool `ddl:"static" sql:"SHOW"` - secrets bool `ddl:"static" sql:"SECRETS"` - Like *Like `ddl:"keyword" sql:"LIKE"` - In *ExtendedIn `ddl:"keyword" sql:"IN"` + show bool `ddl:"static" sql:"SHOW"` + secrets bool `ddl:"static" sql:"SECRETS"` + Like *Like `ddl:"keyword" sql:"LIKE"` + ExtendedIn *ExtendedIn `ddl:"keyword" sql:"IN"` } type secretDBRow struct { CreatedOn time.Time `db:"created_on"` diff --git a/pkg/sdk/secrets_impl_gen.go b/pkg/sdk/secrets_impl_gen.go index c758ad5aad..540ad10b61 100644 --- a/pkg/sdk/secrets_impl_gen.go +++ b/pkg/sdk/secrets_impl_gen.go @@ -52,15 +52,6 @@ func (v *secrets) Show(ctx context.Context, request *ShowSecretRequest) ([]Secre return resultList, nil } -func (v *secrets) ShowByID(ctx context.Context, id SchemaObjectIdentifier) (*Secret, error) { - request := NewShowSecretRequest().WithIn(ExtendedIn{In: In{Schema: id.SchemaId()}}).WithLike(Like{String(id.Name())}) - secrets, err := v.Show(ctx, request) - if err != nil { - return nil, err - } - return collections.FindFirst(secrets, func(r Secret) bool { return r.Name == id.Name() }) -} - func (v *secrets) Describe(ctx context.Context, id SchemaObjectIdentifier) (*SecretDetails, error) { opts := &DescribeSecretOptions{ name: id, @@ -72,14 +63,24 @@ func (v *secrets) Describe(ctx context.Context, id SchemaObjectIdentifier) (*Sec return result.convert(), nil } +func (v *secrets) ShowByID(ctx context.Context, id SchemaObjectIdentifier) (*Secret, error) { + request := NewShowSecretRequest(). + WithLike(Like{Pattern: String(id.Name())}). + WithExtendedIn(ExtendedIn{In: In{Schema: id.SchemaId()}}) + secrets, err := v.Show(ctx, request) + if err != nil { + return nil, err + } + return collections.FindFirst(secrets, func(r Secret) bool { return r.Name == id.Name() }) +} + func (r *CreateWithOAuthClientCredentialsFlowSecretRequest) toOpts() *CreateWithOAuthClientCredentialsFlowSecretOptions { opts := &CreateWithOAuthClientCredentialsFlowSecretOptions{ OrReplace: r.OrReplace, IfNotExists: r.IfNotExists, name: r.name, ApiIntegration: r.ApiIntegration, - - Comment: r.Comment, + Comment: r.Comment, } if r.OauthScopes != nil { @@ -134,6 +135,7 @@ func (r *AlterSecretRequest) toOpts() *AlterSecretOptions { } if r.Set != nil { + opts.Set = &SecretSet{ Comment: r.Set.Comment, } @@ -192,8 +194,8 @@ func (r *DropSecretRequest) toOpts() *DropSecretOptions { func (r *ShowSecretRequest) toOpts() *ShowSecretOptions { opts := &ShowSecretOptions{ - Like: r.Like, - In: r.In, + Like: r.Like, + ExtendedIn: r.ExtendedIn, } return opts } diff --git a/pkg/sdk/testint/secrets_gen_integration_test.go b/pkg/sdk/testint/secrets_gen_integration_test.go index 26a12edad0..a35a6448c5 100644 --- a/pkg/sdk/testint/secrets_gen_integration_test.go +++ b/pkg/sdk/testint/secrets_gen_integration_test.go @@ -594,15 +594,15 @@ func TestInt_Secrets(t *testing.T) { secret, secretCleanup := testClientHelper().Secret.CreateWithOAuthClientCredentialsFlow(t, id, integrationId, []sdk.ApiIntegrationScope{{Scope: "foo"}}) t.Cleanup(secretCleanup) - returnedSecrets, err := client.Secrets.Show(ctx, sdk.NewShowSecretRequest().WithIn(sdk.ExtendedIn{In: sdk.In{Account: sdk.Pointer(true)}})) + returnedSecrets, err := client.Secrets.Show(ctx, sdk.NewShowSecretRequest().WithExtendedIn(sdk.ExtendedIn{In: sdk.In{Account: sdk.Pointer(true)}})) require.NoError(t, err) require.Contains(t, returnedSecrets, *secret) - returnedSecrets, err = client.Secrets.Show(ctx, sdk.NewShowSecretRequest().WithIn(sdk.ExtendedIn{In: sdk.In{Database: id.DatabaseId()}})) + returnedSecrets, err = client.Secrets.Show(ctx, sdk.NewShowSecretRequest().WithExtendedIn(sdk.ExtendedIn{In: sdk.In{Database: id.DatabaseId()}})) require.NoError(t, err) require.Contains(t, returnedSecrets, *secret) - returnedSecrets, err = client.Secrets.Show(ctx, sdk.NewShowSecretRequest().WithIn(sdk.ExtendedIn{In: sdk.In{Schema: id.SchemaId()}})) + returnedSecrets, err = client.Secrets.Show(ctx, sdk.NewShowSecretRequest().WithExtendedIn(sdk.ExtendedIn{In: sdk.In{Schema: id.SchemaId()}})) require.NoError(t, err) require.Contains(t, returnedSecrets, *secret) }) @@ -612,15 +612,15 @@ func TestInt_Secrets(t *testing.T) { secret, secretCleanup := testClientHelper().Secret.CreateWithOAuthAuthorizationCodeFlow(t, id, integrationId, "foo", refreshTokenExpiryTime) t.Cleanup(secretCleanup) - returnedSecrets, err := client.Secrets.Show(ctx, sdk.NewShowSecretRequest().WithIn(sdk.ExtendedIn{In: sdk.In{Account: sdk.Pointer(true)}})) + returnedSecrets, err := client.Secrets.Show(ctx, sdk.NewShowSecretRequest().WithExtendedIn(sdk.ExtendedIn{In: sdk.In{Account: sdk.Pointer(true)}})) require.NoError(t, err) require.Contains(t, returnedSecrets, *secret) - returnedSecrets, err = client.Secrets.Show(ctx, sdk.NewShowSecretRequest().WithIn(sdk.ExtendedIn{In: sdk.In{Database: id.DatabaseId()}})) + returnedSecrets, err = client.Secrets.Show(ctx, sdk.NewShowSecretRequest().WithExtendedIn(sdk.ExtendedIn{In: sdk.In{Database: id.DatabaseId()}})) require.NoError(t, err) require.Contains(t, returnedSecrets, *secret) - returnedSecrets, err = client.Secrets.Show(ctx, sdk.NewShowSecretRequest().WithIn(sdk.ExtendedIn{In: sdk.In{Schema: id.SchemaId()}})) + returnedSecrets, err = client.Secrets.Show(ctx, sdk.NewShowSecretRequest().WithExtendedIn(sdk.ExtendedIn{In: sdk.In{Schema: id.SchemaId()}})) require.NoError(t, err) require.Contains(t, returnedSecrets, *secret) }) @@ -640,21 +640,21 @@ func TestInt_Secrets(t *testing.T) { secretGenericString, secretCleanupWithGenericString := testClientHelper().Secret.CreateWithGenericString(t, testClientHelper().Ids.RandomSchemaObjectIdentifier(), "foo") t.Cleanup(secretCleanupWithGenericString) - returnedSecrets, err := client.Secrets.Show(ctx, sdk.NewShowSecretRequest().WithIn(sdk.ExtendedIn{In: sdk.In{Account: sdk.Pointer(true)}})) + returnedSecrets, err := client.Secrets.Show(ctx, sdk.NewShowSecretRequest().WithExtendedIn(sdk.ExtendedIn{In: sdk.In{Account: sdk.Pointer(true)}})) require.NoError(t, err) require.Contains(t, returnedSecrets, *secretOAuthClientCredentials) require.Contains(t, returnedSecrets, *secretOAuthAuthorizationCode) require.Contains(t, returnedSecrets, *secretBasicAuthentication) require.Contains(t, returnedSecrets, *secretGenericString) - returnedSecrets, err = client.Secrets.Show(ctx, sdk.NewShowSecretRequest().WithIn(sdk.ExtendedIn{In: sdk.In{Database: id.DatabaseId()}})) + returnedSecrets, err = client.Secrets.Show(ctx, sdk.NewShowSecretRequest().WithExtendedIn(sdk.ExtendedIn{In: sdk.In{Database: id.DatabaseId()}})) require.NoError(t, err) require.Contains(t, returnedSecrets, *secretOAuthClientCredentials) require.Contains(t, returnedSecrets, *secretOAuthAuthorizationCode) require.Contains(t, returnedSecrets, *secretBasicAuthentication) require.Contains(t, returnedSecrets, *secretGenericString) - returnedSecrets, err = client.Secrets.Show(ctx, sdk.NewShowSecretRequest().WithIn(sdk.ExtendedIn{In: sdk.In{Schema: id.SchemaId()}})) + returnedSecrets, err = client.Secrets.Show(ctx, sdk.NewShowSecretRequest().WithExtendedIn(sdk.ExtendedIn{In: sdk.In{Schema: id.SchemaId()}})) require.NoError(t, err) require.Contains(t, returnedSecrets, *secretOAuthClientCredentials) require.Contains(t, returnedSecrets, *secretOAuthAuthorizationCode) @@ -667,15 +667,15 @@ func TestInt_Secrets(t *testing.T) { secret, secretCleanup := testClientHelper().Secret.CreateWithGenericString(t, id, "foo") t.Cleanup(secretCleanup) - returnedSecrets, err := client.Secrets.Show(ctx, sdk.NewShowSecretRequest().WithIn(sdk.ExtendedIn{In: sdk.In{Account: sdk.Pointer(true)}})) + returnedSecrets, err := client.Secrets.Show(ctx, sdk.NewShowSecretRequest().WithExtendedIn(sdk.ExtendedIn{In: sdk.In{Account: sdk.Pointer(true)}})) require.NoError(t, err) require.Contains(t, returnedSecrets, *secret) - returnedSecrets, err = client.Secrets.Show(ctx, sdk.NewShowSecretRequest().WithIn(sdk.ExtendedIn{In: sdk.In{Database: id.DatabaseId()}})) + returnedSecrets, err = client.Secrets.Show(ctx, sdk.NewShowSecretRequest().WithExtendedIn(sdk.ExtendedIn{In: sdk.In{Database: id.DatabaseId()}})) require.NoError(t, err) require.Contains(t, returnedSecrets, *secret) - returnedSecrets, err = client.Secrets.Show(ctx, sdk.NewShowSecretRequest().WithIn(sdk.ExtendedIn{In: sdk.In{Schema: id.SchemaId()}})) + returnedSecrets, err = client.Secrets.Show(ctx, sdk.NewShowSecretRequest().WithExtendedIn(sdk.ExtendedIn{In: sdk.In{Schema: id.SchemaId()}})) require.NoError(t, err) require.Contains(t, returnedSecrets, *secret) }) From 1599afb36a36122395af19213ab1a4562b220e90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Budzy=C5=84ski?= Date: Tue, 26 Nov 2024 15:53:43 +0100 Subject: [PATCH 05/15] linter adjustemnts --- pkg/sdk/poc/generator/show_by_id_filtering.go | 18 +++++++++--------- pkg/sdk/secrets_gen_test.go | 2 +- pkg/sdk/secrets_impl_gen.go | 1 - 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/pkg/sdk/poc/generator/show_by_id_filtering.go b/pkg/sdk/poc/generator/show_by_id_filtering.go index 32558e24c0..7b5ed2ea83 100644 --- a/pkg/sdk/poc/generator/show_by_id_filtering.go +++ b/pkg/sdk/poc/generator/show_by_id_filtering.go @@ -24,21 +24,21 @@ func (s *Operation) withFiltering(filtering ...ShowByIDFilteringKind) *Operation switch f { case ShowByIDLikeFiltering: s.ShowByIDFiltering = append(s.ShowByIDFiltering, ShowByIDFiltering{ - Kind: "Like", - Args: "Pattern: String(id.Name())", - IdentifierBased: false, + Kind: "Like", + Args: "Pattern: String(id.Name())", + IdentifierBased: false, }) case ShowByIDInFiltering: s.ShowByIDFiltering = append(s.ShowByIDFiltering, ShowByIDFiltering{ - Kind: "In", - Args: "%[1]v: id.%[1]vId()", - IdentifierBased: true, + Kind: "In", + Args: "%[1]v: id.%[1]vId()", + IdentifierBased: true, }) case ShowByIDExtendedInFiltering: s.ShowByIDFiltering = append(s.ShowByIDFiltering, ShowByIDFiltering{ - Kind: "ExtendedIn", - Args: "In: In{%[1]v: id.%[1]vId()}", - IdentifierBased: true, + Kind: "ExtendedIn", + Args: "In: In{%[1]v: id.%[1]vId()}", + IdentifierBased: true, }) } } diff --git a/pkg/sdk/secrets_gen_test.go b/pkg/sdk/secrets_gen_test.go index 37cc96726a..808aabad6b 100644 --- a/pkg/sdk/secrets_gen_test.go +++ b/pkg/sdk/secrets_gen_test.go @@ -341,7 +341,7 @@ func TestSecrets_Show(t *testing.T) { t.Run("show with in", func(t *testing.T) { opts := defaultOpts() - opts.In = &ExtendedIn{ + opts.ExtendedIn = &ExtendedIn{ In: In{ Account: Bool(true), }, diff --git a/pkg/sdk/secrets_impl_gen.go b/pkg/sdk/secrets_impl_gen.go index 540ad10b61..c180b0acb9 100644 --- a/pkg/sdk/secrets_impl_gen.go +++ b/pkg/sdk/secrets_impl_gen.go @@ -135,7 +135,6 @@ func (r *AlterSecretRequest) toOpts() *AlterSecretOptions { } if r.Set != nil { - opts.Set = &SecretSet{ Comment: r.Set.Comment, } From dcbae836e144f035d4db6eb503161bc116506e2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Budzy=C5=84ski?= Date: Thu, 28 Nov 2024 11:15:05 +0100 Subject: [PATCH 06/15] add filtering type --- pkg/sdk/poc/generator/show_by_id_filtering.go | 35 ++++++++++++------- .../implementation_functions.tmpl | 6 ++-- 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/pkg/sdk/poc/generator/show_by_id_filtering.go b/pkg/sdk/poc/generator/show_by_id_filtering.go index 7b5ed2ea83..e48ac12a6f 100644 --- a/pkg/sdk/poc/generator/show_by_id_filtering.go +++ b/pkg/sdk/poc/generator/show_by_id_filtering.go @@ -1,9 +1,17 @@ package generator +type ShowByIDFilteringType string + +const ( + SimpleFiltering ShowByIDFilteringType = "Simple" + IdentifierBasedFiltering ShowByIDFilteringType = "IdentifierBased" +) + type ShowByIDFiltering struct { - Kind string - Args string - IdentifierBased bool + Name string + Kind string + Args string + Type ShowByIDFilteringType } type ShowByIDFilteringKind uint @@ -24,21 +32,24 @@ func (s *Operation) withFiltering(filtering ...ShowByIDFilteringKind) *Operation switch f { case ShowByIDLikeFiltering: s.ShowByIDFiltering = append(s.ShowByIDFiltering, ShowByIDFiltering{ - Kind: "Like", - Args: "Pattern: String(id.Name())", - IdentifierBased: false, + Name: "Like", + Kind: "Like", + Args: "Pattern: String(id.Name())", + Type: SimpleFiltering, }) case ShowByIDInFiltering: s.ShowByIDFiltering = append(s.ShowByIDFiltering, ShowByIDFiltering{ - Kind: "In", - Args: "%[1]v: id.%[1]vId()", - IdentifierBased: true, + Name: "In", + Kind: "In", + Args: "%[1]v: id.%[1]vId()", + Type: IdentifierBasedFiltering, }) case ShowByIDExtendedInFiltering: s.ShowByIDFiltering = append(s.ShowByIDFiltering, ShowByIDFiltering{ - Kind: "ExtendedIn", - Args: "In: In{%[1]v: id.%[1]vId()}", - IdentifierBased: true, + Name: "In", + Kind: "ExtendedIn", + Args: "In: In{%[1]v: id.%[1]vId()}", + Type: IdentifierBasedFiltering, }) } } diff --git a/pkg/sdk/poc/generator/templates/sub_templates/implementation_functions.tmpl b/pkg/sdk/poc/generator/templates/sub_templates/implementation_functions.tmpl index a647f71339..298528b7a8 100644 --- a/pkg/sdk/poc/generator/templates/sub_templates/implementation_functions.tmpl +++ b/pkg/sdk/poc/generator/templates/sub_templates/implementation_functions.tmpl @@ -21,10 +21,10 @@ // TODO: adjust request if e.g. LIKE is supported for the resource {{ else }} {{- range .ShowByIDFiltering -}} - .{{- if .IdentifierBased }} - With{{ .Kind }}( {{ .Kind }} { {{ printf .Args $idkind }} }) + .{{- if eq .Type "IdentifierBased" }} + With{{ .Name }}( {{ .Kind }} { {{ printf .Args $idkind }} }) {{- else }} - With{{ .Kind }}( {{ .Kind }} { {{ .Args }} }) + With{{ .Name }}( {{ .Kind }} { {{ .Args }} }) {{- end }} {{- end }} {{- end }} From 151741b5401b433f1978f4d6cb91f475560c1888 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Budzy=C5=84ski?= Date: Thu, 28 Nov 2024 13:32:44 +0100 Subject: [PATCH 07/15] first steps into implementing interface --- pkg/sdk/poc/generator/keyword_builders.go | 2 +- pkg/sdk/poc/generator/operation.go | 1 + pkg/sdk/poc/generator/show_by_id_filtering.go | 31 ++++++++++++------- .../implementation_functions.tmpl | 6 +--- 4 files changed, 23 insertions(+), 17 deletions(-) diff --git a/pkg/sdk/poc/generator/keyword_builders.go b/pkg/sdk/poc/generator/keyword_builders.go index 9945292936..556c5fbbcc 100644 --- a/pkg/sdk/poc/generator/keyword_builders.go +++ b/pkg/sdk/poc/generator/keyword_builders.go @@ -107,7 +107,7 @@ func (v *QueryStruct) OptionalIn() *QueryStruct { } func (v *QueryStruct) OptionalExtendedIn() *QueryStruct { - return v.PredefinedQueryStructField("ExtendedIn", "*ExtendedIn", KeywordOptions().SQL("IN")) + return v.PredefinedQueryStructField("In", "*ExtendedIn", KeywordOptions().SQL("IN")) } func (v *QueryStruct) OptionalStartsWith() *QueryStruct { diff --git a/pkg/sdk/poc/generator/operation.go b/pkg/sdk/poc/generator/operation.go index 0be525ad8e..83826fd3e2 100644 --- a/pkg/sdk/poc/generator/operation.go +++ b/pkg/sdk/poc/generator/operation.go @@ -164,6 +164,7 @@ func (i *Interface) ShowOperation(doc string, dbRepresentation *dbStruct, resour func (i *Interface) ShowByIdOperation(filtering ...ShowByIDFilteringKind) *Interface { op := i.newNoSqlOperation(string(OperationKindShowByID)) + op.ObjectInterface = i op.withFiltering(filtering...) return i } diff --git a/pkg/sdk/poc/generator/show_by_id_filtering.go b/pkg/sdk/poc/generator/show_by_id_filtering.go index e48ac12a6f..861c25d159 100644 --- a/pkg/sdk/poc/generator/show_by_id_filtering.go +++ b/pkg/sdk/poc/generator/show_by_id_filtering.go @@ -1,17 +1,29 @@ package generator -type ShowByIDFilteringType string - -const ( - SimpleFiltering ShowByIDFilteringType = "Simple" - IdentifierBasedFiltering ShowByIDFilteringType = "IdentifierBased" +import ( + "fmt" ) type ShowByIDFiltering struct { Name string Kind string Args string - Type ShowByIDFilteringType +} + +func (s *ShowByIDFiltering) String() string { + return fmt.Sprintf("With%s(%s{%s})", s.Name, s.Kind, s.Args) +} + +type ShowByIDFilter interface { + String() string +} + +func ShowByIDLikeFilteringFunc() ShowByIDFilter { + return &ShowByIDFiltering{ + Name: "Like", + Kind: "Like", + Args: "Pattern: String(id.Name())", + } } type ShowByIDFilteringKind uint @@ -35,21 +47,18 @@ func (s *Operation) withFiltering(filtering ...ShowByIDFilteringKind) *Operation Name: "Like", Kind: "Like", Args: "Pattern: String(id.Name())", - Type: SimpleFiltering, }) case ShowByIDInFiltering: s.ShowByIDFiltering = append(s.ShowByIDFiltering, ShowByIDFiltering{ Name: "In", Kind: "In", - Args: "%[1]v: id.%[1]vId()", - Type: IdentifierBasedFiltering, + Args: fmt.Sprintf("%[1]v: id.%[1]vId()", s.ObjectInterface.ObjectIdentifierKind()), }) case ShowByIDExtendedInFiltering: s.ShowByIDFiltering = append(s.ShowByIDFiltering, ShowByIDFiltering{ Name: "In", Kind: "ExtendedIn", - Args: "In: In{%[1]v: id.%[1]vId()}", - Type: IdentifierBasedFiltering, + Args: fmt.Sprintf("In: In{%[1]v: id.%[1]vId()}", s.ObjectInterface.ObjectIdentifierKind()), }) } } diff --git a/pkg/sdk/poc/generator/templates/sub_templates/implementation_functions.tmpl b/pkg/sdk/poc/generator/templates/sub_templates/implementation_functions.tmpl index 298528b7a8..27b22c4723 100644 --- a/pkg/sdk/poc/generator/templates/sub_templates/implementation_functions.tmpl +++ b/pkg/sdk/poc/generator/templates/sub_templates/implementation_functions.tmpl @@ -20,12 +20,8 @@ {{- if not .ShowByIDFiltering }} // TODO: adjust request if e.g. LIKE is supported for the resource {{ else }} - {{- range .ShowByIDFiltering -}} - .{{- if eq .Type "IdentifierBased" }} - With{{ .Name }}( {{ .Kind }} { {{ printf .Args $idkind }} }) - {{- else }} + {{- range .ShowByIDFiltering }}. With{{ .Name }}( {{ .Kind }} { {{ .Args }} }) - {{- end }} {{- end }} {{- end }} {{ $impl }}, err := v.Show(ctx, request) From 93ab8ff086d8f0ebfeb6019554a146a943aebe01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Budzy=C5=84ski?= Date: Mon, 2 Dec 2024 11:41:58 +0100 Subject: [PATCH 08/15] refactor --- pkg/sdk/poc/generator/show_by_id_filtering.go | 76 ++++++++++--------- .../implementation_functions.tmpl | 4 +- 2 files changed, 44 insertions(+), 36 deletions(-) diff --git a/pkg/sdk/poc/generator/show_by_id_filtering.go b/pkg/sdk/poc/generator/show_by_id_filtering.go index 861c25d159..5e4f1bd067 100644 --- a/pkg/sdk/poc/generator/show_by_id_filtering.go +++ b/pkg/sdk/poc/generator/show_by_id_filtering.go @@ -2,30 +2,23 @@ package generator import ( "fmt" + "log" ) -type ShowByIDFiltering struct { +type ShowByIDFiltering interface { + WithFiltering() string +} + +type ShowByIDFilter struct { Name string Kind string Args string } -func (s *ShowByIDFiltering) String() string { +func (s *ShowByIDFilter) WithFiltering() string { return fmt.Sprintf("With%s(%s{%s})", s.Name, s.Kind, s.Args) } -type ShowByIDFilter interface { - String() string -} - -func ShowByIDLikeFilteringFunc() ShowByIDFilter { - return &ShowByIDFiltering{ - Name: "Like", - Kind: "Like", - Args: "Pattern: String(id.Name())", - } -} - type ShowByIDFilteringKind uint const ( @@ -39,27 +32,42 @@ const ( ShowByIDExtendedInFiltering ) +func NewShowByIDFiltering(name, kind, args string, identifierKind *string) ShowByIDFiltering { + filter := &ShowByIDFilter{ + Name: name, + Kind: kind, + Args: args, + } + if identifierKind != nil { + filter.Args = fmt.Sprintf(args, *identifierKind) + } + return filter +} + +func NewShowByIDLikeFiltering(string) ShowByIDFiltering { + return NewShowByIDFiltering("Like", "Like", "Pattern: String(id.Name())", nil) +} + +func NewShowByIDInFiltering(identifierKind string) ShowByIDFiltering { + return NewShowByIDFiltering("In", "In", "%[1]v: id.%[1]vId()", &identifierKind) +} + +func NewShowByIDExtendedInFiltering(identifierKind string) ShowByIDFiltering { + return NewShowByIDFiltering("In", "ExtendedIn", "In: In{%[1]v: id.%[1]vId()}", &identifierKind) +} + +var filteringMap = map[ShowByIDFilteringKind]func(string) ShowByIDFiltering{ + ShowByIDLikeFiltering: NewShowByIDLikeFiltering, + ShowByIDInFiltering: NewShowByIDInFiltering, + ShowByIDExtendedInFiltering: NewShowByIDExtendedInFiltering, +} + func (s *Operation) withFiltering(filtering ...ShowByIDFilteringKind) *Operation { - for _, f := range filtering { - switch f { - case ShowByIDLikeFiltering: - s.ShowByIDFiltering = append(s.ShowByIDFiltering, ShowByIDFiltering{ - Name: "Like", - Kind: "Like", - Args: "Pattern: String(id.Name())", - }) - case ShowByIDInFiltering: - s.ShowByIDFiltering = append(s.ShowByIDFiltering, ShowByIDFiltering{ - Name: "In", - Kind: "In", - Args: fmt.Sprintf("%[1]v: id.%[1]vId()", s.ObjectInterface.ObjectIdentifierKind()), - }) - case ShowByIDExtendedInFiltering: - s.ShowByIDFiltering = append(s.ShowByIDFiltering, ShowByIDFiltering{ - Name: "In", - Kind: "ExtendedIn", - Args: fmt.Sprintf("In: In{%[1]v: id.%[1]vId()}", s.ObjectInterface.ObjectIdentifierKind()), - }) + for _, filteringKind := range filtering { + if filter, ok := filteringMap[filteringKind]; ok { + s.ShowByIDFiltering = append(s.ShowByIDFiltering, filter(s.ObjectInterface.ObjectIdentifierKind())) + } else { + log.Println("No showByID filtering found for kind:", filteringKind) } } return s diff --git a/pkg/sdk/poc/generator/templates/sub_templates/implementation_functions.tmpl b/pkg/sdk/poc/generator/templates/sub_templates/implementation_functions.tmpl index 27b22c4723..c1dea2b32f 100644 --- a/pkg/sdk/poc/generator/templates/sub_templates/implementation_functions.tmpl +++ b/pkg/sdk/poc/generator/templates/sub_templates/implementation_functions.tmpl @@ -18,10 +18,10 @@ func (v *{{ $impl }}) ShowByID(ctx context.Context, id {{ .ObjectInterface.IdentifierKind }}) (*{{ .ObjectInterface.NameSingular }}, error) { request := NewShow{{ .ObjectInterface.NameSingular }}Request() {{- if not .ShowByIDFiltering }} - // TODO: adjust request if e.g. LIKE is supported for the resource + // TODO: adjust request if e.g. LIKE is supported for the resource with ShowByIDLikeFiltering and other filters for "ShowByIdOperation" in the definition file. {{ else }} {{- range .ShowByIDFiltering }}. - With{{ .Name }}( {{ .Kind }} { {{ .Args }} }) + {{ .WithFiletering }} {{- end }} {{- end }} {{ $impl }}, err := v.Show(ctx, request) From 947902d9acf52be9a27473d009de1d44d3ebf122 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Budzy=C5=84ski?= Date: Mon, 2 Dec 2024 12:46:38 +0100 Subject: [PATCH 09/15] refactor cleanup --- pkg/sdk/poc/generator/operation.go | 2 +- pkg/sdk/poc/generator/show_by_id_filtering.go | 59 +++++++++++-------- .../implementation_functions.tmpl | 4 +- 3 files changed, 36 insertions(+), 29 deletions(-) diff --git a/pkg/sdk/poc/generator/operation.go b/pkg/sdk/poc/generator/operation.go index 83826fd3e2..04a4f61686 100644 --- a/pkg/sdk/poc/generator/operation.go +++ b/pkg/sdk/poc/generator/operation.go @@ -164,7 +164,7 @@ func (i *Interface) ShowOperation(doc string, dbRepresentation *dbStruct, resour func (i *Interface) ShowByIdOperation(filtering ...ShowByIDFilteringKind) *Interface { op := i.newNoSqlOperation(string(OperationKindShowByID)) - op.ObjectInterface = i + op.ObjectInterface = i op.withFiltering(filtering...) return i } diff --git a/pkg/sdk/poc/generator/show_by_id_filtering.go b/pkg/sdk/poc/generator/show_by_id_filtering.go index 5e4f1bd067..390078e60d 100644 --- a/pkg/sdk/poc/generator/show_by_id_filtering.go +++ b/pkg/sdk/poc/generator/show_by_id_filtering.go @@ -5,20 +5,6 @@ import ( "log" ) -type ShowByIDFiltering interface { - WithFiltering() string -} - -type ShowByIDFilter struct { - Name string - Kind string - Args string -} - -func (s *ShowByIDFilter) WithFiltering() string { - return fmt.Sprintf("With%s(%s{%s})", s.Name, s.Kind, s.Args) -} - type ShowByIDFilteringKind uint const ( @@ -30,10 +16,33 @@ const ( // Enables filtering with: ExtendedIn // Based on the identifier Kind ShowByIDExtendedInFiltering + // Enables filtering with: Limit + ShowByIDLimitFiltering ) -func NewShowByIDFiltering(name, kind, args string, identifierKind *string) ShowByIDFiltering { - filter := &ShowByIDFilter{ +type ShowByIDFiltering interface { + WithFiltering() string +} + +type showByIDFilter struct { + Name string + Kind string + Args string +} + +func (s *showByIDFilter) WithFiltering() string { + return fmt.Sprintf("With%s(%s{%s})", s.Name, s.Kind, s.Args) +} + +var filteringMap = map[ShowByIDFilteringKind]func(string) ShowByIDFiltering{ + ShowByIDLikeFiltering: newShowByIDLikeFiltering, + ShowByIDInFiltering: newShowByIDInFiltering, + ShowByIDExtendedInFiltering: newShowByIDExtendedInFiltering, + ShowByIDLimitFiltering: newShowByIDLimitFiltering, +} + +func newShowByIDFiltering(name, kind, args string, identifierKind *string) ShowByIDFiltering { + filter := &showByIDFilter{ Name: name, Kind: kind, Args: args, @@ -44,22 +53,20 @@ func NewShowByIDFiltering(name, kind, args string, identifierKind *string) ShowB return filter } -func NewShowByIDLikeFiltering(string) ShowByIDFiltering { - return NewShowByIDFiltering("Like", "Like", "Pattern: String(id.Name())", nil) +func newShowByIDLikeFiltering(string) ShowByIDFiltering { + return newShowByIDFiltering("Like", "Like", "Pattern: String(id.Name())", nil) } -func NewShowByIDInFiltering(identifierKind string) ShowByIDFiltering { - return NewShowByIDFiltering("In", "In", "%[1]v: id.%[1]vId()", &identifierKind) +func newShowByIDInFiltering(identifierKind string) ShowByIDFiltering { + return newShowByIDFiltering("In", "In", "%[1]v: id.%[1]vId()", &identifierKind) } -func NewShowByIDExtendedInFiltering(identifierKind string) ShowByIDFiltering { - return NewShowByIDFiltering("In", "ExtendedIn", "In: In{%[1]v: id.%[1]vId()}", &identifierKind) +func newShowByIDExtendedInFiltering(identifierKind string) ShowByIDFiltering { + return newShowByIDFiltering("In", "ExtendedIn", "In: In{%[1]v: id.%[1]vId()}", &identifierKind) } -var filteringMap = map[ShowByIDFilteringKind]func(string) ShowByIDFiltering{ - ShowByIDLikeFiltering: NewShowByIDLikeFiltering, - ShowByIDInFiltering: NewShowByIDInFiltering, - ShowByIDExtendedInFiltering: NewShowByIDExtendedInFiltering, +func newShowByIDLimitFiltering(string) ShowByIDFiltering { + return newShowByIDFiltering("Limit", "LimitFrom", "Rows: Int(1)", nil) } func (s *Operation) withFiltering(filtering ...ShowByIDFilteringKind) *Operation { diff --git a/pkg/sdk/poc/generator/templates/sub_templates/implementation_functions.tmpl b/pkg/sdk/poc/generator/templates/sub_templates/implementation_functions.tmpl index c1dea2b32f..da1544fc56 100644 --- a/pkg/sdk/poc/generator/templates/sub_templates/implementation_functions.tmpl +++ b/pkg/sdk/poc/generator/templates/sub_templates/implementation_functions.tmpl @@ -18,10 +18,10 @@ func (v *{{ $impl }}) ShowByID(ctx context.Context, id {{ .ObjectInterface.IdentifierKind }}) (*{{ .ObjectInterface.NameSingular }}, error) { request := NewShow{{ .ObjectInterface.NameSingular }}Request() {{- if not .ShowByIDFiltering }} - // TODO: adjust request if e.g. LIKE is supported for the resource with ShowByIDLikeFiltering and other filters for "ShowByIdOperation" in the definition file. + // TODO: adjust request with resource supported filtering e.g. for LIKE use 'generator.ShowByIDLikeFiltering' in 'ShowByIdOperation()' inside the definition file. {{ else }} {{- range .ShowByIDFiltering }}. - {{ .WithFiletering }} + {{ .WithFiltering }} {{- end }} {{- end }} {{ $impl }}, err := v.Show(ctx, request) From 46338317e73c3293098f0730143ed6f0e30a7d65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Budzy=C5=84ski?= Date: Mon, 2 Dec 2024 13:20:51 +0100 Subject: [PATCH 10/15] test generator run for streamlits --- pkg/sdk/poc/generator/show_by_id_filtering.go | 4 ++-- pkg/sdk/streamlits_def.go | 5 ++++- pkg/sdk/streamlits_impl_gen.go | 6 +++++- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/pkg/sdk/poc/generator/show_by_id_filtering.go b/pkg/sdk/poc/generator/show_by_id_filtering.go index 390078e60d..8afb913f5d 100644 --- a/pkg/sdk/poc/generator/show_by_id_filtering.go +++ b/pkg/sdk/poc/generator/show_by_id_filtering.go @@ -34,7 +34,7 @@ func (s *showByIDFilter) WithFiltering() string { return fmt.Sprintf("With%s(%s{%s})", s.Name, s.Kind, s.Args) } -var filteringMap = map[ShowByIDFilteringKind]func(string) ShowByIDFiltering{ +var filteringMapping = map[ShowByIDFilteringKind]func(string) ShowByIDFiltering{ ShowByIDLikeFiltering: newShowByIDLikeFiltering, ShowByIDInFiltering: newShowByIDInFiltering, ShowByIDExtendedInFiltering: newShowByIDExtendedInFiltering, @@ -71,7 +71,7 @@ func newShowByIDLimitFiltering(string) ShowByIDFiltering { func (s *Operation) withFiltering(filtering ...ShowByIDFilteringKind) *Operation { for _, filteringKind := range filtering { - if filter, ok := filteringMap[filteringKind]; ok { + if filter, ok := filteringMapping[filteringKind]; ok { s.ShowByIDFiltering = append(s.ShowByIDFiltering, filter(s.ObjectInterface.ObjectIdentifierKind())) } else { log.Println("No showByID filtering found for kind:", filteringKind) diff --git a/pkg/sdk/streamlits_def.go b/pkg/sdk/streamlits_def.go index bbe422424a..d19b48b87d 100644 --- a/pkg/sdk/streamlits_def.go +++ b/pkg/sdk/streamlits_def.go @@ -103,7 +103,10 @@ var StreamlitsDef = g.NewInterface( OptionalLike(). OptionalIn(). OptionalLimit(), -).ShowByIdOperation().DescribeOperation( +).ShowByIdOperation( + g.ShowByIDLikeFiltering, + g.ShowByIDInFiltering, +).DescribeOperation( g.DescriptionMappingKindSingleValue, "https://docs.snowflake.com/en/sql-reference/sql/desc-streamlit", g.DbStruct("streamlitsDetailRow"). diff --git a/pkg/sdk/streamlits_impl_gen.go b/pkg/sdk/streamlits_impl_gen.go index 0544bc8cfe..c76c30e310 100644 --- a/pkg/sdk/streamlits_impl_gen.go +++ b/pkg/sdk/streamlits_impl_gen.go @@ -38,7 +38,9 @@ func (v *streamlits) Show(ctx context.Context, request *ShowStreamlitRequest) ([ } func (v *streamlits) ShowByID(ctx context.Context, id SchemaObjectIdentifier) (*Streamlit, error) { - request := NewShowStreamlitRequest().WithIn(In{Schema: id.SchemaId()}).WithLike(Like{String(id.Name())}) + request := NewShowStreamlitRequest(). + WithLike(Like{Pattern: String(id.Name())}). + WithIn(In{Schema: id.SchemaId()}) streamlits, err := v.Show(ctx, request) if err != nil { return nil, err @@ -88,6 +90,7 @@ func (r *AlterStreamlitRequest) toOpts() *AlterStreamlitOptions { } if r.Set != nil { + opts.Set = &StreamlitSet{ RootLocation: r.Set.RootLocation, MainFile: r.Set.MainFile, @@ -102,6 +105,7 @@ func (r *AlterStreamlitRequest) toOpts() *AlterStreamlitOptions { ExternalAccessIntegrations: r.Set.ExternalAccessIntegrations.ExternalAccessIntegrations, } } + } if r.Unset != nil { From aaf81f1f9b74900a639cf369a122edf240e15904 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Budzy=C5=84ski?= Date: Mon, 2 Dec 2024 13:27:19 +0100 Subject: [PATCH 11/15] rephrase the TODO message --- .../templates/sub_templates/implementation_functions.tmpl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/sdk/poc/generator/templates/sub_templates/implementation_functions.tmpl b/pkg/sdk/poc/generator/templates/sub_templates/implementation_functions.tmpl index da1544fc56..661188940c 100644 --- a/pkg/sdk/poc/generator/templates/sub_templates/implementation_functions.tmpl +++ b/pkg/sdk/poc/generator/templates/sub_templates/implementation_functions.tmpl @@ -18,7 +18,7 @@ func (v *{{ $impl }}) ShowByID(ctx context.Context, id {{ .ObjectInterface.IdentifierKind }}) (*{{ .ObjectInterface.NameSingular }}, error) { request := NewShow{{ .ObjectInterface.NameSingular }}Request() {{- if not .ShowByIDFiltering }} - // TODO: adjust request with resource supported filtering e.g. for LIKE use 'generator.ShowByIDLikeFiltering' in 'ShowByIdOperation()' inside the definition file. + // TODO: adjust request with resource supported filtering e.g. for LIKE use 'ShowByIDLikeFiltering' in 'ShowByIdOperation()' inside the definition file. {{ else }} {{- range .ShowByIDFiltering }}. {{ .WithFiltering }} From 637082805361888801b0c3d59ad46a85cbbe634e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Budzy=C5=84ski?= Date: Mon, 2 Dec 2024 13:52:51 +0100 Subject: [PATCH 12/15] regenerate secrets back to In --- pkg/datasources/secrets.go | 2 +- pkg/sdk/poc/generator/show_by_id_filtering.go | 4 ++-- pkg/sdk/secrets_dto_builders_gen.go | 4 ++-- pkg/sdk/secrets_dto_gen.go | 4 ++-- pkg/sdk/secrets_gen.go | 8 +++---- pkg/sdk/secrets_gen_test.go | 2 +- pkg/sdk/secrets_impl_gen.go | 6 ++--- pkg/sdk/streamlits_impl_gen.go | 7 ++---- .../testint/secrets_gen_integration_test.go | 24 +++++++++---------- 9 files changed, 29 insertions(+), 32 deletions(-) diff --git a/pkg/datasources/secrets.go b/pkg/datasources/secrets.go index 8983f3d68d..c40ec7aa6e 100644 --- a/pkg/datasources/secrets.go +++ b/pkg/datasources/secrets.go @@ -109,7 +109,7 @@ func ReadSecrets(ctx context.Context, d *schema.ResourceData, meta any) diag.Dia req := sdk.NewShowSecretRequest() handleLike(d, &req.Like) - err := handleExtendedIn(d, &req.ExtendedIn) + err := handleExtendedIn(d, &req.In) if err != nil { return diag.FromErr(err) } diff --git a/pkg/sdk/poc/generator/show_by_id_filtering.go b/pkg/sdk/poc/generator/show_by_id_filtering.go index 8afb913f5d..341713659c 100644 --- a/pkg/sdk/poc/generator/show_by_id_filtering.go +++ b/pkg/sdk/poc/generator/show_by_id_filtering.go @@ -13,7 +13,7 @@ const ( // Enables filtering with: In // Based on the identifier Kind ShowByIDInFiltering - // Enables filtering with: ExtendedIn + // Enables filtering with: In // Based on the identifier Kind ShowByIDExtendedInFiltering // Enables filtering with: Limit @@ -62,7 +62,7 @@ func newShowByIDInFiltering(identifierKind string) ShowByIDFiltering { } func newShowByIDExtendedInFiltering(identifierKind string) ShowByIDFiltering { - return newShowByIDFiltering("In", "ExtendedIn", "In: In{%[1]v: id.%[1]vId()}", &identifierKind) + return newShowByIDFiltering("In", "In", "In: In{%[1]v: id.%[1]vId()}", &identifierKind) } func newShowByIDLimitFiltering(string) ShowByIDFiltering { diff --git a/pkg/sdk/secrets_dto_builders_gen.go b/pkg/sdk/secrets_dto_builders_gen.go index 4577ac7edc..08982900a0 100644 --- a/pkg/sdk/secrets_dto_builders_gen.go +++ b/pkg/sdk/secrets_dto_builders_gen.go @@ -261,8 +261,8 @@ func (s *ShowSecretRequest) WithLike(Like Like) *ShowSecretRequest { return s } -func (s *ShowSecretRequest) WithExtendedIn(ExtendedIn ExtendedIn) *ShowSecretRequest { - s.ExtendedIn = &ExtendedIn +func (s *ShowSecretRequest) WithIn(In ExtendedIn) *ShowSecretRequest { + s.In = &In return s } diff --git a/pkg/sdk/secrets_dto_gen.go b/pkg/sdk/secrets_dto_gen.go index 690ce414d2..dd7ccb7d6a 100644 --- a/pkg/sdk/secrets_dto_gen.go +++ b/pkg/sdk/secrets_dto_gen.go @@ -100,8 +100,8 @@ type DropSecretRequest struct { } type ShowSecretRequest struct { - Like *Like - ExtendedIn *ExtendedIn + Like *Like + In *ExtendedIn } type DescribeSecretRequest struct { diff --git a/pkg/sdk/secrets_gen.go b/pkg/sdk/secrets_gen.go index bc3800cdcd..fdc6d1859d 100644 --- a/pkg/sdk/secrets_gen.go +++ b/pkg/sdk/secrets_gen.go @@ -123,10 +123,10 @@ type DropSecretOptions struct { // ShowSecretOptions is based on https://docs.snowflake.com/en/sql-reference/sql/show-secrets. type ShowSecretOptions struct { - show bool `ddl:"static" sql:"SHOW"` - secrets bool `ddl:"static" sql:"SECRETS"` - Like *Like `ddl:"keyword" sql:"LIKE"` - ExtendedIn *ExtendedIn `ddl:"keyword" sql:"IN"` + show bool `ddl:"static" sql:"SHOW"` + secrets bool `ddl:"static" sql:"SECRETS"` + Like *Like `ddl:"keyword" sql:"LIKE"` + In *ExtendedIn `ddl:"keyword" sql:"IN"` } type secretDBRow struct { CreatedOn time.Time `db:"created_on"` diff --git a/pkg/sdk/secrets_gen_test.go b/pkg/sdk/secrets_gen_test.go index 808aabad6b..37cc96726a 100644 --- a/pkg/sdk/secrets_gen_test.go +++ b/pkg/sdk/secrets_gen_test.go @@ -341,7 +341,7 @@ func TestSecrets_Show(t *testing.T) { t.Run("show with in", func(t *testing.T) { opts := defaultOpts() - opts.ExtendedIn = &ExtendedIn{ + opts.In = &ExtendedIn{ In: In{ Account: Bool(true), }, diff --git a/pkg/sdk/secrets_impl_gen.go b/pkg/sdk/secrets_impl_gen.go index c180b0acb9..9f2e331944 100644 --- a/pkg/sdk/secrets_impl_gen.go +++ b/pkg/sdk/secrets_impl_gen.go @@ -66,7 +66,7 @@ func (v *secrets) Describe(ctx context.Context, id SchemaObjectIdentifier) (*Sec func (v *secrets) ShowByID(ctx context.Context, id SchemaObjectIdentifier) (*Secret, error) { request := NewShowSecretRequest(). WithLike(Like{Pattern: String(id.Name())}). - WithExtendedIn(ExtendedIn{In: In{Schema: id.SchemaId()}}) + WithIn(ExtendedIn{In: In{Schema: id.SchemaId()}}) secrets, err := v.Show(ctx, request) if err != nil { return nil, err @@ -193,8 +193,8 @@ func (r *DropSecretRequest) toOpts() *DropSecretOptions { func (r *ShowSecretRequest) toOpts() *ShowSecretOptions { opts := &ShowSecretOptions{ - Like: r.Like, - ExtendedIn: r.ExtendedIn, + Like: r.Like, + In: r.In, } return opts } diff --git a/pkg/sdk/streamlits_impl_gen.go b/pkg/sdk/streamlits_impl_gen.go index c76c30e310..ac20497773 100644 --- a/pkg/sdk/streamlits_impl_gen.go +++ b/pkg/sdk/streamlits_impl_gen.go @@ -90,14 +90,12 @@ func (r *AlterStreamlitRequest) toOpts() *AlterStreamlitOptions { } if r.Set != nil { - opts.Set = &StreamlitSet{ RootLocation: r.Set.RootLocation, MainFile: r.Set.MainFile, QueryWarehouse: r.Set.QueryWarehouse, - - Comment: r.Set.Comment, - Title: r.Set.Title, + Comment: r.Set.Comment, + Title: r.Set.Title, } if r.Set.ExternalAccessIntegrations != nil { @@ -105,7 +103,6 @@ func (r *AlterStreamlitRequest) toOpts() *AlterStreamlitOptions { ExternalAccessIntegrations: r.Set.ExternalAccessIntegrations.ExternalAccessIntegrations, } } - } if r.Unset != nil { diff --git a/pkg/sdk/testint/secrets_gen_integration_test.go b/pkg/sdk/testint/secrets_gen_integration_test.go index a35a6448c5..26a12edad0 100644 --- a/pkg/sdk/testint/secrets_gen_integration_test.go +++ b/pkg/sdk/testint/secrets_gen_integration_test.go @@ -594,15 +594,15 @@ func TestInt_Secrets(t *testing.T) { secret, secretCleanup := testClientHelper().Secret.CreateWithOAuthClientCredentialsFlow(t, id, integrationId, []sdk.ApiIntegrationScope{{Scope: "foo"}}) t.Cleanup(secretCleanup) - returnedSecrets, err := client.Secrets.Show(ctx, sdk.NewShowSecretRequest().WithExtendedIn(sdk.ExtendedIn{In: sdk.In{Account: sdk.Pointer(true)}})) + returnedSecrets, err := client.Secrets.Show(ctx, sdk.NewShowSecretRequest().WithIn(sdk.ExtendedIn{In: sdk.In{Account: sdk.Pointer(true)}})) require.NoError(t, err) require.Contains(t, returnedSecrets, *secret) - returnedSecrets, err = client.Secrets.Show(ctx, sdk.NewShowSecretRequest().WithExtendedIn(sdk.ExtendedIn{In: sdk.In{Database: id.DatabaseId()}})) + returnedSecrets, err = client.Secrets.Show(ctx, sdk.NewShowSecretRequest().WithIn(sdk.ExtendedIn{In: sdk.In{Database: id.DatabaseId()}})) require.NoError(t, err) require.Contains(t, returnedSecrets, *secret) - returnedSecrets, err = client.Secrets.Show(ctx, sdk.NewShowSecretRequest().WithExtendedIn(sdk.ExtendedIn{In: sdk.In{Schema: id.SchemaId()}})) + returnedSecrets, err = client.Secrets.Show(ctx, sdk.NewShowSecretRequest().WithIn(sdk.ExtendedIn{In: sdk.In{Schema: id.SchemaId()}})) require.NoError(t, err) require.Contains(t, returnedSecrets, *secret) }) @@ -612,15 +612,15 @@ func TestInt_Secrets(t *testing.T) { secret, secretCleanup := testClientHelper().Secret.CreateWithOAuthAuthorizationCodeFlow(t, id, integrationId, "foo", refreshTokenExpiryTime) t.Cleanup(secretCleanup) - returnedSecrets, err := client.Secrets.Show(ctx, sdk.NewShowSecretRequest().WithExtendedIn(sdk.ExtendedIn{In: sdk.In{Account: sdk.Pointer(true)}})) + returnedSecrets, err := client.Secrets.Show(ctx, sdk.NewShowSecretRequest().WithIn(sdk.ExtendedIn{In: sdk.In{Account: sdk.Pointer(true)}})) require.NoError(t, err) require.Contains(t, returnedSecrets, *secret) - returnedSecrets, err = client.Secrets.Show(ctx, sdk.NewShowSecretRequest().WithExtendedIn(sdk.ExtendedIn{In: sdk.In{Database: id.DatabaseId()}})) + returnedSecrets, err = client.Secrets.Show(ctx, sdk.NewShowSecretRequest().WithIn(sdk.ExtendedIn{In: sdk.In{Database: id.DatabaseId()}})) require.NoError(t, err) require.Contains(t, returnedSecrets, *secret) - returnedSecrets, err = client.Secrets.Show(ctx, sdk.NewShowSecretRequest().WithExtendedIn(sdk.ExtendedIn{In: sdk.In{Schema: id.SchemaId()}})) + returnedSecrets, err = client.Secrets.Show(ctx, sdk.NewShowSecretRequest().WithIn(sdk.ExtendedIn{In: sdk.In{Schema: id.SchemaId()}})) require.NoError(t, err) require.Contains(t, returnedSecrets, *secret) }) @@ -640,21 +640,21 @@ func TestInt_Secrets(t *testing.T) { secretGenericString, secretCleanupWithGenericString := testClientHelper().Secret.CreateWithGenericString(t, testClientHelper().Ids.RandomSchemaObjectIdentifier(), "foo") t.Cleanup(secretCleanupWithGenericString) - returnedSecrets, err := client.Secrets.Show(ctx, sdk.NewShowSecretRequest().WithExtendedIn(sdk.ExtendedIn{In: sdk.In{Account: sdk.Pointer(true)}})) + returnedSecrets, err := client.Secrets.Show(ctx, sdk.NewShowSecretRequest().WithIn(sdk.ExtendedIn{In: sdk.In{Account: sdk.Pointer(true)}})) require.NoError(t, err) require.Contains(t, returnedSecrets, *secretOAuthClientCredentials) require.Contains(t, returnedSecrets, *secretOAuthAuthorizationCode) require.Contains(t, returnedSecrets, *secretBasicAuthentication) require.Contains(t, returnedSecrets, *secretGenericString) - returnedSecrets, err = client.Secrets.Show(ctx, sdk.NewShowSecretRequest().WithExtendedIn(sdk.ExtendedIn{In: sdk.In{Database: id.DatabaseId()}})) + returnedSecrets, err = client.Secrets.Show(ctx, sdk.NewShowSecretRequest().WithIn(sdk.ExtendedIn{In: sdk.In{Database: id.DatabaseId()}})) require.NoError(t, err) require.Contains(t, returnedSecrets, *secretOAuthClientCredentials) require.Contains(t, returnedSecrets, *secretOAuthAuthorizationCode) require.Contains(t, returnedSecrets, *secretBasicAuthentication) require.Contains(t, returnedSecrets, *secretGenericString) - returnedSecrets, err = client.Secrets.Show(ctx, sdk.NewShowSecretRequest().WithExtendedIn(sdk.ExtendedIn{In: sdk.In{Schema: id.SchemaId()}})) + returnedSecrets, err = client.Secrets.Show(ctx, sdk.NewShowSecretRequest().WithIn(sdk.ExtendedIn{In: sdk.In{Schema: id.SchemaId()}})) require.NoError(t, err) require.Contains(t, returnedSecrets, *secretOAuthClientCredentials) require.Contains(t, returnedSecrets, *secretOAuthAuthorizationCode) @@ -667,15 +667,15 @@ func TestInt_Secrets(t *testing.T) { secret, secretCleanup := testClientHelper().Secret.CreateWithGenericString(t, id, "foo") t.Cleanup(secretCleanup) - returnedSecrets, err := client.Secrets.Show(ctx, sdk.NewShowSecretRequest().WithExtendedIn(sdk.ExtendedIn{In: sdk.In{Account: sdk.Pointer(true)}})) + returnedSecrets, err := client.Secrets.Show(ctx, sdk.NewShowSecretRequest().WithIn(sdk.ExtendedIn{In: sdk.In{Account: sdk.Pointer(true)}})) require.NoError(t, err) require.Contains(t, returnedSecrets, *secret) - returnedSecrets, err = client.Secrets.Show(ctx, sdk.NewShowSecretRequest().WithExtendedIn(sdk.ExtendedIn{In: sdk.In{Database: id.DatabaseId()}})) + returnedSecrets, err = client.Secrets.Show(ctx, sdk.NewShowSecretRequest().WithIn(sdk.ExtendedIn{In: sdk.In{Database: id.DatabaseId()}})) require.NoError(t, err) require.Contains(t, returnedSecrets, *secret) - returnedSecrets, err = client.Secrets.Show(ctx, sdk.NewShowSecretRequest().WithExtendedIn(sdk.ExtendedIn{In: sdk.In{Schema: id.SchemaId()}})) + returnedSecrets, err = client.Secrets.Show(ctx, sdk.NewShowSecretRequest().WithIn(sdk.ExtendedIn{In: sdk.In{Schema: id.SchemaId()}})) require.NoError(t, err) require.Contains(t, returnedSecrets, *secret) }) From 29b2ad8a9cc966bf21f42665a5aac0a8c67729f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Budzy=C5=84ski?= Date: Thu, 5 Dec 2024 09:15:46 +0100 Subject: [PATCH 13/15] enum identifierKind --- pkg/sdk/poc/generator/interface.go | 9 +++-- pkg/sdk/poc/generator/show_by_id_filtering.go | 35 +++++++++++++++---- 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/pkg/sdk/poc/generator/interface.go b/pkg/sdk/poc/generator/interface.go index e14e12665b..e611ccecc3 100644 --- a/pkg/sdk/poc/generator/interface.go +++ b/pkg/sdk/poc/generator/interface.go @@ -1,7 +1,5 @@ package generator -import "strings" - // 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" @@ -28,7 +26,8 @@ func (i *Interface) NameLowerCased() string { return startingWithLowerCase(i.Name) } -// ObjectIdentifierKind returns the level of the object identifier (e.g. for DatabaseObjectIdentifier, it returns "Database") -func (i *Interface) ObjectIdentifierKind() string { - return strings.Replace(i.IdentifierKind, "ObjectIdentifier", "", 1) +// ObjectIdentifierKind returns the level of the object identifier (e.g. for DatabaseObjectIdentifier, it returns the prefix "Database") +func (i *Interface) ObjectIdentifierKind() identifierPrefix { + // return strings.Replace(i.IdentifierKind, "ObjectIdentifier", "", 1) + return identifierStringToPrefix(i.IdentifierKind) } diff --git a/pkg/sdk/poc/generator/show_by_id_filtering.go b/pkg/sdk/poc/generator/show_by_id_filtering.go index 341713659c..63c278abf4 100644 --- a/pkg/sdk/poc/generator/show_by_id_filtering.go +++ b/pkg/sdk/poc/generator/show_by_id_filtering.go @@ -20,6 +20,27 @@ const ( ShowByIDLimitFiltering ) +type identifierPrefix string + +const ( + AccountIdentifierPrefix identifierPrefix = "Account" + DatabaseIdentifierPrefix identifierPrefix = "Database" + SchemaIdentifierPrefix identifierPrefix = "Schema" +) + +func identifierStringToPrefix(s string) identifierPrefix { + switch s { + case "AccountObjectIdentifier": + return AccountIdentifierPrefix + case "DatabaseObjectIdentifier": + return DatabaseIdentifierPrefix + case "SchemaObjectIdentifier": + return SchemaIdentifierPrefix + default: + return "" + } +} + type ShowByIDFiltering interface { WithFiltering() string } @@ -34,14 +55,14 @@ func (s *showByIDFilter) WithFiltering() string { return fmt.Sprintf("With%s(%s{%s})", s.Name, s.Kind, s.Args) } -var filteringMapping = map[ShowByIDFilteringKind]func(string) ShowByIDFiltering{ +var filteringMapping = map[ShowByIDFilteringKind]func(identifierPrefix) ShowByIDFiltering{ ShowByIDLikeFiltering: newShowByIDLikeFiltering, ShowByIDInFiltering: newShowByIDInFiltering, ShowByIDExtendedInFiltering: newShowByIDExtendedInFiltering, ShowByIDLimitFiltering: newShowByIDLimitFiltering, } -func newShowByIDFiltering(name, kind, args string, identifierKind *string) ShowByIDFiltering { +func newShowByIDFiltering(name, kind, args string, identifierKind *identifierPrefix) ShowByIDFiltering { filter := &showByIDFilter{ Name: name, Kind: kind, @@ -53,19 +74,19 @@ func newShowByIDFiltering(name, kind, args string, identifierKind *string) ShowB return filter } -func newShowByIDLikeFiltering(string) ShowByIDFiltering { +func newShowByIDLikeFiltering(identifierPrefix) ShowByIDFiltering { return newShowByIDFiltering("Like", "Like", "Pattern: String(id.Name())", nil) } -func newShowByIDInFiltering(identifierKind string) ShowByIDFiltering { +func newShowByIDInFiltering(identifierKind identifierPrefix) ShowByIDFiltering { return newShowByIDFiltering("In", "In", "%[1]v: id.%[1]vId()", &identifierKind) } -func newShowByIDExtendedInFiltering(identifierKind string) ShowByIDFiltering { - return newShowByIDFiltering("In", "In", "In: In{%[1]v: id.%[1]vId()}", &identifierKind) +func newShowByIDExtendedInFiltering(identifierKind identifierPrefix) ShowByIDFiltering { + return newShowByIDFiltering("In", "ExtendedIn", "In: In{%[1]v: id.%[1]vId()}", &identifierKind) } -func newShowByIDLimitFiltering(string) ShowByIDFiltering { +func newShowByIDLimitFiltering(identifierPrefix) ShowByIDFiltering { return newShowByIDFiltering("Limit", "LimitFrom", "Rows: Int(1)", nil) } From b02da545805489da3cdf88158434a8b86766b5ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Budzy=C5=84ski?= Date: Thu, 5 Dec 2024 15:31:02 +0100 Subject: [PATCH 14/15] showbyid generation with examples --- pkg/sdk/application_roles_def.go | 4 +- pkg/sdk/application_roles_impl_gen.go | 3 +- pkg/sdk/connections_def.go | 4 +- pkg/sdk/connections_impl_gen.go | 7 +- pkg/sdk/poc/generator/interface.go | 2 +- pkg/sdk/poc/generator/operation.go | 16 +++- pkg/sdk/poc/generator/show_by_id_filtering.go | 91 +++++++++++-------- .../implementation_functions.tmpl | 9 +- pkg/sdk/secrets_def.go | 6 +- pkg/sdk/secrets_dto_builders_gen.go | 2 - pkg/sdk/secrets_gen.go | 2 +- pkg/sdk/secrets_impl_gen.go | 22 ++--- pkg/sdk/session_policies_def.go | 3 + pkg/sdk/session_policies_impl_gen.go | 4 +- pkg/sdk/streamlits_def.go | 2 +- 15 files changed, 101 insertions(+), 76 deletions(-) diff --git a/pkg/sdk/application_roles_def.go b/pkg/sdk/application_roles_def.go index b26ea5da7d..bb439d1817 100644 --- a/pkg/sdk/application_roles_def.go +++ b/pkg/sdk/application_roles_def.go @@ -60,4 +60,6 @@ var ApplicationRolesDef = g.NewInterface( Identifier("ApplicationName", g.KindOfT[AccountObjectIdentifier](), g.IdentifierOptions()). OptionalLimitFrom(). WithValidation(g.ValidIdentifier, "ApplicationName"), -).ShowByIdOperation() +).ShowByIdOperationWithFiltering( + g.ShowByIDApplicationNameFiltering, +) diff --git a/pkg/sdk/application_roles_impl_gen.go b/pkg/sdk/application_roles_impl_gen.go index a02e940f3d..b61491940a 100644 --- a/pkg/sdk/application_roles_impl_gen.go +++ b/pkg/sdk/application_roles_impl_gen.go @@ -33,7 +33,8 @@ func (v *applicationRoles) Show(ctx context.Context, request *ShowApplicationRol } func (v *applicationRoles) ShowByID(ctx context.Context, id DatabaseObjectIdentifier) (*ApplicationRole, error) { - request := NewShowApplicationRoleRequest().WithApplicationName(id.DatabaseId()) + request := NewShowApplicationRoleRequest(). + WithApplicationName(id.DatabaseId()) applicationRoles, err := v.Show(ctx, request) if err != nil { return nil, err diff --git a/pkg/sdk/connections_def.go b/pkg/sdk/connections_def.go index cad11042c9..d320728cdb 100644 --- a/pkg/sdk/connections_def.go +++ b/pkg/sdk/connections_def.go @@ -105,4 +105,6 @@ var ConnectionDef = g.NewInterface( Show(). SQL("CONNECTIONS"). OptionalLike(), -).ShowByIdOperation() +).ShowByIdOperationWithFiltering( + g.ShowByIDLikeFiltering, +) diff --git a/pkg/sdk/connections_impl_gen.go b/pkg/sdk/connections_impl_gen.go index a1fb403c4c..ae3121870f 100644 --- a/pkg/sdk/connections_impl_gen.go +++ b/pkg/sdk/connections_impl_gen.go @@ -40,10 +40,9 @@ func (v *connections) Show(ctx context.Context, request *ShowConnectionRequest) } func (v *connections) ShowByID(ctx context.Context, id AccountObjectIdentifier) (*Connection, error) { - connections, err := v.Show(ctx, NewShowConnectionRequest().WithLike( - Like{ - Pattern: String(id.Name()), - })) + request := NewShowConnectionRequest(). + WithLike(Like{Pattern: String(id.Name())}) + connections, err := v.Show(ctx, request) if err != nil { return nil, err } diff --git a/pkg/sdk/poc/generator/interface.go b/pkg/sdk/poc/generator/interface.go index e611ccecc3..98c213367e 100644 --- a/pkg/sdk/poc/generator/interface.go +++ b/pkg/sdk/poc/generator/interface.go @@ -27,7 +27,7 @@ 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) ObjectIdentifierKind() identifierPrefix { +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 04a4f61686..c8d0c7e422 100644 --- a/pkg/sdk/poc/generator/operation.go +++ b/pkg/sdk/poc/generator/operation.go @@ -87,10 +87,9 @@ func addDescriptionMapping(op *Operation, from, to *Field) { op.DescribeMapping = newMapping("convert", from, to) } -func (i *Interface) newNoSqlOperation(kind string) *Operation { +func newNoSqlOperation(kind string) *Operation { operation := newOperation(kind, "placeholder"). withOptionsStruct(nil) - i.Operations = append(i.Operations, operation) return operation } @@ -162,10 +161,17 @@ func (i *Interface) ShowOperation(doc string, dbRepresentation *dbStruct, resour return i } -func (i *Interface) ShowByIdOperation(filtering ...ShowByIDFilteringKind) *Interface { - op := i.newNoSqlOperation(string(OperationKindShowByID)) +func (i *Interface) ShowByIdOperation() *Interface { + op := newNoSqlOperation(string(OperationKindShowByID)) + i.Operations = append(i.Operations, op) + return i +} + +func (i *Interface) ShowByIdOperationWithFiltering(filter ShowByIDFilteringKind, filtering ...ShowByIDFilteringKind) *Interface { + op := newNoSqlOperation(string(OperationKindShowByID)) op.ObjectInterface = i - op.withFiltering(filtering...) + op.withFiltering(append([]ShowByIDFilteringKind{filter}, filtering...)...) + i.Operations = append(i.Operations, op) return i } diff --git a/pkg/sdk/poc/generator/show_by_id_filtering.go b/pkg/sdk/poc/generator/show_by_id_filtering.go index 63c278abf4..6e5f91a56d 100644 --- a/pkg/sdk/poc/generator/show_by_id_filtering.go +++ b/pkg/sdk/poc/generator/show_by_id_filtering.go @@ -8,27 +8,22 @@ import ( type ShowByIDFilteringKind uint const ( - // Enables filtering with: Like ShowByIDLikeFiltering ShowByIDFilteringKind = iota - // Enables filtering with: In - // Based on the identifier Kind ShowByIDInFiltering - // Enables filtering with: In - // Based on the identifier Kind ShowByIDExtendedInFiltering - // Enables filtering with: Limit - ShowByIDLimitFiltering + ShowByIDApplicationNameFiltering + ShowByIDNoFiltering ) -type identifierPrefix string +type idPrefix string const ( - AccountIdentifierPrefix identifierPrefix = "Account" - DatabaseIdentifierPrefix identifierPrefix = "Database" - SchemaIdentifierPrefix identifierPrefix = "Schema" + AccountIdentifierPrefix idPrefix = "Account" + DatabaseIdentifierPrefix idPrefix = "Database" + SchemaIdentifierPrefix idPrefix = "Schema" ) -func identifierStringToPrefix(s string) identifierPrefix { +func identifierStringToPrefix(s string) idPrefix { switch s { case "AccountObjectIdentifier": return AccountIdentifierPrefix @@ -55,46 +50,70 @@ func (s *showByIDFilter) WithFiltering() string { return fmt.Sprintf("With%s(%s{%s})", s.Name, s.Kind, s.Args) } -var filteringMapping = map[ShowByIDFilteringKind]func(identifierPrefix) ShowByIDFiltering{ - ShowByIDLikeFiltering: newShowByIDLikeFiltering, - ShowByIDInFiltering: newShowByIDInFiltering, - ShowByIDExtendedInFiltering: newShowByIDExtendedInFiltering, - ShowByIDLimitFiltering: newShowByIDLimitFiltering, -} - -func newShowByIDFiltering(name, kind, args string, identifierKind *identifierPrefix) ShowByIDFiltering { - filter := &showByIDFilter{ +func newShowByIDFiltering(name, kind, args string) ShowByIDFiltering { + return &showByIDFilter{ Name: name, Kind: kind, Args: args, } - if identifierKind != nil { - filter.Args = fmt.Sprintf(args, *identifierKind) - } - return filter } -func newShowByIDLikeFiltering(identifierPrefix) ShowByIDFiltering { - return newShowByIDFiltering("Like", "Like", "Pattern: String(id.Name())", nil) +func newShowByIDLikeFiltering() ShowByIDFiltering { + return newShowByIDFiltering("Like", "Like", "Pattern: String(id.Name())") +} + +func newShowByIDInFiltering(identifierKind idPrefix) ShowByIDFiltering { + return newShowByIDFiltering("In", "In", fmt.Sprintf("%[1]v: id.%[1]vId()", identifierKind)) +} + +func newShowByIDExtendedInFiltering(identifierKind idPrefix) ShowByIDFiltering { + return newShowByIDFiltering("In", "ExtendedIn", fmt.Sprintf("In: In{%[1]v: id.%[1]vId()}", identifierKind)) } -func newShowByIDInFiltering(identifierKind identifierPrefix) ShowByIDFiltering { - return newShowByIDFiltering("In", "In", "%[1]v: id.%[1]vId()", &identifierKind) +// ApplicationName filtering for application_roles +type showByIDApplicationFilter struct { + showByIDFilter } -func newShowByIDExtendedInFiltering(identifierKind identifierPrefix) ShowByIDFiltering { - return newShowByIDFiltering("In", "ExtendedIn", "In: In{%[1]v: id.%[1]vId()}", &identifierKind) +func (s *showByIDApplicationFilter) WithFiltering() string { + return fmt.Sprintf("With%s(%s)", s.Name, s.Args) +} + +func newShowByIDApplicationFiltering() ShowByIDFiltering { + return &showByIDApplicationFilter{ + showByIDFilter: showByIDFilter{ + Name: "ApplicationName", + Kind: "", + Args: "id.DatabaseId()", + }, + } +} + +// noop for NoFiltering +type showByIDNoFilter struct{} + +func (s *showByIDNoFilter) WithFiltering() string { + return "" } -func newShowByIDLimitFiltering(identifierPrefix) ShowByIDFiltering { - return newShowByIDFiltering("Limit", "LimitFrom", "Rows: Int(1)", nil) +func newShowByIDNoFiltering() ShowByIDFiltering { + return &showByIDNoFilter{} } func (s *Operation) withFiltering(filtering ...ShowByIDFilteringKind) *Operation { for _, filteringKind := range filtering { - if filter, ok := filteringMapping[filteringKind]; ok { - s.ShowByIDFiltering = append(s.ShowByIDFiltering, filter(s.ObjectInterface.ObjectIdentifierKind())) - } else { + switch filteringKind { + case ShowByIDInFiltering: + s.ShowByIDFiltering = append(s.ShowByIDFiltering, newShowByIDInFiltering(s.ObjectInterface.ObjectIdentifierPrefix())) + case ShowByIDExtendedInFiltering: + s.ShowByIDFiltering = append(s.ShowByIDFiltering, newShowByIDExtendedInFiltering(s.ObjectInterface.ObjectIdentifierPrefix())) + case ShowByIDLikeFiltering: + s.ShowByIDFiltering = append(s.ShowByIDFiltering, newShowByIDLikeFiltering()) + case ShowByIDApplicationNameFiltering: + s.ShowByIDFiltering = append(s.ShowByIDFiltering, newShowByIDApplicationFiltering()) + case ShowByIDNoFiltering: + s.ShowByIDFiltering = []ShowByIDFiltering{newShowByIDNoFiltering()} + default: log.Println("No showByID filtering found for kind:", filteringKind) } } diff --git a/pkg/sdk/poc/generator/templates/sub_templates/implementation_functions.tmpl b/pkg/sdk/poc/generator/templates/sub_templates/implementation_functions.tmpl index 661188940c..dd5e562780 100644 --- a/pkg/sdk/poc/generator/templates/sub_templates/implementation_functions.tmpl +++ b/pkg/sdk/poc/generator/templates/sub_templates/implementation_functions.tmpl @@ -14,15 +14,10 @@ return resultList, nil } {{ else if eq .Name "ShowByID" }} - {{ $idkind := .ObjectInterface.ObjectIdentifierKind }} func (v *{{ $impl }}) ShowByID(ctx context.Context, id {{ .ObjectInterface.IdentifierKind }}) (*{{ .ObjectInterface.NameSingular }}, error) { request := NewShow{{ .ObjectInterface.NameSingular }}Request() - {{- if not .ShowByIDFiltering }} - // TODO: adjust request with resource supported filtering e.g. for LIKE use 'ShowByIDLikeFiltering' in 'ShowByIdOperation()' inside the definition file. - {{ else }} - {{- range .ShowByIDFiltering }}. - {{ .WithFiltering }} - {{- end }} + {{- range .ShowByIDFiltering }} + {{- .WithFiltering -}} {{- end }} {{ $impl }}, err := v.Show(ctx, request) if err != nil { diff --git a/pkg/sdk/secrets_def.go b/pkg/sdk/secrets_def.go index 24ca5641b3..2598c8f497 100644 --- a/pkg/sdk/secrets_def.go +++ b/pkg/sdk/secrets_def.go @@ -241,6 +241,9 @@ var SecretsDef = g.NewInterface( SQL("SECRETS"). OptionalLike(). OptionalExtendedIn(), +).ShowByIdOperationWithFiltering( + g.ShowByIDLikeFiltering, + g.ShowByIDExtendedInFiltering, ).DescribeOperation( g.DescriptionMappingKindSingleValue, "https://docs.snowflake.com/en/sql-reference/sql/desc-secret", @@ -251,7 +254,4 @@ var SecretsDef = g.NewInterface( SQL("SECRET"). Name(). WithValidation(g.ValidIdentifier, "name"), -).ShowByIdOperation( - g.ShowByIDLikeFiltering, - g.ShowByIDExtendedInFiltering, ) diff --git a/pkg/sdk/secrets_dto_builders_gen.go b/pkg/sdk/secrets_dto_builders_gen.go index 08982900a0..1d80cc5bfd 100644 --- a/pkg/sdk/secrets_dto_builders_gen.go +++ b/pkg/sdk/secrets_dto_builders_gen.go @@ -2,8 +2,6 @@ package sdk -import () - func NewCreateWithOAuthClientCredentialsFlowSecretRequest( name SchemaObjectIdentifier, ApiIntegration AccountObjectIdentifier, diff --git a/pkg/sdk/secrets_gen.go b/pkg/sdk/secrets_gen.go index fdc6d1859d..30d467f25e 100644 --- a/pkg/sdk/secrets_gen.go +++ b/pkg/sdk/secrets_gen.go @@ -14,8 +14,8 @@ type Secrets interface { Alter(ctx context.Context, request *AlterSecretRequest) error Drop(ctx context.Context, request *DropSecretRequest) error Show(ctx context.Context, request *ShowSecretRequest) ([]Secret, error) - Describe(ctx context.Context, id SchemaObjectIdentifier) (*SecretDetails, error) ShowByID(ctx context.Context, id SchemaObjectIdentifier) (*Secret, error) + Describe(ctx context.Context, id SchemaObjectIdentifier) (*SecretDetails, error) } // CreateWithOAuthClientCredentialsFlowSecretOptions is based on https://docs.snowflake.com/en/sql-reference/sql/create-secret. diff --git a/pkg/sdk/secrets_impl_gen.go b/pkg/sdk/secrets_impl_gen.go index 9f2e331944..8e65e3463a 100644 --- a/pkg/sdk/secrets_impl_gen.go +++ b/pkg/sdk/secrets_impl_gen.go @@ -52,17 +52,6 @@ func (v *secrets) Show(ctx context.Context, request *ShowSecretRequest) ([]Secre return resultList, nil } -func (v *secrets) Describe(ctx context.Context, id SchemaObjectIdentifier) (*SecretDetails, error) { - opts := &DescribeSecretOptions{ - name: id, - } - result, err := validateAndQueryOne[secretDetailsDBRow](v.client, ctx, opts) - if err != nil { - return nil, err - } - return result.convert(), nil -} - func (v *secrets) ShowByID(ctx context.Context, id SchemaObjectIdentifier) (*Secret, error) { request := NewShowSecretRequest(). WithLike(Like{Pattern: String(id.Name())}). @@ -74,6 +63,17 @@ func (v *secrets) ShowByID(ctx context.Context, id SchemaObjectIdentifier) (*Sec return collections.FindFirst(secrets, func(r Secret) bool { return r.Name == id.Name() }) } +func (v *secrets) Describe(ctx context.Context, id SchemaObjectIdentifier) (*SecretDetails, error) { + opts := &DescribeSecretOptions{ + name: id, + } + result, err := validateAndQueryOne[secretDetailsDBRow](v.client, ctx, opts) + if err != nil { + return nil, err + } + return result.convert(), nil +} + func (r *CreateWithOAuthClientCredentialsFlowSecretRequest) toOpts() *CreateWithOAuthClientCredentialsFlowSecretOptions { opts := &CreateWithOAuthClientCredentialsFlowSecretOptions{ OrReplace: r.OrReplace, diff --git a/pkg/sdk/session_policies_def.go b/pkg/sdk/session_policies_def.go index 65f8870cf4..3b172f5562 100644 --- a/pkg/sdk/session_policies_def.go +++ b/pkg/sdk/session_policies_def.go @@ -89,6 +89,9 @@ var SessionPoliciesDef = g.NewInterface( Show(). SQL("SESSION POLICIES"), ). + ShowByIdOperationWithFiltering( + g.ShowByIDNoFiltering, + ). DescribeOperation( g.DescriptionMappingKindSingleValue, "https://docs.snowflake.com/en/sql-reference/sql/desc-session-policy", diff --git a/pkg/sdk/session_policies_impl_gen.go b/pkg/sdk/session_policies_impl_gen.go index 93a0293b93..c06b9adc3b 100644 --- a/pkg/sdk/session_policies_impl_gen.go +++ b/pkg/sdk/session_policies_impl_gen.go @@ -38,11 +38,11 @@ func (v *sessionPolicies) Show(ctx context.Context, request *ShowSessionPolicyRe } func (v *sessionPolicies) ShowByID(ctx context.Context, id SchemaObjectIdentifier) (*SessionPolicy, error) { - sessionPolicies, err := v.Show(ctx, NewShowSessionPolicyRequest()) + request := NewShowSessionPolicyRequest() + sessionPolicies, err := v.Show(ctx, request) if err != nil { return nil, err } - return collections.FindFirst(sessionPolicies, func(r SessionPolicy) bool { return r.Name == id.Name() }) } diff --git a/pkg/sdk/streamlits_def.go b/pkg/sdk/streamlits_def.go index d19b48b87d..e7bf14bb13 100644 --- a/pkg/sdk/streamlits_def.go +++ b/pkg/sdk/streamlits_def.go @@ -103,7 +103,7 @@ var StreamlitsDef = g.NewInterface( OptionalLike(). OptionalIn(). OptionalLimit(), -).ShowByIdOperation( +).ShowByIdOperationWithFiltering( g.ShowByIDLikeFiltering, g.ShowByIDInFiltering, ).DescribeOperation( From 9cd65c3b480da5f80e0fae4fa39cfd921aeff55d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Budzy=C5=84ski?= Date: Thu, 5 Dec 2024 15:37:09 +0100 Subject: [PATCH 15/15] showbyid fix for generating nofiltering --- pkg/sdk/poc/generator/show_by_id_filtering.go | 16 ++++------------ .../sub_templates/implementation_functions.tmpl | 4 +++- 2 files changed, 7 insertions(+), 13 deletions(-) diff --git a/pkg/sdk/poc/generator/show_by_id_filtering.go b/pkg/sdk/poc/generator/show_by_id_filtering.go index 6e5f91a56d..c80ec3aef3 100644 --- a/pkg/sdk/poc/generator/show_by_id_filtering.go +++ b/pkg/sdk/poc/generator/show_by_id_filtering.go @@ -58,6 +58,10 @@ func newShowByIDFiltering(name, kind, args string) ShowByIDFiltering { } } +func newShowByIDNoFiltering() ShowByIDFiltering { + return newShowByIDFiltering("NoFiltering", "", "") +} + func newShowByIDLikeFiltering() ShowByIDFiltering { return newShowByIDFiltering("Like", "Like", "Pattern: String(id.Name())") } @@ -70,7 +74,6 @@ func newShowByIDExtendedInFiltering(identifierKind idPrefix) ShowByIDFiltering { return newShowByIDFiltering("In", "ExtendedIn", fmt.Sprintf("In: In{%[1]v: id.%[1]vId()}", identifierKind)) } -// ApplicationName filtering for application_roles type showByIDApplicationFilter struct { showByIDFilter } @@ -89,17 +92,6 @@ func newShowByIDApplicationFiltering() ShowByIDFiltering { } } -// noop for NoFiltering -type showByIDNoFilter struct{} - -func (s *showByIDNoFilter) WithFiltering() string { - return "" -} - -func newShowByIDNoFiltering() ShowByIDFiltering { - return &showByIDNoFilter{} -} - func (s *Operation) withFiltering(filtering ...ShowByIDFilteringKind) *Operation { for _, filteringKind := range filtering { switch filteringKind { diff --git a/pkg/sdk/poc/generator/templates/sub_templates/implementation_functions.tmpl b/pkg/sdk/poc/generator/templates/sub_templates/implementation_functions.tmpl index dd5e562780..60b0cc7e0d 100644 --- a/pkg/sdk/poc/generator/templates/sub_templates/implementation_functions.tmpl +++ b/pkg/sdk/poc/generator/templates/sub_templates/implementation_functions.tmpl @@ -17,7 +17,9 @@ func (v *{{ $impl }}) ShowByID(ctx context.Context, id {{ .ObjectInterface.IdentifierKind }}) (*{{ .ObjectInterface.NameSingular }}, error) { request := NewShow{{ .ObjectInterface.NameSingular }}Request() {{- range .ShowByIDFiltering }} - {{- .WithFiltering -}} + {{- if not (eq .Name "NoFiltering") -}}. + {{ .WithFiltering }} + {{- end }} {{- end }} {{ $impl }}, err := v.Show(ctx, request) if err != nil {