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/mongo
|
@ -411,6 +411,44 @@ func (r *UserRepositoryMongo) FindByAgeGreaterThanEqual(ctx context.Context, arg
|
|||
}
|
||||
return entities, nil
|
||||
}
|
||||
`,
|
||||
},
|
||||
{
|
||||
Name: "find with Between comparator",
|
||||
MethodSpec: spec.MethodSpec{
|
||||
Name: "FindByAgeBetween",
|
||||
Params: []code.Param{
|
||||
{Name: "ctx", Type: code.ExternalType{PackageAlias: "context", Name: "Context"}},
|
||||
{Name: "fromAge", Type: code.SimpleType("int")},
|
||||
{Name: "toAge", 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{
|
||||
{Comparator: spec.ComparatorBetween, Field: "Age"},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
ExpectedCode: `
|
||||
func (r *UserRepositoryMongo) FindByAgeBetween(ctx context.Context, arg0 int, arg1 int) ([]*UserModel, error) {
|
||||
cursor, err := r.collection.Find(ctx, bson.M{
|
||||
"age": bson.M{"$gte": arg0, "$lte": arg1},
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var entities []*UserModel
|
||||
if err := cursor.All(ctx, &entities); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return entities, nil
|
||||
}
|
||||
`,
|
||||
},
|
||||
{
|
||||
|
|
|
@ -14,8 +14,10 @@ type querySpec struct {
|
|||
|
||||
func (q querySpec) Code() string {
|
||||
var predicateCodes []string
|
||||
for i, predicate := range q.Predicates {
|
||||
predicateCodes = append(predicateCodes, predicate.Code(i))
|
||||
var argIndex int
|
||||
for _, predicate := range q.Predicates {
|
||||
predicateCodes = append(predicateCodes, predicate.Code(argIndex))
|
||||
argIndex += predicate.Comparator.NumberOfArguments()
|
||||
}
|
||||
|
||||
var lines []string
|
||||
|
@ -53,6 +55,8 @@ func (p predicate) Code(argIndex int) string {
|
|||
return fmt.Sprintf(`"%s": bson.M{"$gt": arg%d}`, p.Field, argIndex)
|
||||
case spec.ComparatorGreaterThanEqual:
|
||||
return fmt.Sprintf(`"%s": bson.M{"$gte": arg%d}`, p.Field, argIndex)
|
||||
case spec.ComparatorBetween:
|
||||
return fmt.Sprintf(`"%s": bson.M{"$gte": arg%d, "$lte": arg%d}`, p.Field, argIndex, argIndex+1)
|
||||
case spec.ComparatorIn:
|
||||
return fmt.Sprintf(`"%s": bson.M{"$in": arg%d}`, p.Field, argIndex)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue