From 899b6d069cbed80ac3b529f7fc0916f47ee45d71 Mon Sep 17 00:00:00 2001 From: BRUHITsABunny <53124399+BRUHItsABunny@users.noreply.github.com> Date: Fri, 25 Nov 2022 21:48:02 +0100 Subject: [PATCH 1/4] Support JSON serialization and deserialization of atomic.Time --- CHANGELOG.md | 1 + time.go | 16 ++++++++++++++++ time_test.go | 26 ++++++++++++++++++++++++++ 3 files changed, 43 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 729a316..8fec538 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - Add `String` method to `atomic.Pointer[T]` type allowing users to safely print underlying values of pointers. +- Support JSON serialization and deserialization of `atomic.Time` ## [1.10.0] - 2022-08-11 ### Added diff --git a/time.go b/time.go index 1660feb..016d995 100644 --- a/time.go +++ b/time.go @@ -23,6 +23,7 @@ package atomic import ( + "encoding/json" "time" ) @@ -53,3 +54,18 @@ func (x *Time) Load() time.Time { func (x *Time) Store(val time.Time) { x.v.Store(packTime(val)) } + +// MarshalJSON encodes the wrapped time.Time into JSON. +func (x *Time) MarshalJSON() ([]byte, error) { + return json.Marshal(x.Load()) +} + +// UnmarshalJSON decodes a time.Time from JSON. +func (x *Time) UnmarshalJSON(b []byte) error { + var v time.Time + if err := json.Unmarshal(b, &v); err != nil { + return err + } + x.Store(v) + return nil +} diff --git a/time_test.go b/time_test.go index 83ac022..8471dfb 100644 --- a/time_test.go +++ b/time_test.go @@ -21,6 +21,7 @@ package atomic import ( + "encoding/json" "testing" "time" @@ -71,6 +72,31 @@ func TestLargeTime(t *testing.T) { atom.Store(dayBeforePast) assert.Equal(t, 1677, atom.Load().Year()) }) + + t.Run("JSON/Marshal", func(t *testing.T) { + now := time.Now() + atom := NewTime(now) + bytes, err := json.Marshal(atom) + require.NoError(t, err, "json.Marshal errored unexpectedly.") + + oBytes, err := json.Marshal(now) + require.NoError(t, err, "json.Marshal errored unexpectedly on the native time.Time.") + require.Equal(t, oBytes, bytes, "json.Marshal encoded the wrong bytes.") + }) + + t.Run("JSON/Unmarshal", func(t *testing.T) { + now := time.Now() + atom := NewTime(time.Time{}) + + oBytes, err := json.Marshal(now) + require.NoError(t, err, "json.Marshal errored unexpectedly on the native time.Time.") + err = json.Unmarshal(oBytes, &atom) + require.NoError(t, err, "json.Unmarshal errored unexpectedly.") + // NOTE: https://pkg.go.dev/time#Time.MarshalJSON, the time is formatted as RFC 3339 + // example: 2022-11-25T21:06:44.6023404+01:00 + // So we cannot compare now and atom.Load() because we lack nanoseconds + require.Equal(t, now.UnixMicro(), atom.Load().UnixMicro(), "json.Unmarshal didn't set the correct value.") + }) } func TestMonotonic(t *testing.T) { From 9e798cd188880ecbd5b1f69af7cbc1ad915f43d2 Mon Sep 17 00:00:00 2001 From: BRUHITsABunny <53124399+BRUHItsABunny@users.noreply.github.com> Date: Fri, 25 Nov 2022 22:43:09 +0100 Subject: [PATCH 2/4] Support JSON serialization and deserialization of atomic.Time --- time_ext.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/time_ext.go b/time_ext.go index 1e3dc97..10b3a22 100644 --- a/time_ext.go +++ b/time_ext.go @@ -22,7 +22,7 @@ package atomic import "time" -//go:generate bin/gen-atomicwrapper -name=Time -type=time.Time -wrapped=Value -pack=packTime -unpack=unpackTime -imports time -file=time.go +//go:generate bin/gen-atomicwrapper -name=Time -type=time.Time -wrapped=Value -pack=packTime -unpack=unpackTime -json -imports time -file=time.go func packTime(t time.Time) interface{} { return t From 5e04057b700c82eb5c00ec1177dca1073d1454b3 Mon Sep 17 00:00:00 2001 From: BRUHItsABunny <53124399+BRUHItsABunny@users.noreply.github.com> Date: Sun, 10 Nov 2024 13:38:27 -0500 Subject: [PATCH 3/4] feat: implement text marshaller --- time.go | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/time.go b/time.go index 1f8e9f1..16cafa6 100644 --- a/time.go +++ b/time.go @@ -23,7 +23,6 @@ package atomic import ( - "encoding/json" "time" ) @@ -55,15 +54,15 @@ func (x *Time) Store(val time.Time) { x.v.Store(packTime(val)) } -// MarshalJSON encodes the wrapped time.Time into JSON. -func (x *Time) MarshalJSON() ([]byte, error) { - return json.Marshal(x.Load()) +// MarshalText encodes the wrapped time.Time into JSON. +func (x *Time) MarshalText() ([]byte, error) { + return x.Load().MarshalText() } -// UnmarshalJSON decodes a time.Time from JSON. -func (x *Time) UnmarshalJSON(b []byte) error { +// UnmarshalText decodes a time.Time from JSON. +func (x *Time) UnmarshalText(b []byte) error { var v time.Time - if err := json.Unmarshal(b, &v); err != nil { + if err := v.UnmarshalText(b); err != nil { return err } x.Store(v) From 7ba3a15e1435c6327bb4cc7bf803edd57a172fcb Mon Sep 17 00:00:00 2001 From: BRUHItsABunny <53124399+BRUHItsABunny@users.noreply.github.com> Date: Sun, 10 Nov 2024 13:39:42 -0500 Subject: [PATCH 4/4] chore: update comments --- time.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/time.go b/time.go index 16cafa6..6dd0c12 100644 --- a/time.go +++ b/time.go @@ -54,12 +54,12 @@ func (x *Time) Store(val time.Time) { x.v.Store(packTime(val)) } -// MarshalText encodes the wrapped time.Time into JSON. +// MarshalText encodes the wrapped time.Time into string. func (x *Time) MarshalText() ([]byte, error) { return x.Load().MarshalText() } -// UnmarshalText decodes a time.Time from JSON. +// UnmarshalText decodes a time.Time from string. func (x *Time) UnmarshalText(b []byte) error { var v time.Time if err := v.UnmarshalText(b); err != nil {