[#168] acl: Implement binary/JSON encoders/decoders on BearerTokenBody

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2020-11-13 15:03:29 +03:00 committed by Alex Vanin
parent 0f4d5d5f1e
commit d8fa8df442
4 changed files with 45 additions and 4 deletions

View file

@ -134,3 +134,23 @@ func (l *TokenLifetime) UnmarshalJSON(data []byte) error {
return nil return nil
} }
func (bt *BearerTokenBody) MarshalJSON() ([]byte, error) {
return protojson.MarshalOptions{
EmitUnpopulated: true,
}.Marshal(
BearerTokenBodyToGRPCMessage(bt),
)
}
func (bt *BearerTokenBody) UnmarshalJSON(data []byte) error {
msg := new(acl.BearerToken_Body)
if err := protojson.Unmarshal(data, msg); err != nil {
return err
}
*bt = *BearerTokenBodyFromGRPCMessage(msg)
return nil
}

View file

@ -69,3 +69,15 @@ func TestTokenLifetimeJSON(t *testing.T) {
require.Equal(t, l, l2) require.Equal(t, l, l2)
} }
func TestBearerTokenBodyJSON(t *testing.T) {
b := generateBearerTokenBody("id")
data, err := b.MarshalJSON()
require.NoError(t, err)
b2 := new(acl.BearerTokenBody)
require.NoError(t, b2.UnmarshalJSON(data))
require.Equal(t, b, b2)
}

View file

@ -418,6 +418,17 @@ func (bt *BearerTokenBody) StableSize() (size int) {
return size return size
} }
func (bt *BearerTokenBody) Unmarshal(data []byte) error {
m := new(acl.BearerToken_Body)
if err := proto.Unmarshal(data, m); err != nil {
return err
}
*bt = *BearerTokenBodyFromGRPCMessage(m)
return nil
}
func (bt *BearerToken) StableMarshal(buf []byte) ([]byte, error) { func (bt *BearerToken) StableMarshal(buf []byte) ([]byte, error) {
if bt == nil { if bt == nil {
return []byte{}, nil return []byte{}, nil

View file

@ -212,16 +212,14 @@ func TestTokenLifetime_StableMarshal(t *testing.T) {
func TestBearerTokenBody_StableMarshal(t *testing.T) { func TestBearerTokenBody_StableMarshal(t *testing.T) {
bearerTokenBodyFrom := generateBearerTokenBody("Bearer Token Body") bearerTokenBodyFrom := generateBearerTokenBody("Bearer Token Body")
transport := new(grpc.BearerToken_Body)
t.Run("non empty", func(t *testing.T) { t.Run("non empty", func(t *testing.T) {
wire, err := bearerTokenBodyFrom.StableMarshal(nil) wire, err := bearerTokenBodyFrom.StableMarshal(nil)
require.NoError(t, err) require.NoError(t, err)
err = goproto.Unmarshal(wire, transport) bearerTokenBodyTo := new(acl.BearerTokenBody)
require.NoError(t, err) require.NoError(t, bearerTokenBodyTo.Unmarshal(wire))
bearerTokenBodyTo := acl.BearerTokenBodyFromGRPCMessage(transport)
require.Equal(t, bearerTokenBodyFrom, bearerTokenBodyTo) require.Equal(t, bearerTokenBodyFrom, bearerTokenBodyTo)
}) })
} }