Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add json support to Pointer #180

Merged
merged 3 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased
- No changes yet.
### Added
- Add `MarshalJSON` and `UnmarshalJSON` method to `atomic.Pointer[T]` type
allowing users to use pointer with json.

## [1.11.0] - 2023-05-02
### Fixed
Expand Down
20 changes: 19 additions & 1 deletion pointer_go118.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,27 @@

package atomic

import "fmt"
import (
"encoding/json"
"fmt"
)

// String returns a human readable representation of a Pointer's underlying value.
func (p *Pointer[T]) String() string {
return fmt.Sprint(p.Load())
}

// MarshalJSON encodes the wrapped pointer into JSON.
func (p *Pointer[T]) MarshalJSON() ([]byte, error) {
return json.Marshal(p.Load())
}

// UnmarshalJSON decodes JSON into the wrapped pointer.
func (p *Pointer[T]) UnmarshalJSON(b []byte) error {
var v T
if err := json.Unmarshal(b, &v); err != nil {
return err

Check warning on line 45 in pointer_go118.go

View check run for this annotation

Codecov / codecov/patch

pointer_go118.go#L45

Added line #L45 was not covered by tests
}
p.Store(&v)
return nil
}
21 changes: 20 additions & 1 deletion pointer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,17 @@
package atomic

import (
"encoding/json"
"fmt"
"testing"

"github.com/stretchr/testify/require"
)

func TestPointer(t *testing.T) {
type foo struct{ v int }
type foo struct {
V int `json:"v"`
}

i := foo{42}
j := foo{0}
Expand Down Expand Up @@ -94,6 +97,22 @@ func TestPointer(t *testing.T) {
atom := tt.newAtomic()
require.Equal(t, fmt.Sprint(tt.initial), atom.String(), "String did not return the correct value.")
})

t.Run("MarshalJSON", func(t *testing.T) {
atom := tt.newAtomic()
marshaledPointer, err := json.Marshal(atom)
require.NoError(t, err)
marshaledRaw, err := json.Marshal(tt.initial)
require.NoError(t, err)
require.Equal(t, marshaledRaw, marshaledPointer, "MarshalJSON did not return the correct value.")
})
})
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could a test case be added for the Unmarshal case?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure what are you asking, do you mean line 112?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I get it. you mean Unmarshal error case

t.Run("UnmarshalJSON", func(t *testing.T) {
var p Pointer[foo]

require.NoError(t, json.Unmarshal([]byte(`{"v":1024}`), &p))
require.Equal(t, 1024, p.Load().V, "UnmarshalJSON should have expected result")
})
}
Loading