From bb68cc6b3a5917ae9385de5e66ecbfb3c5dc6d3c Mon Sep 17 00:00:00 2001 From: Ben Cessa Date: Mon, 3 Jul 2023 12:27:59 -0600 Subject: [PATCH] Replace deprecated AMQP package More information: https://github.com/streadway/amqp/pull/535 --- .golangci.yml | 14 +++++--------- amqp/consumer.go | 2 +- amqp/producer.go | 2 +- amqp/publisher.go | 5 +++-- amqp/session.go | 2 +- go.mod | 11 +++++------ go.sum | 27 +++++++++++---------------- net/rpc/ws/proxy.go | 1 + storage/orm/operator.go | 15 +++++++++++---- storage/orm/operator_test.go | 4 ++-- storage/orm/utils.go | 21 --------------------- 11 files changed, 41 insertions(+), 63 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 00948a3..412b05d 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -45,12 +45,12 @@ linters: - predeclared - exportloopref - whitespace + - nestif + - funlen # Deprecated linters - #- wrapcheck - #- nestif - #- funlen - #- ifshort - #- varcheck + # - wrapcheck + # - ifshort + # - varcheck disable: - deadcode - unused @@ -103,10 +103,6 @@ linters-settings: goconst: min-len: 3 min-occurrences: 5 - depguard: - list-type: blacklist - include-go-root: false - packages: [] misspell: locale: US ignore-words: [] diff --git a/amqp/consumer.go b/amqp/consumer.go index def9262..009e04a 100644 --- a/amqp/consumer.go +++ b/amqp/consumer.go @@ -5,7 +5,7 @@ import ( "sync" "time" - driver "github.com/streadway/amqp" + driver "github.com/rabbitmq/amqp091-go" "go.bryk.io/pkg/errors" xlog "go.bryk.io/pkg/log" ) diff --git a/amqp/producer.go b/amqp/producer.go index 56ea2c4..0cc845c 100644 --- a/amqp/producer.go +++ b/amqp/producer.go @@ -24,7 +24,7 @@ type Producer struct { // Add the timestamp value when creating a new message. SetTime bool - // Add a randomly-generated, unique UUID to each message. + // Add a randomly-generated, unique ID to each message. SetID bool } diff --git a/amqp/publisher.go b/amqp/publisher.go index d8b43c5..1d61787 100644 --- a/amqp/publisher.go +++ b/amqp/publisher.go @@ -7,7 +7,7 @@ import ( "time" "github.com/google/uuid" - driver "github.com/streadway/amqp" + driver "github.com/rabbitmq/amqp091-go" "go.bryk.io/pkg/errors" xlog "go.bryk.io/pkg/log" ) @@ -198,7 +198,8 @@ func (p *Publisher) UnsafePush(msg Message, opts MessageOptions) error { } p.log.Debug("publishing message") - return p.session.channel.Publish( + return p.session.channel.PublishWithContext( + context.TODO(), opts.Exchange, opts.RoutingKey, opts.Mandatory, diff --git a/amqp/session.go b/amqp/session.go index 57e9dcd..6c8686d 100644 --- a/amqp/session.go +++ b/amqp/session.go @@ -7,7 +7,7 @@ import ( "sync" "time" - driver "github.com/streadway/amqp" + driver "github.com/rabbitmq/amqp091-go" "go.bryk.io/pkg/errors" xlog "go.bryk.io/pkg/log" ) diff --git a/go.mod b/go.mod index f0c3b64..2ba0b88 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,7 @@ require ( github.com/briandowns/spinner v1.23.0 github.com/chzyer/readline v1.5.1 github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 - github.com/getsentry/sentry-go v0.21.0 + github.com/getsentry/sentry-go v0.22.0 github.com/google/uuid v1.3.0 github.com/gorilla/handlers v1.5.1 github.com/gorilla/websocket v1.5.0 @@ -18,15 +18,15 @@ require ( github.com/piprate/json-gold v0.5.0 github.com/prometheus/client_golang v1.16.0 github.com/prometheus/client_model v0.4.0 + github.com/rabbitmq/amqp091-go v1.8.1 github.com/rs/zerolog v1.29.1 github.com/sirupsen/logrus v1.9.3 github.com/soheilhy/cmux v0.1.5 github.com/spf13/cobra v1.7.0 github.com/spf13/viper v1.16.0 - github.com/streadway/amqp v1.0.0 github.com/stretchr/testify v1.8.4 go.bryk.io/miracl v0.5.1 - go.mongodb.org/mongo-driver v1.11.7 + go.mongodb.org/mongo-driver v1.12.0 go.opentelemetry.io/contrib/instrumentation/go.mongodb.org/mongo-driver/mongo/otelmongo v0.42.0 go.opentelemetry.io/contrib/instrumentation/host v0.42.0 go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.42.0 @@ -50,8 +50,8 @@ require ( golang.org/x/term v0.9.0 golang.org/x/time v0.3.0 google.golang.org/genproto/googleapis/api v0.0.0-20230530153820-e85fd2cbaebc - google.golang.org/grpc v1.56.0 - google.golang.org/protobuf v1.30.0 + google.golang.org/grpc v1.56.1 + google.golang.org/protobuf v1.31.0 gopkg.in/yaml.v3 v3.0.1 storj.io/drpc v0.0.33 ) @@ -81,7 +81,6 @@ require ( github.com/mitchellh/mapstructure v1.5.0 // indirect github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe // indirect github.com/pelletier/go-toml/v2 v2.0.8 // indirect - github.com/pkg/errors v0.9.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect github.com/pquerna/cachecontrol v0.0.0-20180517163645-1555304b9b35 // indirect diff --git a/go.sum b/go.sum index da4a56f..8d3994d 100644 --- a/go.sum +++ b/go.sum @@ -243,8 +243,8 @@ github.com/frankban/quicktest v1.14.4 h1:g2rn0vABPOOXmZUj+vbmUp0lPoXEMuhTpIluN0X github.com/frankban/quicktest v1.14.4/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0= github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY= github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw= -github.com/getsentry/sentry-go v0.21.0 h1:c9l5F1nPF30JIppulk4veau90PK6Smu3abgVtVQWon4= -github.com/getsentry/sentry-go v0.21.0/go.mod h1:lc76E2QywIyW8WuBnwl8Lc4bkmQH4+w1gwTf25trprY= +github.com/getsentry/sentry-go v0.22.0 h1:XNX9zKbv7baSEI65l+H1GEJgSeIC1c7EN5kluWaP6dM= +github.com/getsentry/sentry-go v0.22.0/go.mod h1:lc76E2QywIyW8WuBnwl8Lc4bkmQH4+w1gwTf25trprY= github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= github.com/gin-gonic/gin v1.8.1/go.mod h1:ji8BvRH1azfM+SYow9zQ6SZMvR8qOMZHmsCuWR9tTTk= @@ -458,6 +458,8 @@ github.com/prometheus/common v0.42.0 h1:EKsfXEYo4JpWMHH5cg+KOUWeuJSov1Id8zGR8eeI github.com/prometheus/common v0.42.0/go.mod h1:xBwqVerjNdUDjgODMpudtOMwlOwf2SaTr1yjz4b7Zbc= github.com/prometheus/procfs v0.10.1 h1:kYK1Va/YMlutzCGazswoHKo//tZVlFpKYh+PymziUAg= github.com/prometheus/procfs v0.10.1/go.mod h1:nwNm2aOCAYw8uTR/9bWRREkZFxAUcWzPHWJq+XBB/FM= +github.com/rabbitmq/amqp091-go v1.8.1 h1:RejT1SBUim5doqcL6s7iN6SBmsQqyTgXb1xMlH0h1hA= +github.com/rabbitmq/amqp091-go v1.8.1/go.mod h1:+jPrT9iY2eLjRaMSRHUhc3z14E/l85kv/f+6luSD3pc= github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ= github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8= @@ -492,8 +494,6 @@ github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/spf13/viper v1.16.0 h1:rGGH0XDZhdUOryiDWjmIvUSWpbNqisK8Wk0Vyefw8hc= github.com/spf13/viper v1.16.0/go.mod h1:yg78JgCJcbrQOvV9YLXgkLaZqUidkY9K+Dd1FofRzQg= -github.com/streadway/amqp v1.0.0 h1:kuuDrUJFZL1QYL9hUNuCxNObNzB0bV/ZG5jV3RWAQgo= -github.com/streadway/amqp v1.0.0/go.mod h1:AZpEONHx3DKn8O/DFsRAY58/XVQiIPMTMB1SddzLXVw= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= @@ -502,7 +502,6 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= -github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= @@ -514,8 +513,6 @@ github.com/subosito/gotenv v1.4.2 h1:X1TuBLAMDFbaTAChgCBLu3DU3UPyELpnF2jjJ2cz/S8 github.com/subosito/gotenv v1.4.2/go.mod h1:ayKnFf/c6rvx/2iiLrJUk1e6plDbT3edrFNGqEflhK0= github.com/tdewolff/minify/v2 v2.12.4/go.mod h1:h+SRvSIX3kwgwTFOpSckvSxgax3uy8kZTSF1Ojrr3bk= github.com/tdewolff/parse/v2 v2.6.4/go.mod h1:woz0cgbLwFdtbjJu8PIKxhW05KplTFQkOdX78o+Jgrs= -github.com/tidwall/pretty v1.0.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4= -github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk= github.com/tklauser/go-sysconf v0.3.11 h1:89WgdJhk5SNwJfu+GKyYveZ4IaJ7xAkecBo+KdJV0CM= github.com/tklauser/go-sysconf v0.3.11/go.mod h1:GqXfhXY3kiPa0nAXPDIQIWzJbMCB7AmcWpGR8lSZfqI= github.com/tklauser/numcpus v0.6.0 h1:kebhY2Qt+3U6RNK7UqpYNA+tJ23IBEGKkB7JQBfDYms= @@ -529,10 +526,8 @@ github.com/vmihailenco/msgpack/v5 v5.3.5/go.mod h1:7xyJ9e+0+9SaZT0Wt1RGleJXzli6Q 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.1/go.mod h1:RaEWvsqvNKKvBPvcKeFjrG2cJqOkHTiyTpzz23ni57g= github.com/xdg-go/scram v1.1.2 h1:FHX5I5B4i4hKRVRBCFRxq1iQRej7WO3hhBuJf+UUySY= github.com/xdg-go/scram v1.1.2/go.mod h1:RT/sEzTbU5y00aCK8UOx6R7YryM0iF1N2MOmC3kKLN4= -github.com/xdg-go/stringprep v1.0.3/go.mod h1:W3f5j4i+9rC0kuIEJL0ky1VpHXQU3ocBgklLGvcBnW8= github.com/xdg-go/stringprep v1.0.4 h1:XLI/Ng3O1Atzq0oBs3TWm+5ZVgkq2aqdlvP9JtoZ6c8= github.com/xdg-go/stringprep v1.0.4/go.mod h1:mPGuuIYwz7CmR2bT9j4GbQqutWS1zV24gijq1dTyGkM= github.com/xhit/go-str2duration v1.2.0/go.mod h1:3cPSlfZlUHVlneIVfePFWcJZsuwf+P1v2SRTV4cUmp4= @@ -556,8 +551,8 @@ go.etcd.io/etcd/api/v3 v3.5.9/go.mod h1:uyAal843mC8uUVSLWz6eHa/d971iDGnCRpmKd2Z+ go.etcd.io/etcd/client/pkg/v3 v3.5.9/go.mod h1:y+CzeSmkMpWN2Jyu1npecjB9BBnABxGM4pN8cGuJeL4= go.etcd.io/etcd/client/v2 v2.305.7/go.mod h1:GQGT5Z3TBuAQGvgPfhR7VPySu/SudxmEkRq9BgzFU6s= go.etcd.io/etcd/client/v3 v3.5.9/go.mod h1:i/Eo5LrZ5IKqpbtpPDuaUnDOUv471oDg8cjQaUr2MbA= -go.mongodb.org/mongo-driver v1.11.7 h1:LIwYxASDLGUg/8wOhgOOZhX8tQa/9tgZPgzZoVqJvcs= -go.mongodb.org/mongo-driver v1.11.7/go.mod h1:G9TgswdsWjX4tmDA5zfs2+6AEPpYJwqblyjsfuh8oXY= +go.mongodb.org/mongo-driver v1.12.0 h1:aPx33jmn/rQuJXPQLZQ8NtfPQG8CaqgLThFtqRb0PiE= +go.mongodb.org/mongo-driver v1.12.0/go.mod h1:AZkxhPnFJUoH7kZlFkVKucV20K387miPfm7oimrSmK0= go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU= go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8= go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= @@ -721,7 +716,6 @@ golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E= golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y= @@ -793,6 +787,7 @@ golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ= +golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/text v0.10.0 h1:UpjohKhiEgNc0CSauXmwYftY1+LlaC75SJwh0SgCX58= golang.org/x/text v0.10.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= @@ -950,8 +945,8 @@ google.golang.org/grpc v1.35.0/go.mod h1:qjiiYl8FncCW8feJPdyg3v6XW24KsRHe+dy9BAG google.golang.org/grpc v1.36.0/go.mod h1:qjiiYl8FncCW8feJPdyg3v6XW24KsRHe+dy9BAGRRjU= google.golang.org/grpc v1.40.0/go.mod h1:ogyxbiOoUXAkP+4+xa6PZSE9DZgIHtSpzjDTB9KAK34= google.golang.org/grpc v1.42.0/go.mod h1:k+4IHHFw41K8+bbowsex27ge2rCb65oeWqe4jJ590SU= -google.golang.org/grpc v1.56.0 h1:+y7Bs8rtMd07LeXmL3NxcTLn7mUkbKZqEpPhMNkwJEE= -google.golang.org/grpc v1.56.0/go.mod h1:I9bI3vqKfayGqPUAwGdOSu7kt6oIJLixfffKrpXqQ9s= +google.golang.org/grpc v1.56.1 h1:z0dNfjIl0VpaZ9iSVjA6daGatAYwPGstTjt5vkRMFkQ= +google.golang.org/grpc v1.56.1/go.mod h1:I9bI3vqKfayGqPUAwGdOSu7kt6oIJLixfffKrpXqQ9s= google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= @@ -965,8 +960,8 @@ google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlba google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= -google.golang.org/protobuf v1.30.0 h1:kPPoIgf3TsEvrm0PFe15JQ+570QVxYzEvvHqChK+cng= -google.golang.org/protobuf v1.30.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= +google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8= +google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= diff --git a/net/rpc/ws/proxy.go b/net/rpc/ws/proxy.go index b77302f..f1920ad 100644 --- a/net/rpc/ws/proxy.go +++ b/net/rpc/ws/proxy.go @@ -104,6 +104,7 @@ func (p *Proxy) prepareRequest(incoming http.Request, outgoing *http.Request) { } } +// nolint: funlen func (p *Proxy) proxy(w http.ResponseWriter, r *http.Request) { var responseHeader http.Header diff --git a/storage/orm/operator.go b/storage/orm/operator.go index c7da99e..31e09ee 100644 --- a/storage/orm/operator.go +++ b/storage/orm/operator.go @@ -29,10 +29,15 @@ type Operator struct { // Remember to call the 'Close' method to free resources when the // operator is no longer needed. func NewOperator(db string, opts *options.ClientOptions) (*Operator, error) { - // Use custom registry by default - if opts.Registry == nil { - opts.Registry = bsonRegistry() - } + // By default: + // - fallback to "json" struct tags if "bson" struct tags are missing + // - marshal `nil` maps as empty BSON documents + // - marshal `nil` slices as empty BSON arrays + opts.SetBSONOptions(&options.BSONOptions{ + UseJSONStructTags: true, + NilMapAsEmpty: true, + NilSliceAsEmpty: true, + }) // Validate client options var err error @@ -88,6 +93,8 @@ func (op *Operator) Tx(body TransactionBody, opts *options.TransactionOptions) e // 2. Default BSON encoder rules // 3. Use `bson` tags, if available // 4. Use `json` tags, if available +// 5. Marshal `nil` maps as empty BSON documents +// 6. Marshal `nil` slices as empty BSON arrays // https://pkg.go.dev/go.mongodb.org/mongo-driver/bson func (op *Operator) Model(name string) *Model { return &Model{ diff --git a/storage/orm/operator_test.go b/storage/orm/operator_test.go index 4790c10..a0cfedc 100644 --- a/storage/orm/operator_test.go +++ b/storage/orm/operator_test.go @@ -323,7 +323,7 @@ func TestOperator(t *testing.T) { // Set transaction options opts := options.Transaction() opts.SetReadConcern(readconcern.Snapshot()) - opts.SetWriteConcern(writeconcern.New(writeconcern.WMajority())) + opts.SetWriteConcern(writeconcern.Majority()) // Start transaction err := op.Tx(func(tx *Transaction) error { @@ -399,7 +399,7 @@ func ExampleOperator_Tx() { // Set transaction options opts := options.Transaction() opts.SetReadConcern(readconcern.Snapshot()) - opts.SetWriteConcern(writeconcern.New(writeconcern.WMajority())) + opts.SetWriteConcern(writeconcern.Majority()) // Complex multi-collection operation complexOperation := func(tx *Transaction) error { diff --git a/storage/orm/utils.go b/storage/orm/utils.go index efc2009..79f6f9d 100644 --- a/storage/orm/utils.go +++ b/storage/orm/utils.go @@ -5,7 +5,6 @@ import ( "reflect" "go.mongodb.org/mongo-driver/bson" - "go.mongodb.org/mongo-driver/bson/bsoncodec" "go.mongodb.org/mongo-driver/bson/primitive" ) @@ -42,23 +41,3 @@ func checkType(el interface{}, expected reflect.Kind, desc string) error { } return nil } - -// Return a BSON codec that honor JSON tags by default. -// https://pkg.go.dev/go.mongodb.org/mongo-driver/bson#hdr-Structs -func bsonRegistry() *bsoncodec.Registry { - // Register default encoders/decoders - // https://pkg.go.dev/go.mongodb.org/mongo-driver/bson/bsoncodec#hdr-DefaultValueEncoders_and_DefaultValueDecoders - rb := bsoncodec.NewRegistryBuilder() - dve := bsoncodec.DefaultValueEncoders{} - dvd := bsoncodec.DefaultValueDecoders{} - dvd.RegisterDefaultDecoders(rb) - dve.RegisterDefaultEncoders(rb) - - // Register custom struct encoder/decoder - structcodec, _ := bsoncodec.NewStructCodec(bsoncodec.JSONFallbackStructTagParser) - rb.RegisterDefaultEncoder(reflect.Struct, structcodec) - rb.RegisterDefaultDecoder(reflect.Struct, structcodec) - - // Return registry instance - return rb.Build() -}