diff --git a/dataeq/dataeq.go b/dataeq/dataeq.go index f69b03e..567b386 100644 --- a/dataeq/dataeq.go +++ b/dataeq/dataeq.go @@ -28,27 +28,22 @@ 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. @@ -56,12 +51,12 @@ 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 diff --git a/dataeq/dataeq_test.go b/dataeq/dataeq_test.go index dce42f3..ae9c131 100644 --- a/dataeq/dataeq_test.go +++ b/dataeq/dataeq_test.go @@ -9,6 +9,7 @@ import ( ) func TestJSON_ConvertByte(t *testing.T) { + t.Parallel() data := []struct { title string b []byte @@ -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 @@ -71,6 +74,7 @@ func (m *invalidMarshaler) MarshalJSON() ([]byte, error) { } func TestJSON_Convert(t *testing.T) { + t.Parallel() data := []struct { title string x interface{} @@ -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 @@ -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{} @@ -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) diff --git a/dataeq/doc.go b/dataeq/doc.go index 767c87b..0e58c30 100644 --- a/dataeq/doc.go +++ b/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