From 258dda270a3385ded96d9e26e628abf1116f0711 Mon Sep 17 00:00:00 2001 From: Jakub Michalak Date: Fri, 17 May 2024 17:15:48 +0200 Subject: [PATCH] Make ShowByID configurable in generator --- pkg/sdk/poc/generator/config.go | 26 ++++++++++++++++++++++++++ pkg/sdk/poc/generator/operation.go | 16 +++++++++++++--- pkg/sdk/poc/generator/plain_struct.go | 4 ++++ pkg/sdk/poc/generator/templates.go | 13 ++++++++++++- pkg/sdk/views_def.go | 6 +++++- pkg/sdk/views_impl_gen.go | 4 ++-- 6 files changed, 62 insertions(+), 7 deletions(-) create mode 100644 pkg/sdk/poc/generator/config.go diff --git a/pkg/sdk/poc/generator/config.go b/pkg/sdk/poc/generator/config.go new file mode 100644 index 0000000000..109af0befc --- /dev/null +++ b/pkg/sdk/poc/generator/config.go @@ -0,0 +1,26 @@ +package generator + +type Config struct { + Values map[string]any +} + +func NewConfig() *Config { + return &Config{ + Values: make(map[string]any), + } +} + +type Pattern struct { + Field string + Pattern string +} + +func (c *Config) WithLike(patterns []Pattern) *Config { + c.Values["Like"] = patterns + return c +} + +func (c *Config) WithIn(patterns []Pattern) *Config { + c.Values["In"] = patterns + return c +} diff --git a/pkg/sdk/poc/generator/operation.go b/pkg/sdk/poc/generator/operation.go index c62f99712a..5f2f93f1ec 100644 --- a/pkg/sdk/poc/generator/operation.go +++ b/pkg/sdk/poc/generator/operation.go @@ -38,6 +38,8 @@ type Operation struct { DescribeKind *DescriptionMappingKind // DescribeMapping is a definition of mapping needed by Operation kind of OperationKindDescribe DescribeMapping *Mapping + // Config is used to configure an operation + Config *Config } type Mapping struct { @@ -77,6 +79,11 @@ func (s *Operation) withHelperStructs(helperStructs ...*Field) *Operation { return s } +func (s *Operation) withConfig(config *Config) *Operation { + s.Config = config + return s +} + func addShowMapping(op *Operation, from, to *Field) { op.ShowMapping = newMapping("convert", from, to) } @@ -85,9 +92,12 @@ 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, config ...Config) *Interface { operation := newOperation(kind, "placeholder"). withOptionsStruct(nil) + if len(config) > 0 { + operation = operation.withConfig(&config[0]) + } i.Operations = append(i.Operations, operation) return i } @@ -160,8 +170,8 @@ 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(config ...Config) *Interface { + return i.newNoSqlOperation(string(OperationKindShowByID), config...) } func (i *Interface) DescribeOperation(describeKind DescriptionMappingKind, doc string, dbRepresentation *dbStruct, resourceRepresentation *plainStruct, queryStruct *QueryStruct) *Interface { diff --git a/pkg/sdk/poc/generator/plain_struct.go b/pkg/sdk/poc/generator/plain_struct.go index facb90f81a..adf03ae4d7 100644 --- a/pkg/sdk/poc/generator/plain_struct.go +++ b/pkg/sdk/poc/generator/plain_struct.go @@ -53,6 +53,10 @@ func (v *plainStruct) OptionalNumber(dbName string) *plainStruct { return v.Field(dbName, "*int") } +func (v *plainStruct) PlainStructField(name string, plainStruct *plainStruct) *plainStruct { + return v.Field(name, plainStruct.IntoField().Kind) +} + func (v *plainStruct) IntoField() *Field { f := NewField(v.name, v.name, nil, nil) for _, field := range v.fields { diff --git a/pkg/sdk/poc/generator/templates.go b/pkg/sdk/poc/generator/templates.go index 3f3d2957a2..ee7d0f25a6 100644 --- a/pkg/sdk/poc/generator/templates.go +++ b/pkg/sdk/poc/generator/templates.go @@ -115,6 +115,11 @@ var ImplementationTemplate, _ = template.New("implementationTemplate"). return &{{ .To.KindNoPtr }}{} } {{ end }} +{{ define "SHOWBYID_REQ" }} + {{- range $k, $v := .Values -}} + .With{{$k}}(&{{$k}}{ {{range $v}} {{.Field}}: {{.Pattern}}, {{end}}}) + {{- end}} +{{ end }} import ( "context" @@ -140,8 +145,14 @@ type {{ $impl }} struct { } {{ else if eq .Name "ShowByID" }} func (v *{{ $impl }}) ShowByID(ctx context.Context, id {{ .ObjectInterface.IdentifierKind }}) (*{{ .ObjectInterface.NameSingular }}, error) { + req := NewShow{{ .ObjectInterface.NameSingular }}Request( + ) + {{- if not .Config -}} // TODO: adjust request if e.g. LIKE is supported for the resource - {{ $impl }}, err := v.Show(ctx, NewShow{{ .ObjectInterface.NameSingular }}Request()) + {{- else -}} + {{ template "SHOWBYID_REQ" .Config }} + {{- end -}} + {{ $impl }}, err := v.Show(ctx, req) if err != nil { return nil, err } diff --git a/pkg/sdk/views_def.go b/pkg/sdk/views_def.go index 85db9d7b34..e4e2206761 100644 --- a/pkg/sdk/views_def.go +++ b/pkg/sdk/views_def.go @@ -201,7 +201,11 @@ var ViewsDef = g.NewInterface( OptionalStartsWith(). OptionalLimit(), ). - ShowByIdOperation(). + ShowByIdOperation( + *g.NewConfig(). + WithIn([]g.Pattern{{Field: "Schema", Pattern: "NewDatabaseObjectIdentifier(id.DatabaseName(), id.SchemaName())"}}). + WithLike([]g.Pattern{{Field: "Pattern", Pattern: "String(id.Name())"}}), + ). DescribeOperation( g.DescriptionMappingKindSlice, "https://docs.snowflake.com/en/sql-reference/sql/desc-view", diff --git a/pkg/sdk/views_impl_gen.go b/pkg/sdk/views_impl_gen.go index 4acd186b9b..3996cfe146 100644 --- a/pkg/sdk/views_impl_gen.go +++ b/pkg/sdk/views_impl_gen.go @@ -38,8 +38,8 @@ func (v *views) Show(ctx context.Context, request *ShowViewRequest) ([]View, err } func (v *views) ShowByID(ctx context.Context, id SchemaObjectIdentifier) (*View, error) { - request := NewShowViewRequest().WithIn(&In{Schema: NewDatabaseObjectIdentifier(id.DatabaseName(), id.SchemaName())}).WithLike(&Like{String(id.Name())}) - views, err := v.Show(ctx, request) + req := NewShowViewRequest().WithIn(&In{Schema: NewDatabaseObjectIdentifier(id.DatabaseName(), id.SchemaName())}).WithLike(&Like{Pattern: String(id.Name())}) + views, err := v.Show(ctx, req) if err != nil { return nil, err }