session: Unify unmarshaling code #72

Merged
fyrchik merged 1 commit from fyrchik/frostfs-api-go:unify-json-handling into master 2024-04-22 15:01:22 +00:00
2 changed files with 16 additions and 105 deletions

View file

@ -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) {

View file

@ -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))
}