diff --git a/v2/acl/json.go b/v2/acl/json.go index 04256f7f..c80c165a 100644 --- a/v2/acl/json.go +++ b/v2/acl/json.go @@ -114,3 +114,23 @@ func (t *Table) UnmarshalJSON(data []byte) error { return nil } + +func (l *TokenLifetime) MarshalJSON() ([]byte, error) { + return protojson.MarshalOptions{ + EmitUnpopulated: true, + }.Marshal( + TokenLifetimeToGRPCMessage(l), + ) +} + +func (l *TokenLifetime) UnmarshalJSON(data []byte) error { + msg := new(acl.BearerToken_Body_TokenLifetime) + + if err := protojson.Unmarshal(data, msg); err != nil { + return err + } + + *l = *TokenLifetimeFromGRPCMessage(msg) + + return nil +} diff --git a/v2/acl/json_test.go b/v2/acl/json_test.go index cc5a8346..b860d356 100644 --- a/v2/acl/json_test.go +++ b/v2/acl/json_test.go @@ -57,3 +57,15 @@ func TestTable_MarshalJSON(t *testing.T) { require.Equal(t, tab, tab2) } + +func TestTokenLifetimeJSON(t *testing.T) { + l := generateLifetime(1, 2, 3) + + data, err := l.MarshalJSON() + require.NoError(t, err) + + l2 := new(acl.TokenLifetime) + require.NoError(t, l2.UnmarshalJSON(data)) + + require.Equal(t, l, l2) +} diff --git a/v2/acl/marshal.go b/v2/acl/marshal.go index f434970f..8ff99642 100644 --- a/v2/acl/marshal.go +++ b/v2/acl/marshal.go @@ -359,6 +359,17 @@ func (l *TokenLifetime) StableSize() (size int) { return size } +func (l *TokenLifetime) Unmarshal(data []byte) error { + m := new(acl.BearerToken_Body_TokenLifetime) + if err := proto.Unmarshal(data, m); err != nil { + return err + } + + *l = *TokenLifetimeFromGRPCMessage(m) + + return nil +} + func (bt *BearerTokenBody) StableMarshal(buf []byte) ([]byte, error) { if bt == nil { return []byte{}, nil diff --git a/v2/acl/marshal_test.go b/v2/acl/marshal_test.go index 0aecf974..f55834bb 100644 --- a/v2/acl/marshal_test.go +++ b/v2/acl/marshal_test.go @@ -198,16 +198,14 @@ func TestTable_StableMarshal(t *testing.T) { func TestTokenLifetime_StableMarshal(t *testing.T) { lifetimeFrom := generateLifetime(10, 20, 30) - transport := new(grpc.BearerToken_Body_TokenLifetime) t.Run("non empty", func(t *testing.T) { wire, err := lifetimeFrom.StableMarshal(nil) require.NoError(t, err) - err = goproto.Unmarshal(wire, transport) - require.NoError(t, err) + lifetimeTo := new(acl.TokenLifetime) + require.NoError(t, lifetimeTo.Unmarshal(wire)) - lifetimeTo := acl.TokenLifetimeFromGRPCMessage(transport) require.Equal(t, lifetimeFrom, lifetimeTo) }) }