Add increment update operator
This commit is contained in:
parent
0538f87a91
commit
66c63d4d6c
12 changed files with 330 additions and 42 deletions
internal/mongo
|
@ -122,8 +122,8 @@ func (g RepositoryGenerator) generateFindImplementation(operation spec.FindOpera
|
|||
return generateFromTemplate("mongo_repository_findmany", findManyTemplate, tmplData)
|
||||
}
|
||||
|
||||
func (g RepositoryGenerator) mongoSorts(sortSpec []spec.Sort) ([]sort, error) {
|
||||
var sorts []sort
|
||||
func (g RepositoryGenerator) mongoSorts(sortSpec []spec.Sort) ([]findSort, error) {
|
||||
var sorts []findSort
|
||||
|
||||
for _, s := range sortSpec {
|
||||
bsonFieldReference, err := g.bsonFieldReference(s.FieldReference)
|
||||
|
@ -131,7 +131,7 @@ func (g RepositoryGenerator) mongoSorts(sortSpec []spec.Sort) ([]sort, error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
sorts = append(sorts, sort{
|
||||
sorts = append(sorts, findSort{
|
||||
BsonTag: bsonFieldReference,
|
||||
Ordering: s.Ordering,
|
||||
})
|
||||
|
@ -196,6 +196,8 @@ func getUpdateOperatorKey(operator spec.UpdateOperator) string {
|
|||
return "$set"
|
||||
case spec.UpdateOperatorPush:
|
||||
return "$push"
|
||||
case spec.UpdateOperatorInc:
|
||||
return "$inc"
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
|
|
|
@ -1063,7 +1063,7 @@ func (r *UserRepositoryMongo) UpdateAgeByGender(arg0 context.Context, arg1 int,
|
|||
Params: []code.Param{
|
||||
{Name: "ctx", Type: code.ExternalType{PackageAlias: "context", Name: "Context"}},
|
||||
{Name: "consentHistory", Type: code.SimpleType("ConsentHistory")},
|
||||
{Name: "gender", Type: code.ExternalType{PackageAlias: "primitive", Name: "ObjectID"}},
|
||||
{Name: "id", Type: code.ExternalType{PackageAlias: "primitive", Name: "ObjectID"}},
|
||||
},
|
||||
Returns: []code.Type{
|
||||
code.SimpleType("bool"),
|
||||
|
@ -1095,6 +1095,47 @@ func (r *UserRepositoryMongo) UpdateConsentHistoryPushByID(arg0 context.Context,
|
|||
}
|
||||
return result.MatchedCount > 0, err
|
||||
}
|
||||
`,
|
||||
},
|
||||
{
|
||||
Name: "simple update inc method",
|
||||
MethodSpec: spec.MethodSpec{
|
||||
Name: "UpdateAgeIncByID",
|
||||
Params: []code.Param{
|
||||
{Name: "ctx", Type: code.ExternalType{PackageAlias: "context", Name: "Context"}},
|
||||
{Name: "age", Type: code.SimpleType("int")},
|
||||
{Name: "id", Type: code.ExternalType{PackageAlias: "primitive", Name: "ObjectID"}},
|
||||
},
|
||||
Returns: []code.Type{
|
||||
code.SimpleType("bool"),
|
||||
code.SimpleType("error"),
|
||||
},
|
||||
Operation: spec.UpdateOperation{
|
||||
Update: spec.UpdateFields{
|
||||
{FieldReference: spec.FieldReference{ageField}, ParamIndex: 1, Operator: spec.UpdateOperatorInc},
|
||||
},
|
||||
Mode: spec.QueryModeOne,
|
||||
Query: spec.QuerySpec{
|
||||
Predicates: []spec.Predicate{
|
||||
{FieldReference: spec.FieldReference{idField}, Comparator: spec.ComparatorEqual, ParamIndex: 2},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
ExpectedCode: `
|
||||
func (r *UserRepositoryMongo) UpdateAgeIncByID(arg0 context.Context, arg1 int, arg2 primitive.ObjectID) (bool, error) {
|
||||
result, err := r.collection.UpdateOne(arg0, bson.M{
|
||||
"_id": arg2,
|
||||
}, bson.M{
|
||||
"$inc": bson.M{
|
||||
"age": arg1,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return result.MatchedCount > 0, err
|
||||
}
|
||||
`,
|
||||
},
|
||||
{
|
||||
|
@ -1129,12 +1170,12 @@ func (r *UserRepositoryMongo) UpdateEnabledAndConsentHistoryPushByID(arg0 contex
|
|||
result, err := r.collection.UpdateOne(arg0, bson.M{
|
||||
"_id": arg3,
|
||||
}, bson.M{
|
||||
"$set": bson.M{
|
||||
"enabled": arg1,
|
||||
},
|
||||
"$push": bson.M{
|
||||
"consent_history": arg2,
|
||||
},
|
||||
"$set": bson.M{
|
||||
"enabled": arg1,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return false, err
|
||||
|
|
|
@ -2,6 +2,7 @@ package mongo
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/sunboyy/repogen/internal/spec"
|
||||
|
@ -26,11 +27,17 @@ func (u updateModel) Code() string {
|
|||
type updateFields map[string][]updateField
|
||||
|
||||
func (u updateFields) Code() string {
|
||||
var lines []string
|
||||
for k, v := range u {
|
||||
lines = append(lines, fmt.Sprintf(` "%s": bson.M{`, k))
|
||||
var keys []string
|
||||
for k := range u {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
sort.Strings(keys)
|
||||
|
||||
for _, field := range v {
|
||||
var lines []string
|
||||
for _, key := range keys {
|
||||
lines = append(lines, fmt.Sprintf(` "%s": bson.M{`, key))
|
||||
|
||||
for _, field := range u[key] {
|
||||
lines = append(lines, fmt.Sprintf(` "%s": arg%d,`, field.BsonTag, field.ParamIndex))
|
||||
}
|
||||
|
||||
|
|
|
@ -91,15 +91,15 @@ const insertManyTemplate = ` var entities []interface{}
|
|||
type mongoFindTemplateData struct {
|
||||
EntityType string
|
||||
QuerySpec querySpec
|
||||
Sorts []sort
|
||||
Sorts []findSort
|
||||
}
|
||||
|
||||
type sort struct {
|
||||
type findSort struct {
|
||||
BsonTag string
|
||||
Ordering spec.Ordering
|
||||
}
|
||||
|
||||
func (s sort) OrderNum() int {
|
||||
func (s findSort) OrderNum() int {
|
||||
if s.Ordering == spec.OrderingAscending {
|
||||
return 1
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue