-
Notifications
You must be signed in to change notification settings - Fork 426
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: Add ShowByID filtering generation #3227
Changes from 18 commits
f4d01dd
a7366ec
bc0fb66
5cb03d1
8f2a301
1599afb
ebeffac
dcbae83
151741b
93ab8ff
947902d
4633831
04bc3ce
aaf81f1
6370828
29b2ad8
b02da54
9cd65c3
9d51aa9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,6 +38,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 { | ||
|
@@ -85,11 +87,10 @@ func addDescriptionMapping(op *Operation, from, to *Field) { | |
op.DescribeMapping = newMapping("convert", from, to) | ||
} | ||
|
||
func (i *Interface) newNoSqlOperation(kind string) *Interface { | ||
func 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 { | ||
|
@@ -161,7 +162,17 @@ func (i *Interface) ShowOperation(doc string, dbRepresentation *dbStruct, resour | |
} | ||
|
||
func (i *Interface) ShowByIdOperation() *Interface { | ||
return i.newNoSqlOperation(string(OperationKindShowByID)) | ||
op := newNoSqlOperation(string(OperationKindShowByID)) | ||
i.Operations = append(i.Operations, op) | ||
return i | ||
} | ||
|
||
func (i *Interface) ShowByIdOperationWithFiltering(filter ShowByIDFilteringKind, filtering ...ShowByIDFilteringKind) *Interface { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. New Operation forcing users to use filtering options (as discussed with @sfc-gh-asawicki) |
||
op := newNoSqlOperation(string(OperationKindShowByID)) | ||
op.ObjectInterface = i | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The I wanted to use it before the template execution to invoke the filtering just by calling I'm open for discussion on this one. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm fine with assigning it in the constructor ( |
||
op.withFiltering(append([]ShowByIDFilteringKind{filter}, filtering...)...) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: you can do it a little bit shorter with |
||
i.Operations = append(i.Operations, op) | ||
return i | ||
} | ||
|
||
func (i *Interface) DescribeOperation(describeKind DescriptionMappingKind, doc string, dbRepresentation *dbStruct, resourceRepresentation *plainStruct, queryStruct *QueryStruct) *Interface { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package generator | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
) | ||
|
||
type ShowByIDFilteringKind uint | ||
|
||
const ( | ||
ShowByIDLikeFiltering ShowByIDFilteringKind = iota | ||
ShowByIDInFiltering | ||
ShowByIDExtendedInFiltering | ||
sfc-gh-jmichalak marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ShowByIDApplicationNameFiltering | ||
ShowByIDNoFiltering | ||
) | ||
|
||
type idPrefix string | ||
|
||
const ( | ||
AccountIdentifierPrefix idPrefix = "Account" | ||
DatabaseIdentifierPrefix idPrefix = "Database" | ||
SchemaIdentifierPrefix idPrefix = "Schema" | ||
) | ||
|
||
func identifierStringToPrefix(s string) idPrefix { | ||
switch s { | ||
case "AccountObjectIdentifier": | ||
return AccountIdentifierPrefix | ||
case "DatabaseObjectIdentifier": | ||
return DatabaseIdentifierPrefix | ||
case "SchemaObjectIdentifier": | ||
return SchemaIdentifierPrefix | ||
default: | ||
return "" | ||
} | ||
} | ||
|
||
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) | ||
} | ||
|
||
func newShowByIDFiltering(name, kind, args string) ShowByIDFiltering { | ||
return &showByIDFilter{ | ||
Name: name, | ||
Kind: kind, | ||
Args: args, | ||
} | ||
} | ||
|
||
func newShowByIDNoFiltering() ShowByIDFiltering { | ||
return newShowByIDFiltering("NoFiltering", "", "") | ||
} | ||
|
||
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)) | ||
} | ||
|
||
type showByIDApplicationFilter struct { | ||
showByIDFilter | ||
} | ||
|
||
func (s *showByIDApplicationFilter) WithFiltering() string { | ||
return fmt.Sprintf("With%s(%s)", s.Name, s.Args) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
|
||
func newShowByIDApplicationFiltering() ShowByIDFiltering { | ||
return &showByIDApplicationFilter{ | ||
showByIDFilter: showByIDFilter{ | ||
Name: "ApplicationName", | ||
Kind: "", | ||
Args: "id.DatabaseId()", | ||
}, | ||
} | ||
} | ||
|
||
func (s *Operation) withFiltering(filtering ...ShowByIDFilteringKind) *Operation { | ||
for _, filteringKind := range filtering { | ||
switch filteringKind { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Discussed with @sfc-gh-jmichalak to reject the usage of map[ShowByIDFilteringKind]func(idPrefix) ShowByIDFiltering and use a switch instead, so that there is no need to append more arguments to each function if new filtering is added |
||
case ShowByIDInFiltering: | ||
sfc-gh-jmichalak marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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) | ||
} | ||
} | ||
return s | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) { | ||
|
@@ -14,8 +15,13 @@ | |
} | ||
{{ else if eq .Name "ShowByID" }} | ||
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 not (eq .Name "NoFiltering") -}}. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. mind the dot at the end here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wouldn't it be easier if the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1, I think having empty slice and removing this |
||
{{ .WithFiltering }} | ||
{{- end }} | ||
{{- end }} | ||
{{ $impl }}, err := v.Show(ctx, request) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove if it doesn't serve any purpose
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is used to fill the "In" filtering with ObjectIdentifier prefixes based on the object identifier type e.g. DatabaseObjectIdentifier etc. So that the filtering is defined once, and is applicable for any ObejctIdentifier type.