From 94f1817580e33051401aaf3f6243e9f92ab616d6 Mon Sep 17 00:00:00 2001 From: kain Date: Thu, 2 Nov 2023 18:24:11 +0800 Subject: [PATCH] update: [Rest] PendingDto changed to bson.Marshal and bson.Unmarshal --- go.mod | 12 ++++++----- go.sum | 14 +++++++++++++ rest/controller.go | 4 ++-- rest/service.go | 51 +++++++++++++++++++++++++++------------------- 4 files changed, 53 insertions(+), 28 deletions(-) diff --git a/go.mod b/go.mod index 3a40e7f..1c78ad9 100644 --- a/go.mod +++ b/go.mod @@ -9,12 +9,12 @@ require ( github.com/go-faker/faker/v4 v4.2.0 github.com/go-playground/validator/v10 v10.15.5 github.com/golang-jwt/jwt/v5 v5.0.0 - github.com/google/uuid v1.3.1 + github.com/google/uuid v1.4.0 github.com/hertz-contrib/binding v0.1.0 github.com/hertz-contrib/requestid v1.1.0 github.com/nats-io/nats.go v1.31.0 github.com/nats-io/nkeys v0.4.6 - github.com/redis/go-redis/v9 v9.2.1 + github.com/redis/go-redis/v9 v9.3.0 github.com/stretchr/testify v1.8.4 go.mongodb.org/mongo-driver v1.12.1 golang.org/x/crypto v0.14.0 @@ -26,18 +26,18 @@ require ( github.com/bytedance/go-tagexpr/v2 v2.9.11 // indirect github.com/cespare/xxhash/v2 v2.2.0 // indirect github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect - github.com/chenzhuoyu/iasm v0.9.0 // indirect + github.com/chenzhuoyu/iasm v0.9.1 // indirect github.com/cloudwego/netpoll v0.5.1 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect - github.com/fsnotify/fsnotify v1.6.0 // indirect + github.com/fsnotify/fsnotify v1.7.0 // indirect github.com/gabriel-vasile/mimetype v1.4.3 // indirect github.com/go-playground/locales v0.14.1 // indirect github.com/go-playground/universal-translator v0.18.1 // indirect github.com/golang/protobuf v1.5.3 // indirect github.com/golang/snappy v0.0.4 // indirect github.com/google/go-cmp v0.5.6 // indirect - github.com/klauspost/compress v1.17.1 // indirect + github.com/klauspost/compress v1.17.2 // indirect github.com/klauspost/cpuid/v2 v2.2.5 // indirect github.com/kr/pretty v0.3.0 // indirect github.com/leodido/go-urn v1.2.4 // indirect @@ -50,6 +50,8 @@ require ( github.com/tidwall/match v1.1.1 // indirect github.com/tidwall/pretty v1.2.1 // indirect github.com/twitchyliquid64/golang-asm v0.15.1 // indirect + github.com/vmihailenco/msgpack/v5 v5.4.1 // indirect + github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect github.com/xdg-go/pbkdf2 v1.0.0 // indirect github.com/xdg-go/scram v1.1.2 // indirect github.com/xdg-go/stringprep v1.0.4 // indirect diff --git a/go.sum b/go.sum index 3070965..0918dda 100644 --- a/go.sum +++ b/go.sum @@ -27,6 +27,8 @@ github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d h1:77cEq6EriyTZ github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d/go.mod h1:8EPpVsBuRksnlj1mLy4AWzRNQYxauNi62uWcE3to6eA= github.com/chenzhuoyu/iasm v0.9.0 h1:9fhXjVzq5hUy2gkhhgHl95zG2cEAhw9OSGs8toWWAwo= github.com/chenzhuoyu/iasm v0.9.0/go.mod h1:Xjy2NpN3h7aUqeqM+woSuuvxmIe6+DDsiNLIrkAmYog= +github.com/chenzhuoyu/iasm v0.9.1 h1:tUHQJXo3NhBqw6s33wkGn9SP3bvrWLdlVIJ3hQBL7P0= +github.com/chenzhuoyu/iasm v0.9.1/go.mod h1:Xjy2NpN3h7aUqeqM+woSuuvxmIe6+DDsiNLIrkAmYog= github.com/cloudwego/hertz v0.4.2/go.mod h1:K1U0RlU07CDeBINfHNbafH/3j9uSgIW8otbjUys3OPY= github.com/cloudwego/hertz v0.7.1 h1:4M8l4zvAE6yNxzfozpxHLMnRSRRPslKNw/McH5/qFns= github.com/cloudwego/hertz v0.7.1/go.mod h1:WliNtVbwihWHHgAaIQEbVXl0O3aWj0ks1eoPrcEAnjs= @@ -43,6 +45,8 @@ github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cu github.com/fsnotify/fsnotify v1.5.4/go.mod h1:OVB6XrOHzAwXMpEM7uPOzcehqUV2UqJxmVXmkdnm1bU= github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY= github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw= +github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA= +github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM= github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0= github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk= github.com/go-faker/faker/v4 v4.2.0 h1:dGebOupKwssrODV51E0zbMrv5e2gO9VWSLNC1WDCpWg= @@ -71,6 +75,8 @@ github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4= github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/google/uuid v1.4.0 h1:MtMxsa51/r9yyhkyLsVeVt0B+BGQZzpQiTQ4eHZ8bc4= +github.com/google/uuid v1.4.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8= github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= github.com/henrylee2cn/ameda v1.4.8/go.mod h1:liZulR8DgHxdK+MEwvZIylGnmcjzQ6N6f2PlWe7nEO4= @@ -85,6 +91,8 @@ github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfV github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk= github.com/klauspost/compress v1.17.1 h1:NE3C767s2ak2bweCZo3+rdP4U/HoyVXLv/X9f2gPS5g= github.com/klauspost/compress v1.17.1/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE= +github.com/klauspost/compress v1.17.2 h1:RlWWUY/Dr4fL8qk9YG7DTZ7PDgME2V4csBXA8L/ixi4= +github.com/klauspost/compress v1.17.2/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE= github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= github.com/klauspost/cpuid/v2 v2.2.5 h1:0E5MSMDEoAulmXNFquVs//DdoomxaoTY1kUhbc/qbZg= github.com/klauspost/cpuid/v2 v2.2.5/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws= @@ -118,6 +126,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/redis/go-redis/v9 v9.2.1 h1:WlYJg71ODF0dVspZZCpYmoF1+U1Jjk9Rwd7pq6QmlCg= github.com/redis/go-redis/v9 v9.2.1/go.mod h1:hdY0cQFCN4fnSYT6TkisLufl/4W5UIXyv0b/CLO2V2M= +github.com/redis/go-redis/v9 v9.3.0 h1:RiVDjmig62jIWp7Kk4XVLs0hzV6pI3PyTnnL0cnn0u0= +github.com/redis/go-redis/v9 v9.3.0/go.mod h1:hdY0cQFCN4fnSYT6TkisLufl/4W5UIXyv0b/CLO2V2M= github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc= github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8= github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE= @@ -150,6 +160,10 @@ github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4= github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= +github.com/vmihailenco/msgpack/v5 v5.4.1 h1:cQriyiUvjTwOHg8QZaPihLWeRAAVoCpE00IUPn0Bjt8= +github.com/vmihailenco/msgpack/v5 v5.4.1/go.mod h1:GaZTsDaehaPpQVyxrf5mtQlH+pc21PIudVV/E3rRQok= +github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g= +github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds= github.com/xdg-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c= github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI= github.com/xdg-go/scram v1.1.2 h1:FHX5I5B4i4hKRVRBCFRxq1iQRej7WO3hhBuJf+UUySY= diff --git a/rest/controller.go b/rest/controller.go index 28ab72f..f0e79b5 100644 --- a/rest/controller.go +++ b/rest/controller.go @@ -550,8 +550,8 @@ type SortDto struct { } type SortDtoData struct { - Key string `json:"key" vd:"required"` - Values []primitive.ObjectID `json:"values" vd:"gt=0,dive,mongodb"` + Key string `json:"key" vd:"required"` + Values []primitive.ObjectID `json:"values" vd:"gt=0,dive,mongodb"` } func (x *Controller) Sort(ctx context.Context, c *app.RequestContext) { diff --git a/rest/service.go b/rest/service.go index 23de70c..366c5c7 100644 --- a/rest/service.go +++ b/rest/service.go @@ -47,7 +47,7 @@ const ( ActionSort = 8 ) -func (x *Service) Create(ctx context.Context, name string, doc M) (result interface{}, err error) { +func (x *Service) Create(ctx context.Context, name string, doc interface{}) (result interface{}, err error) { if result, err = x.Db.Collection(name).InsertOne(ctx, doc); err != nil { return } @@ -107,7 +107,7 @@ func (x *Service) FindOne(ctx context.Context, name string, filter M, option *op return } -func (x *Service) Update(ctx context.Context, name string, filter M, update M) (result interface{}, err error) { +func (x *Service) Update(ctx context.Context, name string, filter M, update interface{}) (result interface{}, err error) { if result, err = x.Db.Collection(name).UpdateMany(ctx, filter, update); err != nil { return } @@ -122,7 +122,7 @@ func (x *Service) Update(ctx context.Context, name string, filter M, update M) ( return } -func (x *Service) UpdateById(ctx context.Context, name string, id primitive.ObjectID, update M) (result interface{}, err error) { +func (x *Service) UpdateById(ctx context.Context, name string, id primitive.ObjectID, update interface{}) (result interface{}, err error) { filter := M{"_id": id} if result, err = x.Db.Collection(name).UpdateOne(ctx, filter, update); err != nil { return @@ -138,7 +138,7 @@ func (x *Service) UpdateById(ctx context.Context, name string, id primitive.Obje return } -func (x *Service) Replace(ctx context.Context, name string, id primitive.ObjectID, doc M) (result interface{}, err error) { +func (x *Service) Replace(ctx context.Context, name string, id primitive.ObjectID, doc interface{}) (result interface{}, err error) { filter := M{"_id": id} if result, err = x.Db.Collection(name).ReplaceOne(ctx, filter, doc); err != nil { return @@ -241,11 +241,11 @@ func (x *Service) Transaction(ctx context.Context, txn string) { } type PendingDto struct { - Action int `json:"action"` - Name string `json:"name"` - Id primitive.ObjectID `json:"id,omitempty"` - Filter M `json:"filter,omitempty"` - Data interface{} `json:"data,omitempty"` + Action int `bson:"action"` + Name string `bson:"name"` + Id primitive.ObjectID `bson:"id,omitempty"` + Filter M `bson:"filter,omitempty"` + Data interface{} `bson:"data,omitempty"` } func (x *Service) TxnNotExists(ctx context.Context, key string) (err error) { @@ -265,7 +265,7 @@ func (x *Service) Pending(ctx context.Context, txn string, dto PendingDto) (err return } var b []byte - if b, err = sonic.Marshal(dto); err != nil { + if b, err = bson.Marshal(dto); err != nil { return } if err = x.RDb.LPush(ctx, key, b).Err(); err != nil { @@ -309,7 +309,7 @@ func (x *Service) Commit(ctx context.Context, txn string) (_ interface{}, err er return } var dto PendingDto - if err = sonic.Unmarshal(b, &dto); err != nil { + if err = bson.Unmarshal(b, &dto); err != nil { return } var r interface{} @@ -326,27 +326,36 @@ func (x *Service) Commit(ctx context.Context, txn string) (_ interface{}, err er func (x *Service) Invoke(ctx context.Context, dto PendingDto) (_ interface{}, _ error) { switch dto.Action { case ActionCreate: - return x.Create(ctx, dto.Name, dto.Data.(M)) + return x.Create(ctx, dto.Name, dto.Data) case ActionBulkCreate: - return x.BulkCreate(ctx, dto.Name, dto.Data.([]interface{})) + return x.BulkCreate(ctx, dto.Name, dto.Data.(primitive.A)) case ActionUpdate: - return x.Update(ctx, dto.Name, dto.Filter, dto.Data.(M)) + return x.Update(ctx, dto.Name, dto.Filter, dto.Data) case ActionUpdateById: - return x.UpdateById(ctx, dto.Name, dto.Id, dto.Data.(M)) + return x.UpdateById(ctx, dto.Name, dto.Id, dto.Data) case ActionReplace: - return x.Replace(ctx, dto.Name, dto.Id, dto.Data.(M)) + return x.Replace(ctx, dto.Name, dto.Id, dto.Data) case ActionDelete: return x.Delete(ctx, dto.Name, dto.Id, true) case ActionBulkDelete: return x.BulkDelete(ctx, dto.Name, dto.Filter, true) case ActionSort: - data := dto.Data.(M) + data := dto.Data.(primitive.D) + var key string var ids []primitive.ObjectID - for _, v := range data["values"].([]interface{}) { - id, _ := primitive.ObjectIDFromHex(v.(string)) - ids = append(ids, id) + for _, v := range data { + switch v.Key { + case "key": + key = v.Value.(string) + break + case "values": + for _, id := range v.Value.(primitive.A) { + ids = append(ids, id.(primitive.ObjectID)) + } + break + } } - return x.Sort(ctx, dto.Name, data["key"].(string), ids) + return x.Sort(ctx, dto.Name, key, ids) } return }