diff --git a/v2/session/json.go b/v2/session/json.go index bb9489e7..f8cf61dd 100644 --- a/v2/session/json.go +++ b/v2/session/json.go @@ -24,3 +24,23 @@ func (c *ObjectSessionContext) 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(session.SessionToken_Body_TokenLifetime) + + if err := protojson.Unmarshal(data, msg); err != nil { + return err + } + + *l = *TokenLifetimeFromGRPCMessage(msg) + + return nil +} diff --git a/v2/session/json_test.go b/v2/session/json_test.go index d76b6f4a..add76148 100644 --- a/v2/session/json_test.go +++ b/v2/session/json_test.go @@ -18,3 +18,15 @@ func TestChecksumJSON(t *testing.T) { require.Equal(t, ctx, ctx2) } + +func TestTokenLifetimeJSON(t *testing.T) { + l := generateLifetime(1, 2, 3) + + data, err := l.MarshalJSON() + require.NoError(t, err) + + l2 := new(session.TokenLifetime) + require.NoError(t, l2.UnmarshalJSON(data)) + + require.Equal(t, l, l2) +} diff --git a/v2/session/marshal.go b/v2/session/marshal.go index 34e038ef..2e103a04 100644 --- a/v2/session/marshal.go +++ b/v2/session/marshal.go @@ -225,6 +225,17 @@ func (l *TokenLifetime) StableSize() (size int) { return size } +func (l *TokenLifetime) Unmarshal(data []byte) error { + m := new(session.SessionToken_Body_TokenLifetime) + if err := goproto.Unmarshal(data, m); err != nil { + return err + } + + *l = *TokenLifetimeFromGRPCMessage(m) + + return nil +} + func (c *ObjectSessionContext) StableMarshal(buf []byte) ([]byte, error) { if c == nil { return []byte{}, nil diff --git a/v2/session/marshal_test.go b/v2/session/marshal_test.go index 8fd889ec..059aa849 100644 --- a/v2/session/marshal_test.go +++ b/v2/session/marshal_test.go @@ -62,16 +62,14 @@ func TestXHeader_StableMarshal(t *testing.T) { func TestTokenLifetime_StableMarshal(t *testing.T) { lifetimeFrom := generateLifetime(10, 20, 30) - transport := new(grpc.SessionToken_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(session.TokenLifetime) + require.NoError(t, lifetimeTo.Unmarshal(wire)) - lifetimeTo := session.TokenLifetimeFromGRPCMessage(transport) require.Equal(t, lifetimeFrom, lifetimeTo) }) }