From 4ec7fd94e0cf0e174d0e846aa6ee6af4a12b6fd7 Mon Sep 17 00:00:00 2001 From: Nam Huynh Date: Tue, 3 Jan 2023 15:22:57 +0700 Subject: [PATCH] add module jsoniter && helper function for models --- go.mod | 3 +++ go.sum | 9 +++++++++ model.go | 28 +++++++++++++++++++++++++++- natsio.go | 4 ++++ 4 files changed, 43 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 0c5bc39..ade3eb7 100644 --- a/go.mod +++ b/go.mod @@ -11,6 +11,9 @@ require ( require ( github.com/golang/protobuf v1.5.2 // indirect github.com/google/go-cmp v0.5.8 // indirect + github.com/json-iterator/go v1.1.12 // indirect + github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect + github.com/modern-go/reflect2 v1.0.2 // indirect github.com/nats-io/nats-server/v2 v2.6.1 // indirect github.com/nats-io/nkeys v0.3.0 // indirect github.com/nats-io/nuid v1.0.1 // indirect diff --git a/go.sum b/go.sum index eace857..95d1faf 100644 --- a/go.sum +++ b/go.sum @@ -19,6 +19,9 @@ github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg= github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= +github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= github.com/klauspost/compress v1.13.4/go.mod h1:8dP1Hq4DHOhN9w426knH3Rhby4rFm6D8eO+e+Dq5Gzg= github.com/klauspost/compress v1.13.6 h1:P76CopJELS0TiO2mebmnzgWaajssP/EszplttgQxcgc= github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk= @@ -27,6 +30,11 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/minio/highwayhash v1.0.1 h1:dZ6IIu8Z14VlC0VpfKofAhCy74wu/Qb5gcn52yWoz/0= github.com/minio/highwayhash v1.0.1/go.mod h1:BQskDq+xkJ12lmlUUi7U0M5Swg3EWR+dLTk+kldvVxY= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= +github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc= github.com/nats-io/jwt v1.2.2 h1:w3GMTO969dFg+UOKTmmyuu7IGdusK+7Ytlt//OYH/uU= github.com/nats-io/jwt v1.2.2/go.mod h1:/xX356yQA6LuXI9xWW7mZNpxgF2mBmGecH+Fj34sP5Q= @@ -46,6 +54,7 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +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.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= diff --git a/model.go b/model.go index 158308d..e102f0f 100644 --- a/model.go +++ b/model.go @@ -1,6 +1,10 @@ package natsio -import "go.mongodb.org/mongo-driver/bson/primitive" +import ( + "errors" + + "go.mongodb.org/mongo-driver/bson/primitive" +) // // Common @@ -22,6 +26,17 @@ type User struct { Avatar *FilePhoto `json:"avatar"` } +func ParseUser(user User, result interface{}) (err error) { + b := InterfaceToBytes(user) + if len(b) > 0 { + err = json.Unmarshal(b, &result) + } else { + err = errors.New("[natsio.ParseUser] cannot read data") + } + + return +} + // // FILE PHOTO // @@ -42,3 +57,14 @@ type FileDimensions struct { Small *FileSize `json:"sm"` Medium *FileSize `json:"md"` } + +func ParsePhoto(photo FilePhoto, result interface{}) (err error) { + b := InterfaceToBytes(photo) + if len(b) > 0 { + err = json.Unmarshal(b, &result) + } else { + err = errors.New("[natsio.ParsePhoto] cannot read data") + } + + return +} diff --git a/natsio.go b/natsio.go index 17df5e0..d7351dd 100644 --- a/natsio.go +++ b/natsio.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" + jsoniter "github.com/json-iterator/go" "github.com/nats-io/nats.go" ) @@ -23,6 +24,9 @@ type JetStream struct { var ( natsServer Server natsJetStream JetStream + + // builtin json alternative + json = jsoniter.ConfigCompatibleWithStandardLibrary ) // Connect ...