Merge pull request #21 from sunboyy/find-sort
Add functionality to sort in find operation
This commit is contained in:
commit
706706e91e
10 changed files with 460 additions and 31 deletions
|
@ -131,7 +131,7 @@ FindByCity(ctx context.Context, city string) ([]*Model, error)
|
||||||
FindAll(ctx context.Context) ([]*Model, error)
|
FindAll(ctx context.Context) ([]*Model, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
Repogen determines a single-entity operation or a multiple-entity by checking the first return value. If it is a pointer of a model, the method will be single-entity operation. If it is a slice of pointers of a model, the method will be multiple-entity operation.
|
Repogen determines a single-entity or a multiple-entity operation by checking the first return value. If it is a pointer of a model, the method will be single-entity operation. If it is a slice of pointers of a model, the method will be multiple-entity operation.
|
||||||
|
|
||||||
The requirement of the `Find` operation method is that there must be only two return values, the second return value must be of type `error` and the first method parameter must be of type `context.Context`. The requirement of number of method parameters depends on the query which will be described in the query specification section.
|
The requirement of the `Find` operation method is that there must be only two return values, the second return value must be of type `error` and the first method parameter must be of type `context.Context`. The requirement of number of method parameters depends on the query which will be described in the query specification section.
|
||||||
|
|
||||||
|
|
|
@ -77,7 +77,7 @@ func TestGenerateMongoRepository(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "FindByAgeLessThanEqual",
|
Name: "FindByAgeLessThanEqualOrderByAge",
|
||||||
Params: []code.Param{
|
Params: []code.Param{
|
||||||
{Name: "ctx", Type: code.ExternalType{PackageAlias: "context", Name: "Context"}},
|
{Name: "ctx", Type: code.ExternalType{PackageAlias: "context", Name: "Context"}},
|
||||||
{Name: "age", Type: code.SimpleType("int")},
|
{Name: "age", Type: code.SimpleType("int")},
|
||||||
|
@ -93,10 +93,13 @@ func TestGenerateMongoRepository(t *testing.T) {
|
||||||
{Field: "Age", Comparator: spec.ComparatorLessThanEqual, ParamIndex: 1},
|
{Field: "Age", Comparator: spec.ComparatorLessThanEqual, ParamIndex: 1},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
Sorts: []spec.Sort{
|
||||||
|
{FieldName: "Age", Ordering: spec.OrderingAscending},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "FindByAgeGreaterThan",
|
Name: "FindByAgeGreaterThanOrderByAgeAsc",
|
||||||
Params: []code.Param{
|
Params: []code.Param{
|
||||||
{Name: "ctx", Type: code.ExternalType{PackageAlias: "context", Name: "Context"}},
|
{Name: "ctx", Type: code.ExternalType{PackageAlias: "context", Name: "Context"}},
|
||||||
{Name: "age", Type: code.SimpleType("int")},
|
{Name: "age", Type: code.SimpleType("int")},
|
||||||
|
@ -112,10 +115,13 @@ func TestGenerateMongoRepository(t *testing.T) {
|
||||||
{Field: "Age", Comparator: spec.ComparatorGreaterThan, ParamIndex: 1},
|
{Field: "Age", Comparator: spec.ComparatorGreaterThan, ParamIndex: 1},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
Sorts: []spec.Sort{
|
||||||
|
{FieldName: "Age", Ordering: spec.OrderingAscending},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "FindByAgeGreaterThanEqual",
|
Name: "FindByAgeGreaterThanEqualOrderByAgeDesc",
|
||||||
Params: []code.Param{
|
Params: []code.Param{
|
||||||
{Name: "ctx", Type: code.ExternalType{PackageAlias: "context", Name: "Context"}},
|
{Name: "ctx", Type: code.ExternalType{PackageAlias: "context", Name: "Context"}},
|
||||||
{Name: "age", Type: code.SimpleType("int")},
|
{Name: "age", Type: code.SimpleType("int")},
|
||||||
|
@ -131,6 +137,9 @@ func TestGenerateMongoRepository(t *testing.T) {
|
||||||
{Field: "Age", Comparator: spec.ComparatorGreaterThanEqual, ParamIndex: 1},
|
{Field: "Age", Comparator: spec.ComparatorGreaterThanEqual, ParamIndex: 1},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
Sorts: []spec.Sort{
|
||||||
|
{FieldName: "Age", Ordering: spec.OrderingDescending},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -191,6 +200,7 @@ import (
|
||||||
"go.mongodb.org/mongo-driver/bson"
|
"go.mongodb.org/mongo-driver/bson"
|
||||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||||
"go.mongodb.org/mongo-driver/mongo"
|
"go.mongodb.org/mongo-driver/mongo"
|
||||||
|
"go.mongodb.org/mongo-driver/mongo/options"
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewUserRepository(collection *mongo.Collection) UserRepository {
|
func NewUserRepository(collection *mongo.Collection) UserRepository {
|
||||||
|
@ -207,7 +217,7 @@ func (r *UserRepositoryMongo) FindByID(arg0 context.Context, arg1 primitive.Obje
|
||||||
var entity UserModel
|
var entity UserModel
|
||||||
if err := r.collection.FindOne(arg0, bson.M{
|
if err := r.collection.FindOne(arg0, bson.M{
|
||||||
"_id": arg1,
|
"_id": arg1,
|
||||||
}).Decode(&entity); err != nil {
|
}, options.FindOne().SetSort(bson.M{})).Decode(&entity); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &entity, nil
|
return &entity, nil
|
||||||
|
@ -219,7 +229,7 @@ func (r *UserRepositoryMongo) FindByGenderNotAndAgeLessThan(arg0 context.Context
|
||||||
{"gender": bson.M{"$ne": arg1}},
|
{"gender": bson.M{"$ne": arg1}},
|
||||||
{"age": bson.M{"$lt": arg2}},
|
{"age": bson.M{"$lt": arg2}},
|
||||||
},
|
},
|
||||||
})
|
}, options.Find().SetSort(bson.M{}))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -230,10 +240,12 @@ func (r *UserRepositoryMongo) FindByGenderNotAndAgeLessThan(arg0 context.Context
|
||||||
return entities, nil
|
return entities, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *UserRepositoryMongo) FindByAgeLessThanEqual(arg0 context.Context, arg1 int) ([]*UserModel, error) {
|
func (r *UserRepositoryMongo) FindByAgeLessThanEqualOrderByAge(arg0 context.Context, arg1 int) ([]*UserModel, error) {
|
||||||
cursor, err := r.collection.Find(arg0, bson.M{
|
cursor, err := r.collection.Find(arg0, bson.M{
|
||||||
"age": bson.M{"$lte": arg1},
|
"age": bson.M{"$lte": arg1},
|
||||||
})
|
}, options.Find().SetSort(bson.M{
|
||||||
|
"age": 1,
|
||||||
|
}))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -244,10 +256,12 @@ func (r *UserRepositoryMongo) FindByAgeLessThanEqual(arg0 context.Context, arg1
|
||||||
return entities, nil
|
return entities, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *UserRepositoryMongo) FindByAgeGreaterThan(arg0 context.Context, arg1 int) ([]*UserModel, error) {
|
func (r *UserRepositoryMongo) FindByAgeGreaterThanOrderByAgeAsc(arg0 context.Context, arg1 int) ([]*UserModel, error) {
|
||||||
cursor, err := r.collection.Find(arg0, bson.M{
|
cursor, err := r.collection.Find(arg0, bson.M{
|
||||||
"age": bson.M{"$gt": arg1},
|
"age": bson.M{"$gt": arg1},
|
||||||
})
|
}, options.Find().SetSort(bson.M{
|
||||||
|
"age": 1,
|
||||||
|
}))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -258,10 +272,12 @@ func (r *UserRepositoryMongo) FindByAgeGreaterThan(arg0 context.Context, arg1 in
|
||||||
return entities, nil
|
return entities, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *UserRepositoryMongo) FindByAgeGreaterThanEqual(arg0 context.Context, arg1 int) ([]*UserModel, error) {
|
func (r *UserRepositoryMongo) FindByAgeGreaterThanEqualOrderByAgeDesc(arg0 context.Context, arg1 int) ([]*UserModel, error) {
|
||||||
cursor, err := r.collection.Find(arg0, bson.M{
|
cursor, err := r.collection.Find(arg0, bson.M{
|
||||||
"age": bson.M{"$gte": arg1},
|
"age": bson.M{"$gte": arg1},
|
||||||
})
|
}, options.Find().SetSort(bson.M{
|
||||||
|
"age": -1,
|
||||||
|
}))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -275,7 +291,7 @@ func (r *UserRepositoryMongo) FindByAgeGreaterThanEqual(arg0 context.Context, ar
|
||||||
func (r *UserRepositoryMongo) FindByAgeBetween(arg0 context.Context, arg1 int, arg2 int) ([]*UserModel, error) {
|
func (r *UserRepositoryMongo) FindByAgeBetween(arg0 context.Context, arg1 int, arg2 int) ([]*UserModel, error) {
|
||||||
cursor, err := r.collection.Find(arg0, bson.M{
|
cursor, err := r.collection.Find(arg0, bson.M{
|
||||||
"age": bson.M{"$gte": arg1, "$lte": arg2},
|
"age": bson.M{"$gte": arg1, "$lte": arg2},
|
||||||
})
|
}, options.Find().SetSort(bson.M{}))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -292,7 +308,7 @@ func (r *UserRepositoryMongo) FindByGenderOrAge(arg0 context.Context, arg1 Gende
|
||||||
{"gender": arg1},
|
{"gender": arg1},
|
||||||
{"age": arg2},
|
{"age": arg2},
|
||||||
},
|
},
|
||||||
})
|
}, options.Find().SetSort(bson.M{}))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -105,9 +105,15 @@ func (g RepositoryGenerator) generateFindImplementation(operation spec.FindOpera
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sorts, err := g.mongoSorts(operation.Sorts)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
tmplData := mongoFindTemplateData{
|
tmplData := mongoFindTemplateData{
|
||||||
EntityType: g.StructModel.Name,
|
EntityType: g.StructModel.Name,
|
||||||
QuerySpec: querySpec,
|
QuerySpec: querySpec,
|
||||||
|
Sorts: sorts,
|
||||||
}
|
}
|
||||||
|
|
||||||
if operation.Mode == spec.QueryModeOne {
|
if operation.Mode == spec.QueryModeOne {
|
||||||
|
@ -116,6 +122,24 @@ func (g RepositoryGenerator) generateFindImplementation(operation spec.FindOpera
|
||||||
return generateFromTemplate("mongo_repository_findmany", findManyTemplate, tmplData)
|
return generateFromTemplate("mongo_repository_findmany", findManyTemplate, tmplData)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (g RepositoryGenerator) mongoSorts(sortSpec []spec.Sort) ([]sort, error) {
|
||||||
|
var sorts []sort
|
||||||
|
|
||||||
|
for _, s := range sortSpec {
|
||||||
|
bsonTag, err := g.bsonTagFromFieldName(s.FieldName)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
sorts = append(sorts, sort{
|
||||||
|
BsonTag: bsonTag,
|
||||||
|
Ordering: s.Ordering,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return sorts, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (g RepositoryGenerator) generateUpdateImplementation(operation spec.UpdateOperation) (string, error) {
|
func (g RepositoryGenerator) generateUpdateImplementation(operation spec.UpdateOperation) (string, error) {
|
||||||
update, err := g.getMongoUpdate(operation.Update)
|
update, err := g.getMongoUpdate(operation.Update)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -52,6 +52,7 @@ import (
|
||||||
"go.mongodb.org/mongo-driver/bson"
|
"go.mongodb.org/mongo-driver/bson"
|
||||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||||
"go.mongodb.org/mongo-driver/mongo"
|
"go.mongodb.org/mongo-driver/mongo"
|
||||||
|
"go.mongodb.org/mongo-driver/mongo/options"
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewUserRepository(collection *mongo.Collection) UserRepository {
|
func NewUserRepository(collection *mongo.Collection) UserRepository {
|
||||||
|
@ -192,7 +193,8 @@ func (r *UserRepositoryMongo) FindByID(arg0 context.Context, arg1 primitive.Obje
|
||||||
var entity UserModel
|
var entity UserModel
|
||||||
if err := r.collection.FindOne(arg0, bson.M{
|
if err := r.collection.FindOne(arg0, bson.M{
|
||||||
"_id": arg1,
|
"_id": arg1,
|
||||||
}).Decode(&entity); err != nil {
|
}, options.FindOne().SetSort(bson.M{
|
||||||
|
})).Decode(&entity); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &entity, nil
|
return &entity, nil
|
||||||
|
@ -224,7 +226,8 @@ func (r *UserRepositoryMongo) FindByID(arg0 context.Context, arg1 primitive.Obje
|
||||||
func (r *UserRepositoryMongo) FindByGender(arg0 context.Context, arg1 Gender) ([]*UserModel, error) {
|
func (r *UserRepositoryMongo) FindByGender(arg0 context.Context, arg1 Gender) ([]*UserModel, error) {
|
||||||
cursor, err := r.collection.Find(arg0, bson.M{
|
cursor, err := r.collection.Find(arg0, bson.M{
|
||||||
"gender": arg1,
|
"gender": arg1,
|
||||||
})
|
}, options.Find().SetSort(bson.M{
|
||||||
|
}))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -267,7 +270,8 @@ func (r *UserRepositoryMongo) FindByGenderAndAge(arg0 context.Context, arg1 Gend
|
||||||
{"gender": arg1},
|
{"gender": arg1},
|
||||||
{"age": arg2},
|
{"age": arg2},
|
||||||
},
|
},
|
||||||
})
|
}, options.Find().SetSort(bson.M{
|
||||||
|
}))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -310,7 +314,8 @@ func (r *UserRepositoryMongo) FindByGenderOrAge(arg0 context.Context, arg1 Gende
|
||||||
{"gender": arg1},
|
{"gender": arg1},
|
||||||
{"age": arg2},
|
{"age": arg2},
|
||||||
},
|
},
|
||||||
})
|
}, options.Find().SetSort(bson.M{
|
||||||
|
}))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -347,7 +352,8 @@ func (r *UserRepositoryMongo) FindByGenderOrAge(arg0 context.Context, arg1 Gende
|
||||||
func (r *UserRepositoryMongo) FindByGenderNot(arg0 context.Context, arg1 Gender) ([]*UserModel, error) {
|
func (r *UserRepositoryMongo) FindByGenderNot(arg0 context.Context, arg1 Gender) ([]*UserModel, error) {
|
||||||
cursor, err := r.collection.Find(arg0, bson.M{
|
cursor, err := r.collection.Find(arg0, bson.M{
|
||||||
"gender": bson.M{"$ne": arg1},
|
"gender": bson.M{"$ne": arg1},
|
||||||
})
|
}, options.Find().SetSort(bson.M{
|
||||||
|
}))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -384,7 +390,8 @@ func (r *UserRepositoryMongo) FindByGenderNot(arg0 context.Context, arg1 Gender)
|
||||||
func (r *UserRepositoryMongo) FindByAgeLessThan(arg0 context.Context, arg1 int) ([]*UserModel, error) {
|
func (r *UserRepositoryMongo) FindByAgeLessThan(arg0 context.Context, arg1 int) ([]*UserModel, error) {
|
||||||
cursor, err := r.collection.Find(arg0, bson.M{
|
cursor, err := r.collection.Find(arg0, bson.M{
|
||||||
"age": bson.M{"$lt": arg1},
|
"age": bson.M{"$lt": arg1},
|
||||||
})
|
}, options.Find().SetSort(bson.M{
|
||||||
|
}))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -421,7 +428,8 @@ func (r *UserRepositoryMongo) FindByAgeLessThan(arg0 context.Context, arg1 int)
|
||||||
func (r *UserRepositoryMongo) FindByAgeLessThanEqual(arg0 context.Context, arg1 int) ([]*UserModel, error) {
|
func (r *UserRepositoryMongo) FindByAgeLessThanEqual(arg0 context.Context, arg1 int) ([]*UserModel, error) {
|
||||||
cursor, err := r.collection.Find(arg0, bson.M{
|
cursor, err := r.collection.Find(arg0, bson.M{
|
||||||
"age": bson.M{"$lte": arg1},
|
"age": bson.M{"$lte": arg1},
|
||||||
})
|
}, options.Find().SetSort(bson.M{
|
||||||
|
}))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -458,7 +466,8 @@ func (r *UserRepositoryMongo) FindByAgeLessThanEqual(arg0 context.Context, arg1
|
||||||
func (r *UserRepositoryMongo) FindByAgeGreaterThan(arg0 context.Context, arg1 int) ([]*UserModel, error) {
|
func (r *UserRepositoryMongo) FindByAgeGreaterThan(arg0 context.Context, arg1 int) ([]*UserModel, error) {
|
||||||
cursor, err := r.collection.Find(arg0, bson.M{
|
cursor, err := r.collection.Find(arg0, bson.M{
|
||||||
"age": bson.M{"$gt": arg1},
|
"age": bson.M{"$gt": arg1},
|
||||||
})
|
}, options.Find().SetSort(bson.M{
|
||||||
|
}))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -495,7 +504,8 @@ func (r *UserRepositoryMongo) FindByAgeGreaterThan(arg0 context.Context, arg1 in
|
||||||
func (r *UserRepositoryMongo) FindByAgeGreaterThanEqual(arg0 context.Context, arg1 int) ([]*UserModel, error) {
|
func (r *UserRepositoryMongo) FindByAgeGreaterThanEqual(arg0 context.Context, arg1 int) ([]*UserModel, error) {
|
||||||
cursor, err := r.collection.Find(arg0, bson.M{
|
cursor, err := r.collection.Find(arg0, bson.M{
|
||||||
"age": bson.M{"$gte": arg1},
|
"age": bson.M{"$gte": arg1},
|
||||||
})
|
}, options.Find().SetSort(bson.M{
|
||||||
|
}))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -533,7 +543,8 @@ func (r *UserRepositoryMongo) FindByAgeGreaterThanEqual(arg0 context.Context, ar
|
||||||
func (r *UserRepositoryMongo) FindByAgeBetween(arg0 context.Context, arg1 int, arg2 int) ([]*UserModel, error) {
|
func (r *UserRepositoryMongo) FindByAgeBetween(arg0 context.Context, arg1 int, arg2 int) ([]*UserModel, error) {
|
||||||
cursor, err := r.collection.Find(arg0, bson.M{
|
cursor, err := r.collection.Find(arg0, bson.M{
|
||||||
"age": bson.M{"$gte": arg1, "$lte": arg2},
|
"age": bson.M{"$gte": arg1, "$lte": arg2},
|
||||||
})
|
}, options.Find().SetSort(bson.M{
|
||||||
|
}))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -570,7 +581,8 @@ func (r *UserRepositoryMongo) FindByAgeBetween(arg0 context.Context, arg1 int, a
|
||||||
func (r *UserRepositoryMongo) FindByGenderIn(arg0 context.Context, arg1 []Gender) ([]*UserModel, error) {
|
func (r *UserRepositoryMongo) FindByGenderIn(arg0 context.Context, arg1 []Gender) ([]*UserModel, error) {
|
||||||
cursor, err := r.collection.Find(arg0, bson.M{
|
cursor, err := r.collection.Find(arg0, bson.M{
|
||||||
"gender": bson.M{"$in": arg1},
|
"gender": bson.M{"$in": arg1},
|
||||||
})
|
}, options.Find().SetSort(bson.M{
|
||||||
|
}))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -607,7 +619,8 @@ func (r *UserRepositoryMongo) FindByGenderIn(arg0 context.Context, arg1 []Gender
|
||||||
func (r *UserRepositoryMongo) FindByGenderNotIn(arg0 context.Context, arg1 []Gender) ([]*UserModel, error) {
|
func (r *UserRepositoryMongo) FindByGenderNotIn(arg0 context.Context, arg1 []Gender) ([]*UserModel, error) {
|
||||||
cursor, err := r.collection.Find(arg0, bson.M{
|
cursor, err := r.collection.Find(arg0, bson.M{
|
||||||
"gender": bson.M{"$nin": arg1},
|
"gender": bson.M{"$nin": arg1},
|
||||||
})
|
}, options.Find().SetSort(bson.M{
|
||||||
|
}))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -643,7 +656,8 @@ func (r *UserRepositoryMongo) FindByGenderNotIn(arg0 context.Context, arg1 []Gen
|
||||||
func (r *UserRepositoryMongo) FindByEnabledTrue(arg0 context.Context) ([]*UserModel, error) {
|
func (r *UserRepositoryMongo) FindByEnabledTrue(arg0 context.Context) ([]*UserModel, error) {
|
||||||
cursor, err := r.collection.Find(arg0, bson.M{
|
cursor, err := r.collection.Find(arg0, bson.M{
|
||||||
"enabled": true,
|
"enabled": true,
|
||||||
})
|
}, options.Find().SetSort(bson.M{
|
||||||
|
}))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -679,7 +693,118 @@ func (r *UserRepositoryMongo) FindByEnabledTrue(arg0 context.Context) ([]*UserMo
|
||||||
func (r *UserRepositoryMongo) FindByEnabledFalse(arg0 context.Context) ([]*UserModel, error) {
|
func (r *UserRepositoryMongo) FindByEnabledFalse(arg0 context.Context) ([]*UserModel, error) {
|
||||||
cursor, err := r.collection.Find(arg0, bson.M{
|
cursor, err := r.collection.Find(arg0, bson.M{
|
||||||
"enabled": false,
|
"enabled": false,
|
||||||
})
|
}, options.Find().SetSort(bson.M{
|
||||||
|
}))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
var entities []*UserModel
|
||||||
|
if err := cursor.All(arg0, &entities); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return entities, nil
|
||||||
|
}
|
||||||
|
`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "find with sort ascending",
|
||||||
|
MethodSpec: spec.MethodSpec{
|
||||||
|
Name: "FindAllOrderByAge",
|
||||||
|
Params: []code.Param{
|
||||||
|
{Name: "ctx", Type: code.ExternalType{PackageAlias: "context", Name: "Context"}},
|
||||||
|
},
|
||||||
|
Returns: []code.Type{
|
||||||
|
code.ArrayType{ContainedType: code.PointerType{ContainedType: code.SimpleType("UserModel")}},
|
||||||
|
code.SimpleType("error"),
|
||||||
|
},
|
||||||
|
Operation: spec.FindOperation{
|
||||||
|
Mode: spec.QueryModeMany,
|
||||||
|
Sorts: []spec.Sort{
|
||||||
|
{FieldName: "Age", Ordering: spec.OrderingAscending},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
ExpectedCode: `
|
||||||
|
func (r *UserRepositoryMongo) FindAllOrderByAge(arg0 context.Context) ([]*UserModel, error) {
|
||||||
|
cursor, err := r.collection.Find(arg0, bson.M{
|
||||||
|
|
||||||
|
}, options.Find().SetSort(bson.M{
|
||||||
|
"age": 1,
|
||||||
|
}))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
var entities []*UserModel
|
||||||
|
if err := cursor.All(arg0, &entities); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return entities, nil
|
||||||
|
}
|
||||||
|
`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "find with sort descending",
|
||||||
|
MethodSpec: spec.MethodSpec{
|
||||||
|
Name: "FindAllOrderByAge",
|
||||||
|
Params: []code.Param{
|
||||||
|
{Name: "ctx", Type: code.ExternalType{PackageAlias: "context", Name: "Context"}},
|
||||||
|
},
|
||||||
|
Returns: []code.Type{
|
||||||
|
code.ArrayType{ContainedType: code.PointerType{ContainedType: code.SimpleType("UserModel")}},
|
||||||
|
code.SimpleType("error"),
|
||||||
|
},
|
||||||
|
Operation: spec.FindOperation{
|
||||||
|
Mode: spec.QueryModeMany,
|
||||||
|
Sorts: []spec.Sort{
|
||||||
|
{FieldName: "Age", Ordering: spec.OrderingDescending},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
ExpectedCode: `
|
||||||
|
func (r *UserRepositoryMongo) FindAllOrderByAge(arg0 context.Context) ([]*UserModel, error) {
|
||||||
|
cursor, err := r.collection.Find(arg0, bson.M{
|
||||||
|
|
||||||
|
}, options.Find().SetSort(bson.M{
|
||||||
|
"age": -1,
|
||||||
|
}))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
var entities []*UserModel
|
||||||
|
if err := cursor.All(arg0, &entities); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return entities, nil
|
||||||
|
}
|
||||||
|
`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "find with multiple sorts",
|
||||||
|
MethodSpec: spec.MethodSpec{
|
||||||
|
Name: "FindAllOrderByGenderAndAgeDesc",
|
||||||
|
Params: []code.Param{
|
||||||
|
{Name: "ctx", Type: code.ExternalType{PackageAlias: "context", Name: "Context"}},
|
||||||
|
},
|
||||||
|
Returns: []code.Type{
|
||||||
|
code.ArrayType{ContainedType: code.PointerType{ContainedType: code.SimpleType("UserModel")}},
|
||||||
|
code.SimpleType("error"),
|
||||||
|
},
|
||||||
|
Operation: spec.FindOperation{
|
||||||
|
Mode: spec.QueryModeMany,
|
||||||
|
Sorts: []spec.Sort{
|
||||||
|
{FieldName: "Gender", Ordering: spec.OrderingAscending},
|
||||||
|
{FieldName: "Age", Ordering: spec.OrderingDescending},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
ExpectedCode: `
|
||||||
|
func (r *UserRepositoryMongo) FindAllOrderByGenderAndAgeDesc(arg0 context.Context) ([]*UserModel, error) {
|
||||||
|
cursor, err := r.collection.Find(arg0, bson.M{
|
||||||
|
|
||||||
|
}, options.Find().SetSort(bson.M{
|
||||||
|
"gender": 1,
|
||||||
|
"age": -1,
|
||||||
|
}))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -1652,6 +1777,26 @@ func TestGenerateMethod_Invalid(t *testing.T) {
|
||||||
},
|
},
|
||||||
ExpectedError: mongo.NewBsonTagNotFoundError("AccessToken"),
|
ExpectedError: mongo.NewBsonTagNotFoundError("AccessToken"),
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
Name: "bson tag not found in sort",
|
||||||
|
Method: spec.MethodSpec{
|
||||||
|
Name: "FindAllOrderByAccessToken",
|
||||||
|
Params: []code.Param{
|
||||||
|
{Type: code.ExternalType{PackageAlias: "context", Name: "Context"}},
|
||||||
|
},
|
||||||
|
Returns: []code.Type{
|
||||||
|
code.ArrayType{ContainedType: code.PointerType{ContainedType: code.SimpleType("UserModel")}},
|
||||||
|
code.SimpleType("error"),
|
||||||
|
},
|
||||||
|
Operation: spec.FindOperation{
|
||||||
|
Mode: spec.QueryModeOne,
|
||||||
|
Sorts: []spec.Sort{
|
||||||
|
{FieldName: "AccessToken", Ordering: spec.OrderingAscending},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
ExpectedError: mongo.NewBsonTagNotFoundError("AccessToken"),
|
||||||
|
},
|
||||||
{
|
{
|
||||||
Name: "bson tag not found in update field",
|
Name: "bson tag not found in update field",
|
||||||
Method: spec.MethodSpec{
|
Method: spec.MethodSpec{
|
||||||
|
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/sunboyy/repogen/internal/code"
|
"github.com/sunboyy/repogen/internal/code"
|
||||||
|
"github.com/sunboyy/repogen/internal/spec"
|
||||||
)
|
)
|
||||||
|
|
||||||
const constructorTemplate = `
|
const constructorTemplate = `
|
||||||
|
@ -14,6 +15,7 @@ import (
|
||||||
"go.mongodb.org/mongo-driver/bson"
|
"go.mongodb.org/mongo-driver/bson"
|
||||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||||
"go.mongodb.org/mongo-driver/mongo"
|
"go.mongodb.org/mongo-driver/mongo"
|
||||||
|
"go.mongodb.org/mongo-driver/mongo/options"
|
||||||
)
|
)
|
||||||
|
|
||||||
func New{{.InterfaceName}}(collection *mongo.Collection) {{.InterfaceName}} {
|
func New{{.InterfaceName}}(collection *mongo.Collection) {{.InterfaceName}} {
|
||||||
|
@ -89,19 +91,36 @@ const insertManyTemplate = ` var entities []interface{}
|
||||||
type mongoFindTemplateData struct {
|
type mongoFindTemplateData struct {
|
||||||
EntityType string
|
EntityType string
|
||||||
QuerySpec querySpec
|
QuerySpec querySpec
|
||||||
|
Sorts []sort
|
||||||
|
}
|
||||||
|
|
||||||
|
type sort struct {
|
||||||
|
BsonTag string
|
||||||
|
Ordering spec.Ordering
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s sort) OrderNum() int {
|
||||||
|
if s.Ordering == spec.OrderingAscending {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
return -1
|
||||||
}
|
}
|
||||||
|
|
||||||
const findOneTemplate = ` var entity {{.EntityType}}
|
const findOneTemplate = ` var entity {{.EntityType}}
|
||||||
if err := r.collection.FindOne(arg0, bson.M{
|
if err := r.collection.FindOne(arg0, bson.M{
|
||||||
{{.QuerySpec.Code}}
|
{{.QuerySpec.Code}}
|
||||||
}).Decode(&entity); err != nil {
|
}, options.FindOne().SetSort(bson.M{
|
||||||
|
{{range $index, $element := .Sorts}} "{{$element.BsonTag}}": {{$element.OrderNum}},
|
||||||
|
{{end}} })).Decode(&entity); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &entity, nil`
|
return &entity, nil`
|
||||||
|
|
||||||
const findManyTemplate = ` cursor, err := r.collection.Find(arg0, bson.M{
|
const findManyTemplate = ` cursor, err := r.collection.Find(arg0, bson.M{
|
||||||
{{.QuerySpec.Code}}
|
{{.QuerySpec.Code}}
|
||||||
})
|
}, options.Find().SetSort(bson.M{
|
||||||
|
{{range $index, $element := .Sorts}} "{{$element.BsonTag}}": {{$element.OrderNum}},
|
||||||
|
{{end}} }))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -48,6 +48,19 @@ func (err invalidQueryError) Error() string {
|
||||||
return fmt.Sprintf("invalid query '%s'", err.QueryString)
|
return fmt.Sprintf("invalid query '%s'", err.QueryString)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewInvalidSortError creates invalidSortError
|
||||||
|
func NewInvalidSortError(sortTokens []string) error {
|
||||||
|
return invalidSortError{SortString: strings.Join(sortTokens, "")}
|
||||||
|
}
|
||||||
|
|
||||||
|
type invalidSortError struct {
|
||||||
|
SortString string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (err invalidSortError) Error() string {
|
||||||
|
return fmt.Sprintf("invalid sort '%s'", err.SortString)
|
||||||
|
}
|
||||||
|
|
||||||
// NewUnknownOperationError creates unknownOperationError
|
// NewUnknownOperationError creates unknownOperationError
|
||||||
func NewUnknownOperationError(operationName string) error {
|
func NewUnknownOperationError(operationName string) error {
|
||||||
return unknownOperationError{OperationName: operationName}
|
return unknownOperationError{OperationName: operationName}
|
||||||
|
|
|
@ -38,6 +38,11 @@ func TestError(t *testing.T) {
|
||||||
}),
|
}),
|
||||||
ExpectedString: "cannot use comparator EQUAL_TRUE with struct field 'Age' of type 'int'",
|
ExpectedString: "cannot use comparator EQUAL_TRUE with struct field 'Age' of type 'int'",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
Name: "InvalidSortError",
|
||||||
|
Error: spec.NewInvalidSortError([]string{"Order", "By"}),
|
||||||
|
ExpectedString: "invalid sort 'OrderBy'",
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, testCase := range testTable {
|
for _, testCase := range testTable {
|
||||||
|
|
|
@ -40,6 +40,7 @@ func (o InsertOperation) Name() string {
|
||||||
type FindOperation struct {
|
type FindOperation struct {
|
||||||
Mode QueryMode
|
Mode QueryMode
|
||||||
Query QuerySpec
|
Query QuerySpec
|
||||||
|
Sorts []Sort
|
||||||
}
|
}
|
||||||
|
|
||||||
// Name returns "Find" operation name
|
// Name returns "Find" operation name
|
||||||
|
@ -47,6 +48,21 @@ func (o FindOperation) Name() string {
|
||||||
return "Find"
|
return "Find"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Sort is a detail of sorting find result
|
||||||
|
type Sort struct {
|
||||||
|
FieldName string
|
||||||
|
Ordering Ordering
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ordering is a sort order
|
||||||
|
type Ordering string
|
||||||
|
|
||||||
|
// Ordering constants
|
||||||
|
const (
|
||||||
|
OrderingAscending = "ASC"
|
||||||
|
OrderingDescending = "DESC"
|
||||||
|
)
|
||||||
|
|
||||||
// UpdateOperation is a method specification for update operations
|
// UpdateOperation is a method specification for update operations
|
||||||
type UpdateOperation struct {
|
type UpdateOperation struct {
|
||||||
Update Update
|
Update Update
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
package spec
|
package spec
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/fatih/camelcase"
|
"github.com/fatih/camelcase"
|
||||||
"github.com/sunboyy/repogen/internal/code"
|
"github.com/sunboyy/repogen/internal/code"
|
||||||
)
|
)
|
||||||
|
@ -111,7 +113,14 @@ func (p interfaceMethodParser) parseFindOperation(tokens []string) (Operation, e
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
querySpec, err := parseQuery(tokens, 1)
|
queryTokens, sortTokens := p.splitQueryAndSortTokens(tokens)
|
||||||
|
|
||||||
|
querySpec, err := parseQuery(queryTokens, 1)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
sorts, err := p.parseSort(sortTokens)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -127,9 +136,65 @@ func (p interfaceMethodParser) parseFindOperation(tokens []string) (Operation, e
|
||||||
return FindOperation{
|
return FindOperation{
|
||||||
Mode: mode,
|
Mode: mode,
|
||||||
Query: querySpec,
|
Query: querySpec,
|
||||||
|
Sorts: sorts,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p interfaceMethodParser) parseSort(rawTokens []string) ([]Sort, error) {
|
||||||
|
if len(rawTokens) == 0 {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
sortTokens := rawTokens[2:]
|
||||||
|
|
||||||
|
var sorts []Sort
|
||||||
|
var aggregatedToken sortToken
|
||||||
|
for _, token := range sortTokens {
|
||||||
|
if token != "And" {
|
||||||
|
aggregatedToken = append(aggregatedToken, token)
|
||||||
|
} else if len(aggregatedToken) == 0 {
|
||||||
|
return nil, NewInvalidSortError(rawTokens)
|
||||||
|
} else {
|
||||||
|
sorts = append(sorts, aggregatedToken.ToSort())
|
||||||
|
aggregatedToken = sortToken{}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(aggregatedToken) == 0 {
|
||||||
|
return nil, NewInvalidSortError(rawTokens)
|
||||||
|
}
|
||||||
|
sorts = append(sorts, aggregatedToken.ToSort())
|
||||||
|
|
||||||
|
return sorts, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type sortToken []string
|
||||||
|
|
||||||
|
func (t sortToken) ToSort() Sort {
|
||||||
|
if len(t) > 1 && t[len(t)-1] == "Asc" {
|
||||||
|
return Sort{FieldName: strings.Join(t[:len(t)-1], ""), Ordering: OrderingAscending}
|
||||||
|
}
|
||||||
|
if len(t) > 1 && t[len(t)-1] == "Desc" {
|
||||||
|
return Sort{FieldName: strings.Join(t[:len(t)-1], ""), Ordering: OrderingDescending}
|
||||||
|
}
|
||||||
|
return Sort{FieldName: strings.Join(t, ""), Ordering: OrderingAscending}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p interfaceMethodParser) splitQueryAndSortTokens(tokens []string) ([]string, []string) {
|
||||||
|
var queryTokens []string
|
||||||
|
var sortTokens []string
|
||||||
|
|
||||||
|
for i, token := range tokens {
|
||||||
|
if len(tokens) > i && token == "Order" && tokens[i+1] == "By" {
|
||||||
|
sortTokens = tokens[i:]
|
||||||
|
break
|
||||||
|
} else {
|
||||||
|
queryTokens = append(queryTokens, token)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return queryTokens, sortTokens
|
||||||
|
}
|
||||||
|
|
||||||
func (p interfaceMethodParser) extractModelOrSliceReturns(returns []code.Type) (QueryMode, error) {
|
func (p interfaceMethodParser) extractModelOrSliceReturns(returns []code.Type) (QueryMode, error) {
|
||||||
if len(returns) != 2 {
|
if len(returns) != 2 {
|
||||||
return "", UnsupportedReturnError
|
return "", UnsupportedReturnError
|
||||||
|
|
|
@ -429,6 +429,99 @@ func TestParseInterfaceMethod_Find(t *testing.T) {
|
||||||
}},
|
}},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
Name: "FindByArgOrderByArg method",
|
||||||
|
Method: code.Method{
|
||||||
|
Name: "FindByCityOrderByAge",
|
||||||
|
Params: []code.Param{
|
||||||
|
{Type: code.ExternalType{PackageAlias: "context", Name: "Context"}},
|
||||||
|
{Type: code.SimpleType("string")},
|
||||||
|
},
|
||||||
|
Returns: []code.Type{
|
||||||
|
code.ArrayType{ContainedType: code.PointerType{ContainedType: code.SimpleType("UserModel")}},
|
||||||
|
code.SimpleType("error"),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
ExpectedOperation: spec.FindOperation{
|
||||||
|
Mode: spec.QueryModeMany,
|
||||||
|
Query: spec.QuerySpec{Predicates: []spec.Predicate{
|
||||||
|
{Field: "City", Comparator: spec.ComparatorEqual, ParamIndex: 1},
|
||||||
|
}},
|
||||||
|
Sorts: []spec.Sort{
|
||||||
|
{FieldName: "Age", Ordering: spec.OrderingAscending},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "FindByArgOrderByArgAsc method",
|
||||||
|
Method: code.Method{
|
||||||
|
Name: "FindByCityOrderByAgeAsc",
|
||||||
|
Params: []code.Param{
|
||||||
|
{Type: code.ExternalType{PackageAlias: "context", Name: "Context"}},
|
||||||
|
{Type: code.SimpleType("string")},
|
||||||
|
},
|
||||||
|
Returns: []code.Type{
|
||||||
|
code.ArrayType{ContainedType: code.PointerType{ContainedType: code.SimpleType("UserModel")}},
|
||||||
|
code.SimpleType("error"),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
ExpectedOperation: spec.FindOperation{
|
||||||
|
Mode: spec.QueryModeMany,
|
||||||
|
Query: spec.QuerySpec{Predicates: []spec.Predicate{
|
||||||
|
{Field: "City", Comparator: spec.ComparatorEqual, ParamIndex: 1},
|
||||||
|
}},
|
||||||
|
Sorts: []spec.Sort{
|
||||||
|
{FieldName: "Age", Ordering: spec.OrderingAscending},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "FindByArgOrderByArgDesc method",
|
||||||
|
Method: code.Method{
|
||||||
|
Name: "FindByCityOrderByAgeDesc",
|
||||||
|
Params: []code.Param{
|
||||||
|
{Type: code.ExternalType{PackageAlias: "context", Name: "Context"}},
|
||||||
|
{Type: code.SimpleType("string")},
|
||||||
|
},
|
||||||
|
Returns: []code.Type{
|
||||||
|
code.ArrayType{ContainedType: code.PointerType{ContainedType: code.SimpleType("UserModel")}},
|
||||||
|
code.SimpleType("error"),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
ExpectedOperation: spec.FindOperation{
|
||||||
|
Mode: spec.QueryModeMany,
|
||||||
|
Query: spec.QuerySpec{Predicates: []spec.Predicate{
|
||||||
|
{Field: "City", Comparator: spec.ComparatorEqual, ParamIndex: 1},
|
||||||
|
}},
|
||||||
|
Sorts: []spec.Sort{
|
||||||
|
{FieldName: "Age", Ordering: spec.OrderingDescending},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "FindByArgOrderByArgAndArg method",
|
||||||
|
Method: code.Method{
|
||||||
|
Name: "FindByCityOrderByCityAndAgeDesc",
|
||||||
|
Params: []code.Param{
|
||||||
|
{Type: code.ExternalType{PackageAlias: "context", Name: "Context"}},
|
||||||
|
{Type: code.SimpleType("string")},
|
||||||
|
},
|
||||||
|
Returns: []code.Type{
|
||||||
|
code.ArrayType{ContainedType: code.PointerType{ContainedType: code.SimpleType("UserModel")}},
|
||||||
|
code.SimpleType("error"),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
ExpectedOperation: spec.FindOperation{
|
||||||
|
Mode: spec.QueryModeMany,
|
||||||
|
Query: spec.QuerySpec{Predicates: []spec.Predicate{
|
||||||
|
{Field: "City", Comparator: spec.ComparatorEqual, ParamIndex: 1},
|
||||||
|
}},
|
||||||
|
Sorts: []spec.Sort{
|
||||||
|
{FieldName: "City", Ordering: spec.OrderingAscending},
|
||||||
|
{FieldName: "Age", Ordering: spec.OrderingDescending},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, testCase := range testTable {
|
for _, testCase := range testTable {
|
||||||
|
@ -1248,6 +1341,39 @@ func TestParseInterfaceMethod_Find_Invalid(t *testing.T) {
|
||||||
},
|
},
|
||||||
ExpectedError: spec.InvalidParamError,
|
ExpectedError: spec.InvalidParamError,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
Name: "misplaced operator token (leftmost)",
|
||||||
|
Method: code.Method{
|
||||||
|
Name: "FindAllOrderByAndAge",
|
||||||
|
Returns: []code.Type{
|
||||||
|
code.ArrayType{ContainedType: code.PointerType{ContainedType: code.SimpleType("UserModel")}},
|
||||||
|
code.SimpleType("error"),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
ExpectedError: spec.NewInvalidSortError([]string{"Order", "By", "And", "Age"}),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "misplaced operator token (rightmost)",
|
||||||
|
Method: code.Method{
|
||||||
|
Name: "FindAllOrderByAgeAnd",
|
||||||
|
Returns: []code.Type{
|
||||||
|
code.ArrayType{ContainedType: code.PointerType{ContainedType: code.SimpleType("UserModel")}},
|
||||||
|
code.SimpleType("error"),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
ExpectedError: spec.NewInvalidSortError([]string{"Order", "By", "Age", "And"}),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "misplaced operator token (double operator)",
|
||||||
|
Method: code.Method{
|
||||||
|
Name: "FindAllOrderByAgeAndAndGender",
|
||||||
|
Returns: []code.Type{
|
||||||
|
code.ArrayType{ContainedType: code.PointerType{ContainedType: code.SimpleType("UserModel")}},
|
||||||
|
code.SimpleType("error"),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
ExpectedError: spec.NewInvalidSortError([]string{"Order", "By", "Age", "And", "And", "Gender"}),
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, testCase := range testTable {
|
for _, testCase := range testTable {
|
||||||
|
|
Loading…
Reference in a new issue