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
Autogenerated FindBy #92
Conversation
f0d1c31
to
13630ff
Compare
affected by #93 |
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.
sorry for the amount of requested changes, most of them are small stuff, naming of things and requesting tests
generator/findby.go
Outdated
) | ||
|
||
const ( | ||
tplFindByInclude = "\n\n// FindBy adds a new filter to the query that will require that\n" + |
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.
we should use the ` for multiline strings instead of concatenated strings, it would be easier to read imho
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.
much better
generator/types.go
Outdated
@@ -106,7 +106,7 @@ var reservedKeywords = map[string]struct{}{ | |||
|
|||
// special types that are not analyzed because SQL already knows | |||
// how to handle them | |||
var specialTypes = map[string]string{ | |||
var kallaxKnownTypes = map[string]string{ |
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.
why the rename of this?
generator/findby.go
Outdated
return s | ||
} | ||
|
||
func isEqualizable(f *Field) bool { |
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.
some docs for these 3 functions would be nice for context
generator/findby.go
Outdated
// string, byte, bool, float(s), int(s) or uint(s) | ||
func isBasicType(pkg *types.Package, typ types.Type) bool { | ||
s := typ.String() | ||
return s == "string" || s == "bool" || s == "byte" || s == "float64" || s == "float32" || |
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.
what do you think about converting this to a switch, which would be cleaner?
switch s {
case "string", "bool", "byte", "float64", ...:
return true
}
return false
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.
tricky... but ok :)
generator/findby.go
Outdated
return cleanName(typ, true) | ||
} | ||
|
||
func cleanName(typ types.Type, requireShortName bool) string { |
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.
this is already implemented https://github.com/src-d/go-kallax/blob/master/generator/types.go#L697
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.
this needs tests
generator/findby.go
Outdated
// validType returns the short name of the type that can be used to search for | ||
// the passed Field, in an autogenerated 'FindBy'; | ||
// if the Field is not valid for an autogenerated 'FindBy' it will return an error | ||
func validType(f *Field) (string, error) { |
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.
what do you think about naming this findableTypeName
?
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.
this should have tests
generator/findby.go
Outdated
|
||
// GenFindBy generates FindByPropertyName for all model properties that are | ||
// valid types or collection of valid types. | ||
func (td *TemplateData) GenFindBy(model *Model) string { |
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.
this should have tests
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.
good idea ;)
generator/findby.go
Outdated
return ok || isSQLType(pkg, typ) | ||
} | ||
|
||
func kallaxKnownTypeName(typ types.Type) (string, bool) { |
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.
if the name of kallaxKnownTypes
changes, this should change too
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.
did't changed :)
generator/findby.go
Outdated
|
||
// isSpecialType returns true if the passed type is one of the kallaxKnownTypes | ||
// or a types.SQLType | ||
func isSpecialType(pkg *types.Package, typ types.Type) bool { |
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.
if kallaxKnownTypes
changes, this should change too
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.
did't changed :)
tests/query_test.go
Outdated
|
||
func (s *QuerySuite) TestGeneration() { | ||
q := NewQueryFixtureQuery() | ||
s.hasFindByMethod(q, "ID") |
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.
how about a trable-driven tests?
var cases = []struct{
name string
generated bool
}{
{"ID", true},
// ...
}
for _, c := range cases {
if c.generated {
s.hasFindByMethod(q, c.name)
} else {
s.hasNotFindByMethod(q, c.name)
}
}
that way it's easier to add more cases and there is less repeated code
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.
when tests are implemented lgtm
generator/findby.go
Outdated
if special, ok := specialTypeShortName(typ); ok { | ||
return special | ||
} | ||
|
||
return cleanName(typ, true) | ||
} | ||
|
||
//TODO: review existent implementation generator.types::typeString |
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.
space after //
Btw, a lot of tests are failing |
@erizocosmico many thanks for your recommendations!!!
|
8eb97f9
to
8396838
Compare
affected by #96 |
Codecov Report
@@ Coverage Diff @@
## master #92 +/- ##
==========================================
+ Coverage 80.1% 80.51% +0.41%
==========================================
Files 15 15
Lines 2568 2659 +91
==========================================
+ Hits 2057 2141 +84
- Misses 348 354 +6
- Partials 163 164 +1
Continue to review full report at Codecov.
|
Is this ready for review again? @dpordomingo |
I forgot pushing it; Pending, and will be done after your validation, in order to make it easier the sequential review of the PR :
|
@dpordomingo so is it ready for review, right? |
Yes, it is :) |
tests/query_test.go
Outdated
}) | ||
|
||
s.NotPanics(func() { | ||
//s.True(store.MustFindOne(NewQueryFixtureQuery().FindByBoolean(false)).Eq(queryFixtures[1])) |
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.
leftovers?
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.
nope,
Those tests are already broken tests, affected by #96
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.
Oh, ok, then lgtm
49dc8e0
to
90df9e8
Compare
@dpordomingo tests are failing. Are they supposed to fail yet? |
@erizocosmico @Serabe Tests now passes, it was needed to use the go-parse-utils importer 58a187c |
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.
Disregard the test thing.
case isEqualizable(f): | ||
writeTplCode(buf, parent, f, tplFindByEquality) | ||
case isSortable(f): | ||
writeTplCode(buf, parent, f, tplFindByCondition) |
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.
This path is not tested.
Fix import problem in template_test
solves #39
Functionality
Kallax will auto-generate the
FindBy
of each Model property if it satisfies one of the following;A property will cause an auto-generated
FindBy
if:"Valid"
one:"Basic"
, one of the following:string
,byte
,bool
,float
(s),int
(s) oruint
(s) (see)"Special"
(see)"kallaxKnownTypes"
(SQL already knows how to handle it) (see)"SQLType"
(implementssql.Scanner
anddriver.Valuer
, see)"collection"
of"Valid"
typesType aliases will be valid if one of its underlying types is a
"Valid"
one.Generated FindBy
The auto-generated
FindBy
for a given property named"PropertyName"
will be as it follows:model.ID
->FindByID(v ...kallax.ID)
the query will success if the model.ID is one of the passed
kallax.ID
"Basic"
and"Special"
propertiesstring
andboolean
properties ->FindByPopertyName(v validType)
,the query will success if the model property is
equal
to the passedkallax.ID
FindByPopertyName(cond kallax.ScalarCond, v validType)
,the query will success if the model property satisfies the passed
kallax.ScalarCond
"Collection"
properties ->FindByPopertyName(v ...singleValidType)
the query will success if the model collection property contains one of the passed values