From 4a330a5706124b63f4c9a76af49bd0d1b94c608c Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Sat, 20 Apr 2024 13:58:44 +0300 Subject: [PATCH] [#72] session: Unify unmarshaling code gopatch: ``` @@ var err, msg, data, t, protojson identifier var obj expression @@ +import "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/rpc/message" -import protojson "google.golang.org/protobuf/encoding/protojson" -msg := obj -if err := protojson.Unmarshal(data, msg); err != nil { -return err -} -return t.FromGRPCMessage(msg) +return message.UnmarshalJSON(t, data, obj) @@ var err, msg, data, t, goproto identifier var obj expression @@ +import "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/rpc/message" -import goproto "google.golang.org/protobuf/proto" -msg := obj -if err := goproto.Unmarshal(data, msg); err != nil { -return err -} -return t.FromGRPCMessage(msg) +return message.Unmarshal(t, data, obj) ``` Signed-off-by: Evgenii Stratonikov --- session/json.go | 57 +++++------------------------------------ session/marshal.go | 64 +++++++--------------------------------------- 2 files changed, 16 insertions(+), 105 deletions(-) diff --git a/session/json.go b/session/json.go index 77cbe10..e157fc3 100644 --- a/session/json.go +++ b/session/json.go @@ -3,7 +3,6 @@ package session import ( "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/rpc/message" session "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/session/grpc" - "google.golang.org/protobuf/encoding/protojson" ) func (c *ObjectSessionContext) MarshalJSON() ([]byte, error) { @@ -27,13 +26,7 @@ func (t *TokenBody) MarshalJSON() ([]byte, error) { } func (t *TokenBody) UnmarshalJSON(data []byte) error { - msg := new(session.SessionToken_Body) - - if err := protojson.Unmarshal(data, msg); err != nil { - return err - } - - return t.FromGRPCMessage(msg) + return message.UnmarshalJSON(t, data, new(session.SessionToken_Body)) } func (t *Token) MarshalJSON() ([]byte, error) { @@ -41,13 +34,7 @@ func (t *Token) MarshalJSON() ([]byte, error) { } func (t *Token) UnmarshalJSON(data []byte) error { - msg := new(session.SessionToken) - - if err := protojson.Unmarshal(data, msg); err != nil { - return err - } - - return t.FromGRPCMessage(msg) + return message.UnmarshalJSON(t, data, new(session.SessionToken)) } func (x *XHeader) MarshalJSON() ([]byte, error) { @@ -55,13 +42,7 @@ func (x *XHeader) MarshalJSON() ([]byte, error) { } func (x *XHeader) UnmarshalJSON(data []byte) error { - msg := new(session.XHeader) - - if err := protojson.Unmarshal(data, msg); err != nil { - return err - } - - return x.FromGRPCMessage(msg) + return message.UnmarshalJSON(x, data, new(session.XHeader)) } func (r *RequestMetaHeader) MarshalJSON() ([]byte, error) { @@ -69,13 +50,7 @@ func (r *RequestMetaHeader) MarshalJSON() ([]byte, error) { } func (r *RequestMetaHeader) UnmarshalJSON(data []byte) error { - msg := new(session.RequestMetaHeader) - - if err := protojson.Unmarshal(data, msg); err != nil { - return err - } - - return r.FromGRPCMessage(msg) + return message.UnmarshalJSON(r, data, new(session.RequestMetaHeader)) } func (r *RequestVerificationHeader) MarshalJSON() ([]byte, error) { @@ -83,13 +58,7 @@ func (r *RequestVerificationHeader) MarshalJSON() ([]byte, error) { } func (r *RequestVerificationHeader) UnmarshalJSON(data []byte) error { - msg := new(session.RequestVerificationHeader) - - if err := protojson.Unmarshal(data, msg); err != nil { - return err - } - - return r.FromGRPCMessage(msg) + return message.UnmarshalJSON(r, data, new(session.RequestVerificationHeader)) } func (r *ResponseMetaHeader) MarshalJSON() ([]byte, error) { @@ -97,13 +66,7 @@ func (r *ResponseMetaHeader) MarshalJSON() ([]byte, error) { } func (r *ResponseMetaHeader) UnmarshalJSON(data []byte) error { - msg := new(session.ResponseMetaHeader) - - if err := protojson.Unmarshal(data, msg); err != nil { - return err - } - - return r.FromGRPCMessage(msg) + return message.UnmarshalJSON(r, data, new(session.ResponseMetaHeader)) } func (r *ResponseVerificationHeader) MarshalJSON() ([]byte, error) { @@ -111,13 +74,7 @@ func (r *ResponseVerificationHeader) MarshalJSON() ([]byte, error) { } func (r *ResponseVerificationHeader) UnmarshalJSON(data []byte) error { - msg := new(session.ResponseVerificationHeader) - - if err := protojson.Unmarshal(data, msg); err != nil { - return err - } - - return r.FromGRPCMessage(msg) + return message.UnmarshalJSON(r, data, new(session.ResponseVerificationHeader)) } func (x *ContainerSessionContext) MarshalJSON() ([]byte, error) { diff --git a/session/marshal.go b/session/marshal.go index 8632398..c9ca34b 100644 --- a/session/marshal.go +++ b/session/marshal.go @@ -4,7 +4,6 @@ import ( "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/rpc/message" session "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/session/grpc" "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/util/proto" - goproto "google.golang.org/protobuf/proto" ) const ( @@ -154,12 +153,7 @@ func (x *XHeader) StableSize() (size int) { } func (x *XHeader) Unmarshal(data []byte) error { - m := new(session.XHeader) - if err := goproto.Unmarshal(data, m); err != nil { - return err - } - - return x.FromGRPCMessage(m) + return message.Unmarshal(x, data, new(session.XHeader)) } func (l *TokenLifetime) StableMarshal(buf []byte) []byte { @@ -193,12 +187,7 @@ func (l *TokenLifetime) StableSize() (size int) { } func (l *TokenLifetime) Unmarshal(data []byte) error { - m := new(session.SessionToken_Body_TokenLifetime) - if err := goproto.Unmarshal(data, m); err != nil { - return err - } - - return l.FromGRPCMessage(m) + return message.Unmarshal(l, data, new(session.SessionToken_Body_TokenLifetime)) } func (c *ObjectSessionContext) StableMarshal(buf []byte) []byte { @@ -234,12 +223,7 @@ func (c *ObjectSessionContext) StableSize() (size int) { } func (c *ObjectSessionContext) Unmarshal(data []byte) error { - m := new(session.ObjectSessionContext) - if err := goproto.Unmarshal(data, m); err != nil { - return err - } - - return c.FromGRPCMessage(m) + return message.Unmarshal(c, data, new(session.ObjectSessionContext)) } const ( @@ -338,12 +322,7 @@ func (t *TokenBody) StableSize() (size int) { } func (t *TokenBody) Unmarshal(data []byte) error { - m := new(session.SessionToken_Body) - if err := goproto.Unmarshal(data, m); err != nil { - return err - } - - return t.FromGRPCMessage(m) + return message.Unmarshal(t, data, new(session.SessionToken_Body)) } func (t *Token) StableMarshal(buf []byte) []byte { @@ -375,12 +354,7 @@ func (t *Token) StableSize() (size int) { } func (t *Token) Unmarshal(data []byte) error { - m := new(session.SessionToken) - if err := goproto.Unmarshal(data, m); err != nil { - return err - } - - return t.FromGRPCMessage(m) + return message.Unmarshal(t, data, new(session.SessionToken)) } func (r *RequestMetaHeader) StableMarshal(buf []byte) []byte { @@ -435,12 +409,7 @@ func (r *RequestMetaHeader) StableSize() (size int) { } func (r *RequestMetaHeader) Unmarshal(data []byte) error { - m := new(session.RequestMetaHeader) - if err := goproto.Unmarshal(data, m); err != nil { - return err - } - - return r.FromGRPCMessage(m) + return message.Unmarshal(r, data, new(session.RequestMetaHeader)) } func (r *RequestVerificationHeader) StableMarshal(buf []byte) []byte { @@ -476,12 +445,7 @@ func (r *RequestVerificationHeader) StableSize() (size int) { } func (r *RequestVerificationHeader) Unmarshal(data []byte) error { - m := new(session.RequestVerificationHeader) - if err := goproto.Unmarshal(data, m); err != nil { - return err - } - - return r.FromGRPCMessage(m) + return message.Unmarshal(r, data, new(session.RequestVerificationHeader)) } func (r *ResponseMetaHeader) StableMarshal(buf []byte) []byte { @@ -532,12 +496,7 @@ func (r *ResponseMetaHeader) StableSize() (size int) { } func (r *ResponseMetaHeader) Unmarshal(data []byte) error { - m := new(session.ResponseMetaHeader) - if err := goproto.Unmarshal(data, m); err != nil { - return err - } - - return r.FromGRPCMessage(m) + return message.Unmarshal(r, data, new(session.ResponseMetaHeader)) } func (r *ResponseVerificationHeader) StableMarshal(buf []byte) []byte { @@ -573,10 +532,5 @@ func (r *ResponseVerificationHeader) StableSize() (size int) { } func (r *ResponseVerificationHeader) Unmarshal(data []byte) error { - m := new(session.ResponseVerificationHeader) - if err := goproto.Unmarshal(data, m); err != nil { - return err - } - - return r.FromGRPCMessage(m) + return message.Unmarshal(r, data, new(session.ResponseVerificationHeader)) } -- 2.45.2