Skip to content

Commit

Permalink
Merge pull request #45 from suzuki-shunsuke/perf/change-api-interface
Browse files Browse the repository at this point in the history
BREAKING CHANGE: remove ConvertByte and change signature of Convert
  • Loading branch information
suzuki-shunsuke committed Feb 21, 2021
2 parents 7a9ddda + 2533106 commit 39d753a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 77 deletions.
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

0 comments on commit 39d753a

Please sign in to comment.