[#168] acl: Implement binary/JSON encoders/decoders on BearerTokenBody
Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
parent
0f4d5d5f1e
commit
d8fa8df442
4 changed files with 45 additions and 4 deletions
|
@ -134,3 +134,23 @@ func (l *TokenLifetime) UnmarshalJSON(data []byte) error {
|
|||
|
||||
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
|
||||
}
|
||||
|
|
|
@ -69,3 +69,15 @@ func TestTokenLifetimeJSON(t *testing.T) {
|
|||
|
||||
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)
|
||||
}
|
||||
|
|
|
@ -418,6 +418,17 @@ func (bt *BearerTokenBody) StableSize() (size int) {
|
|||
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) {
|
||||
if bt == nil {
|
||||
return []byte{}, nil
|
||||
|
|
|
@ -212,16 +212,14 @@ func TestTokenLifetime_StableMarshal(t *testing.T) {
|
|||
|
||||
func TestBearerTokenBody_StableMarshal(t *testing.T) {
|
||||
bearerTokenBodyFrom := generateBearerTokenBody("Bearer Token Body")
|
||||
transport := new(grpc.BearerToken_Body)
|
||||
|
||||
t.Run("non empty", func(t *testing.T) {
|
||||
wire, err := bearerTokenBodyFrom.StableMarshal(nil)
|
||||
require.NoError(t, err)
|
||||
|
||||
err = goproto.Unmarshal(wire, transport)
|
||||
require.NoError(t, err)
|
||||
bearerTokenBodyTo := new(acl.BearerTokenBody)
|
||||
require.NoError(t, bearerTokenBodyTo.Unmarshal(wire))
|
||||
|
||||
bearerTokenBodyTo := acl.BearerTokenBodyFromGRPCMessage(transport)
|
||||
require.Equal(t, bearerTokenBodyFrom, bearerTokenBodyTo)
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue