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 ( import (
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/rpc/message" "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/rpc/message"
session "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/session/grpc" session "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/session/grpc"
"google.golang.org/protobuf/encoding/protojson"
) )
func (c *ObjectSessionContext) MarshalJSON() ([]byte, error) { func (c *ObjectSessionContext) MarshalJSON() ([]byte, error) {
@ -27,13 +26,7 @@ func (t *TokenBody) MarshalJSON() ([]byte, error) {
} }
func (t *TokenBody) UnmarshalJSON(data []byte) error { func (t *TokenBody) UnmarshalJSON(data []byte) error {
msg := new(session.SessionToken_Body) return message.UnmarshalJSON(t, data, new(session.SessionToken_Body))
if err := protojson.Unmarshal(data, msg); err != nil {
return err
}
return t.FromGRPCMessage(msg)
} }
func (t *Token) MarshalJSON() ([]byte, error) { func (t *Token) MarshalJSON() ([]byte, error) {
@ -41,13 +34,7 @@ func (t *Token) MarshalJSON() ([]byte, error) {
} }
func (t *Token) UnmarshalJSON(data []byte) error { func (t *Token) UnmarshalJSON(data []byte) error {
msg := new(session.SessionToken) return message.UnmarshalJSON(t, data, new(session.SessionToken))
if err := protojson.Unmarshal(data, msg); err != nil {
return err
}
return t.FromGRPCMessage(msg)
} }
func (x *XHeader) MarshalJSON() ([]byte, error) { func (x *XHeader) MarshalJSON() ([]byte, error) {
@ -55,13 +42,7 @@ func (x *XHeader) MarshalJSON() ([]byte, error) {
} }
func (x *XHeader) UnmarshalJSON(data []byte) error { func (x *XHeader) UnmarshalJSON(data []byte) error {
msg := new(session.XHeader) return message.UnmarshalJSON(x, data, new(session.XHeader))
if err := protojson.Unmarshal(data, msg); err != nil {
return err
}
return x.FromGRPCMessage(msg)
} }
func (r *RequestMetaHeader) MarshalJSON() ([]byte, error) { func (r *RequestMetaHeader) MarshalJSON() ([]byte, error) {
@ -69,13 +50,7 @@ func (r *RequestMetaHeader) MarshalJSON() ([]byte, error) {
} }
func (r *RequestMetaHeader) UnmarshalJSON(data []byte) error { func (r *RequestMetaHeader) UnmarshalJSON(data []byte) error {
msg := new(session.RequestMetaHeader) return message.UnmarshalJSON(r, data, new(session.RequestMetaHeader))
if err := protojson.Unmarshal(data, msg); err != nil {
return err
}
return r.FromGRPCMessage(msg)
} }
func (r *RequestVerificationHeader) MarshalJSON() ([]byte, error) { func (r *RequestVerificationHeader) MarshalJSON() ([]byte, error) {
@ -83,13 +58,7 @@ func (r *RequestVerificationHeader) MarshalJSON() ([]byte, error) {
} }
func (r *RequestVerificationHeader) UnmarshalJSON(data []byte) error { func (r *RequestVerificationHeader) UnmarshalJSON(data []byte) error {
msg := new(session.RequestVerificationHeader) return message.UnmarshalJSON(r, data, new(session.RequestVerificationHeader))
if err := protojson.Unmarshal(data, msg); err != nil {
return err
}
return r.FromGRPCMessage(msg)
} }
func (r *ResponseMetaHeader) MarshalJSON() ([]byte, error) { func (r *ResponseMetaHeader) MarshalJSON() ([]byte, error) {
@ -97,13 +66,7 @@ func (r *ResponseMetaHeader) MarshalJSON() ([]byte, error) {
} }
func (r *ResponseMetaHeader) UnmarshalJSON(data []byte) error { func (r *ResponseMetaHeader) UnmarshalJSON(data []byte) error {
msg := new(session.ResponseMetaHeader) return message.UnmarshalJSON(r, data, new(session.ResponseMetaHeader))
if err := protojson.Unmarshal(data, msg); err != nil {
return err
}
return r.FromGRPCMessage(msg)
} }
func (r *ResponseVerificationHeader) MarshalJSON() ([]byte, error) { func (r *ResponseVerificationHeader) MarshalJSON() ([]byte, error) {
@ -111,13 +74,7 @@ func (r *ResponseVerificationHeader) MarshalJSON() ([]byte, error) {
} }
func (r *ResponseVerificationHeader) UnmarshalJSON(data []byte) error { func (r *ResponseVerificationHeader) UnmarshalJSON(data []byte) error {
msg := new(session.ResponseVerificationHeader) return message.UnmarshalJSON(r, data, new(session.ResponseVerificationHeader))
if err := protojson.Unmarshal(data, msg); err != nil {
return err
}
return r.FromGRPCMessage(msg)
} }
func (x *ContainerSessionContext) MarshalJSON() ([]byte, error) { func (x *ContainerSessionContext) MarshalJSON() ([]byte, error) {

View file

@ -4,7 +4,6 @@ import (
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/rpc/message" "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/rpc/message"
session "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/session/grpc" session "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/session/grpc"
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/util/proto" "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/util/proto"
goproto "google.golang.org/protobuf/proto"
) )
const ( const (
@ -154,12 +153,7 @@ func (x *XHeader) StableSize() (size int) {
} }
func (x *XHeader) Unmarshal(data []byte) error { func (x *XHeader) Unmarshal(data []byte) error {
m := new(session.XHeader) return message.Unmarshal(x, data, new(session.XHeader))
if err := goproto.Unmarshal(data, m); err != nil {
return err
}
return x.FromGRPCMessage(m)
} }
func (l *TokenLifetime) StableMarshal(buf []byte) []byte { func (l *TokenLifetime) StableMarshal(buf []byte) []byte {
@ -193,12 +187,7 @@ func (l *TokenLifetime) StableSize() (size int) {
} }
func (l *TokenLifetime) Unmarshal(data []byte) error { func (l *TokenLifetime) Unmarshal(data []byte) error {
m := new(session.SessionToken_Body_TokenLifetime) return message.Unmarshal(l, data, new(session.SessionToken_Body_TokenLifetime))
if err := goproto.Unmarshal(data, m); err != nil {
return err
}
return l.FromGRPCMessage(m)
} }
func (c *ObjectSessionContext) StableMarshal(buf []byte) []byte { func (c *ObjectSessionContext) StableMarshal(buf []byte) []byte {
@ -234,12 +223,7 @@ func (c *ObjectSessionContext) StableSize() (size int) {
} }
func (c *ObjectSessionContext) Unmarshal(data []byte) error { func (c *ObjectSessionContext) Unmarshal(data []byte) error {
m := new(session.ObjectSessionContext) return message.Unmarshal(c, data, new(session.ObjectSessionContext))
if err := goproto.Unmarshal(data, m); err != nil {
return err
}
return c.FromGRPCMessage(m)
} }
const ( const (
@ -338,12 +322,7 @@ func (t *TokenBody) StableSize() (size int) {
} }
func (t *TokenBody) Unmarshal(data []byte) error { func (t *TokenBody) Unmarshal(data []byte) error {
m := new(session.SessionToken_Body) return message.Unmarshal(t, data, new(session.SessionToken_Body))
if err := goproto.Unmarshal(data, m); err != nil {
return err
}
return t.FromGRPCMessage(m)
} }
func (t *Token) StableMarshal(buf []byte) []byte { func (t *Token) StableMarshal(buf []byte) []byte {
@ -375,12 +354,7 @@ func (t *Token) StableSize() (size int) {
} }
func (t *Token) Unmarshal(data []byte) error { func (t *Token) Unmarshal(data []byte) error {
m := new(session.SessionToken) return message.Unmarshal(t, data, new(session.SessionToken))
if err := goproto.Unmarshal(data, m); err != nil {
return err
}
return t.FromGRPCMessage(m)
} }
func (r *RequestMetaHeader) StableMarshal(buf []byte) []byte { func (r *RequestMetaHeader) StableMarshal(buf []byte) []byte {
@ -435,12 +409,7 @@ func (r *RequestMetaHeader) StableSize() (size int) {
} }
func (r *RequestMetaHeader) Unmarshal(data []byte) error { func (r *RequestMetaHeader) Unmarshal(data []byte) error {
m := new(session.RequestMetaHeader) return message.Unmarshal(r, data, new(session.RequestMetaHeader))
if err := goproto.Unmarshal(data, m); err != nil {
return err
}
return r.FromGRPCMessage(m)
} }
func (r *RequestVerificationHeader) StableMarshal(buf []byte) []byte { func (r *RequestVerificationHeader) StableMarshal(buf []byte) []byte {
@ -476,12 +445,7 @@ func (r *RequestVerificationHeader) StableSize() (size int) {
} }
func (r *RequestVerificationHeader) Unmarshal(data []byte) error { func (r *RequestVerificationHeader) Unmarshal(data []byte) error {
m := new(session.RequestVerificationHeader) return message.Unmarshal(r, data, new(session.RequestVerificationHeader))
if err := goproto.Unmarshal(data, m); err != nil {
return err
}
return r.FromGRPCMessage(m)
} }
func (r *ResponseMetaHeader) StableMarshal(buf []byte) []byte { func (r *ResponseMetaHeader) StableMarshal(buf []byte) []byte {
@ -532,12 +496,7 @@ func (r *ResponseMetaHeader) StableSize() (size int) {
} }
func (r *ResponseMetaHeader) Unmarshal(data []byte) error { func (r *ResponseMetaHeader) Unmarshal(data []byte) error {
m := new(session.ResponseMetaHeader) return message.Unmarshal(r, data, new(session.ResponseMetaHeader))
if err := goproto.Unmarshal(data, m); err != nil {
return err
}
return r.FromGRPCMessage(m)
} }
func (r *ResponseVerificationHeader) StableMarshal(buf []byte) []byte { func (r *ResponseVerificationHeader) StableMarshal(buf []byte) []byte {
@ -573,10 +532,5 @@ func (r *ResponseVerificationHeader) StableSize() (size int) {
} }
func (r *ResponseVerificationHeader) Unmarshal(data []byte) error { func (r *ResponseVerificationHeader) Unmarshal(data []byte) error {
m := new(session.ResponseVerificationHeader) return message.Unmarshal(r, data, new(session.ResponseVerificationHeader))
if err := goproto.Unmarshal(data, m); err != nil {
return err
}
return r.FromGRPCMessage(m)
} }