Skip to content

Commit

Permalink
feat: change API of Convert and ConvertByte
Browse files Browse the repository at this point in the history
BREAKING CHANGE: change API of Convert and ConvertByte
  • Loading branch information
suzuki-shunsuke committed Feb 21, 2021
1 parent 7a9ddda commit 10ee77f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 21 deletions.
29 changes: 12 additions & 17 deletions dataeq/dataeq.go
Expand Up @@ -28,40 +28,35 @@ 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
// ConvertByte unmarshals a byte string to dst.
func (df DataFormat) ConvertByte(b []byte, dst interface{}) error {
return df.unmarshal(b, dst)
}

// 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.ConvertByte(a, dst)
}
b, err := df.marshal(x)
if err != nil {
return nil, err
return err
}
return df.ConvertByte(b)
return df.ConvertByte(b, dst)
}

// Equal returns true if two arguments are equal.
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
14 changes: 11 additions & 3 deletions dataeq/dataeq_test.go
Expand Up @@ -9,6 +9,7 @@ import (
)

func TestJSON_ConvertByte(t *testing.T) {
t.Parallel()
data := []struct {
title string
b []byte
Expand Down Expand Up @@ -51,7 +52,9 @@ func TestJSON_ConvertByte(t *testing.T) {
for _, d := range data {
d := d
t.Run(d.title, func(t *testing.T) {
a, err := dataeq.JSON.ConvertByte(d.b)
t.Parallel()
var a interface{}
err := dataeq.JSON.ConvertByte(d.b, &a)
if d.isError {
require.NotNil(t, err)
return
Expand All @@ -71,6 +74,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 +112,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 +125,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 +174,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 10ee77f

Please sign in to comment.