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

BREAKING CHANGE: remove ConvertByte and change signature of Convert #45

Merged
merged 1 commit into from Feb 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 10 additions & 20 deletions dataeq/dataeq.go
Expand Up @@ -28,40 +28,30 @@ func New(marshal Marshal, unmarshal Unmarshal) DataFormat {
}
}

// ConvertByte unmarshals a byte string to `interface{}`.
func (df DataFormat) ConvertByte(b []byte) (interface{}, error) {
var d interface{}
err := df.unmarshal(b, &d)
if err == nil {
return d, nil
}
return nil, err
}

// Convert converts value to byte string and unmarshals the byte string to `interface{}`.
// Convert converts value to byte string and unmarshals the byte string to dst.
// Convert can be used to normalize the value to compare with the other value.
func (df DataFormat) Convert(x interface{}) (interface{}, error) {
func (df *DataFormat) Convert(x interface{}, dst interface{}) error {
if a, ok := x.([]byte); ok {
return df.ConvertByte(a)
return df.unmarshal(a, dst)
}
b, err := df.marshal(x)
if err != nil {
return nil, err
return err
}
return df.ConvertByte(b)
return df.unmarshal(b, dst)
}

// Equal returns true if two arguments are equal.
func (df DataFormat) Equal(x, y interface{}) (bool, error) {
func (df *DataFormat) Equal(x, y interface{}) (bool, error) {
if reflect.DeepEqual(x, y) {
return true, nil
}
a, err := df.Convert(x)
if err != nil {
var a interface{}
if err := df.Convert(x, &a); err != nil {
return false, err
}
b, err := df.Convert(y)
if err != nil {
var b interface{}
if err := df.Convert(y, &b); err != nil {
return false, err
}
return reflect.DeepEqual(a, b), nil
Expand Down
63 changes: 7 additions & 56 deletions dataeq/dataeq_test.go
Expand Up @@ -8,60 +8,6 @@ import (
"github.com/suzuki-shunsuke/go-dataeq/dataeq"
)

func TestJSON_ConvertByte(t *testing.T) {
data := []struct {
title string
b []byte
isError bool
exp interface{}
}{
{
title: "simple map",
b: []byte(`{"foo": "bar"}`),
exp: map[string]interface{}{
"foo": "bar",
},
},
{
title: "simple array",
b: []byte(`["foo", "bar"]`),
exp: []interface{}{"foo", "bar"},
},
{
title: "simple int",
b: []byte(`5`),
exp: float64(5),
},
{
title: "simple string",
b: []byte(`"hello"`),
exp: "hello",
},
{
title: "simple null",
b: []byte(`null`),
exp: nil,
},
{
title: "invalid JSON",
b: []byte(`foo bar`),
isError: true,
},
}
for _, d := range data {
d := d
t.Run(d.title, func(t *testing.T) {
a, err := dataeq.JSON.ConvertByte(d.b)
if d.isError {
require.NotNil(t, err)
return
}
require.Nil(t, err)
require.Equal(t, d.exp, a)
})
}
}

type (
invalidMarshaler struct{}
)
Expand All @@ -71,6 +17,7 @@ func (m *invalidMarshaler) MarshalJSON() ([]byte, error) {
}

func TestJSON_Convert(t *testing.T) {
t.Parallel()
data := []struct {
title string
x interface{}
Expand Down Expand Up @@ -108,7 +55,9 @@ func TestJSON_Convert(t *testing.T) {
for _, d := range data {
d := d
t.Run(d.title, func(t *testing.T) {
a, err := dataeq.JSON.Convert(d.x)
t.Parallel()
var a interface{}
err := dataeq.JSON.Convert(d.x, &a)
if d.isError {
require.NotNil(t, err)
return
Expand All @@ -119,7 +68,8 @@ func TestJSON_Convert(t *testing.T) {
}
}

func TestJSON_Equal(t *testing.T) {
func TestJSON_Equal(t *testing.T) { //nolint:funlen
t.Parallel()
data := []struct {
title string
x interface{}
Expand Down Expand Up @@ -167,6 +117,7 @@ func TestJSON_Equal(t *testing.T) {
for _, d := range data {
d := d
t.Run(d.title, func(t *testing.T) {
t.Parallel()
f, err := dataeq.JSON.Equal(d.x, d.y)
if d.isError {
require.NotNil(t, err)
Expand Down
2 changes: 1 addition & 1 deletion dataeq/doc.go
@@ -1,5 +1,5 @@
// Package dataeq allows to create API to compare two values as the data format such as JSON and YAML.
// dataeq compare two values by the followin way.
// dataeq compare two values by the following way.
//
// 1. convert value to a byte string by Marshal
// 2. unmarshal a byte string to `interface{}` by Unmarshal
Expand Down