Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Make ShowByID configurable in generator #2822

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions pkg/sdk/poc/generator/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package generator

type Config struct {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Config is very generic, maybe something like ShowByIdConfig would be better. Oh ok, I see it's made to be more generic, but as I mentioned this could be replaced most likely with enum value on Operation struct level like in the case of Describe's different "modes". wdyt?

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
}
16 changes: 13 additions & 3 deletions pkg/sdk/poc/generator/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions pkg/sdk/poc/generator/plain_struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not used anywhere (or I missed something), do you think it's needed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used it earlier, forgot to remove.

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 {
Expand Down
13 changes: 12 additions & 1 deletion pkg/sdk/poc/generator/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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
}
Expand Down
6 changes: 5 additions & 1 deletion pkg/sdk/views_def.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,11 @@ var ViewsDef = g.NewInterface(
OptionalStartsWith().
OptionalLimit(),
).
ShowByIdOperation().
ShowByIdOperation(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like an additional layer of abstraction would be better. I think receiving an enum for common presets would be sufficient (like in the case of DescribeOperation), e.g.

// enum names could be better, but that's what I imagined it like
ShowByIdOperation(g.ShowByIdLikeFiltering)
ShowByIdOperation(g.ShowByIdInDatabaseWithLikeFiltering)
ShowByIdOperation(g.ShowByIdInSchemaWithLikeFiltering)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is exactly what I wanted to point out. I would even say that it would be possible to base the In choice on the identifier type (because for schema objects with search in schema, for db objects we search in db, etc.). But the one suggested by @sfc-gh-jcieslak should be enough.

*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",
Expand Down
4 changes: 2 additions & 2 deletions pkg/sdk/views_impl_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Loading