Add comparator BETWEEN and fix multi-names parameter code extractor
This commit is contained in:
parent
63d11de866
commit
e37f4dbb2f
8 changed files with 159 additions and 15 deletions
internal/spec
|
@ -41,7 +41,11 @@ type QuerySpec struct {
|
|||
|
||||
// NumberOfArguments returns number of arguments required to perform the query
|
||||
func (q QuerySpec) NumberOfArguments() int {
|
||||
return len(q.Predicates)
|
||||
var totalArgs int
|
||||
for _, predicate := range q.Predicates {
|
||||
totalArgs += predicate.Comparator.NumberOfArguments()
|
||||
}
|
||||
return totalArgs
|
||||
}
|
||||
|
||||
// Operator is a boolean operator for merging conditions
|
||||
|
@ -64,6 +68,7 @@ const (
|
|||
ComparatorLessThanEqual Comparator = "LESS_THAN_EQUAL"
|
||||
ComparatorGreaterThan Comparator = "GREATER_THAN"
|
||||
ComparatorGreaterThanEqual Comparator = "GREATER_THAN_EQUAL"
|
||||
ComparatorBetween Comparator = "BETWEEN"
|
||||
ComparatorIn Comparator = "IN"
|
||||
)
|
||||
|
||||
|
@ -75,6 +80,14 @@ func (c Comparator) ArgumentTypeFromFieldType(t code.Type) code.Type {
|
|||
return t
|
||||
}
|
||||
|
||||
// NumberOfArguments returns the number of arguments required to perform the comparison
|
||||
func (c Comparator) NumberOfArguments() int {
|
||||
if c == ComparatorBetween {
|
||||
return 2
|
||||
}
|
||||
return 1
|
||||
}
|
||||
|
||||
// Predicate is a criteria for querying a field
|
||||
type Predicate struct {
|
||||
Field string
|
||||
|
@ -102,5 +115,8 @@ func (t predicateToken) ToPredicate() Predicate {
|
|||
if len(t) > 1 && t[len(t)-1] == "In" {
|
||||
return Predicate{Field: strings.Join(t[:len(t)-1], ""), Comparator: ComparatorIn}
|
||||
}
|
||||
if len(t) > 1 && t[len(t)-1] == "Between" {
|
||||
return Predicate{Field: strings.Join(t[:len(t)-1], ""), Comparator: ComparatorBetween}
|
||||
}
|
||||
return Predicate{Field: strings.Join(t, ""), Comparator: ComparatorEqual}
|
||||
}
|
||||
|
|
|
@ -157,12 +157,13 @@ func (p interfaceMethodParser) validateMethodSignature(querySpec QuerySpec) erro
|
|||
return StructFieldNotFoundError
|
||||
}
|
||||
|
||||
if p.Method.Params[currentParamIndex].Type != predicate.Comparator.ArgumentTypeFromFieldType(
|
||||
structField.Type) {
|
||||
return InvalidParamError
|
||||
for i := 0; i < predicate.Comparator.NumberOfArguments(); i++ {
|
||||
if p.Method.Params[currentParamIndex].Type != predicate.Comparator.ArgumentTypeFromFieldType(
|
||||
structField.Type) {
|
||||
return InvalidParamError
|
||||
}
|
||||
currentParamIndex++
|
||||
}
|
||||
|
||||
currentParamIndex++
|
||||
}
|
||||
|
||||
return nil
|
||||
|
|
|
@ -390,6 +390,39 @@ func TestParseInterfaceMethod(t *testing.T) {
|
|||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "FindByArgBetween method",
|
||||
Method: code.Method{
|
||||
Name: "FindByAgeBetween",
|
||||
Params: []code.Param{
|
||||
{Type: code.ExternalType{PackageAlias: "context", Name: "Context"}},
|
||||
{Type: code.SimpleType("int")},
|
||||
{Type: code.SimpleType("int")},
|
||||
},
|
||||
Returns: []code.Type{
|
||||
code.ArrayType{ContainedType: code.PointerType{ContainedType: code.SimpleType("UserModel")}},
|
||||
code.SimpleType("error"),
|
||||
},
|
||||
},
|
||||
ExpectedOutput: spec.MethodSpec{
|
||||
Name: "FindByAgeBetween",
|
||||
Params: []code.Param{
|
||||
{Type: code.ExternalType{PackageAlias: "context", Name: "Context"}},
|
||||
{Type: code.SimpleType("int")},
|
||||
{Type: code.SimpleType("int")},
|
||||
},
|
||||
Returns: []code.Type{
|
||||
code.ArrayType{ContainedType: code.PointerType{ContainedType: code.SimpleType("UserModel")}},
|
||||
code.SimpleType("error"),
|
||||
},
|
||||
Operation: spec.FindOperation{
|
||||
Mode: spec.QueryModeMany,
|
||||
Query: spec.QuerySpec{Predicates: []spec.Predicate{
|
||||
{Field: "Age", Comparator: spec.ComparatorBetween},
|
||||
}},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "FindByArgIn method",
|
||||
Method: code.Method{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue