-
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 9 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 |
---|---|---|
@@ -1,10 +1,9 @@ | ||
package generator | ||
|
||
type ShowByIDFiltering struct { | ||
Kind string | ||
Args string | ||
IdentifierBased bool | ||
} | ||
import ( | ||
"fmt" | ||
"log" | ||
) | ||
|
||
type ShowByIDFilteringKind uint | ||
|
||
|
@@ -14,32 +13,68 @@ 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 | ||
sfc-gh-jmichalak marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Enables filtering with: Limit | ||
ShowByIDLimitFiltering | ||
) | ||
|
||
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 filteringMapping = map[ShowByIDFilteringKind]func(string) ShowByIDFiltering{ | ||
ShowByIDLikeFiltering: newShowByIDLikeFiltering, | ||
ShowByIDInFiltering: newShowByIDInFiltering, | ||
ShowByIDExtendedInFiltering: newShowByIDExtendedInFiltering, | ||
ShowByIDLimitFiltering: newShowByIDLimitFiltering, | ||
} | ||
|
||
func newShowByIDFiltering(name, kind, args string, identifierKind *string) ShowByIDFiltering { | ||
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. How about using a variadic function? 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. will be done by formatting arguments in the create function (as discussed verbally). This way the identifier kind is removed from |
||
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 { | ||
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. Can we have an enum for |
||
return newShowByIDFiltering("In", "In", "In: In{%[1]v: id.%[1]vId()}", &identifierKind) | ||
} | ||
|
||
func newShowByIDLimitFiltering(string) ShowByIDFiltering { | ||
return newShowByIDFiltering("Limit", "LimitFrom", "Rows: Int(1)", nil) | ||
} | ||
|
||
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())", | ||
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, | ||
}) | ||
for _, filteringKind := range filtering { | ||
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) | ||
} | ||
} | ||
return s | ||
|
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.
The
operation.ObjectInterface
is being set here, because without itoperation.ObjectInterface.ObjectIdentifierKind()
can't be used before executing the template. It results in nil pointer dereference, same as forInterface.NameLowerCased()
.I wanted to use it before the template execution to invoke the filtering just by calling
{{ .WithFiltering }}
in the template.I'm open for discussion on this one.
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.
I'm fine with assigning it in the constructor (
newNoSqlOperation
in this case), it's not bad and imo could be added to other constructors that areInterface
methods