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

docs: update GoDoc #20

Merged
merged 1 commit into from Jul 11, 2020
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
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -9,7 +9,7 @@

Go library to check if two values are equal as data format such as JSON.

This is inspired to [go-jsoneq](https://github.com/suzuki-shunsuke/go-jsoneq), and provides a general interface to support various data formats (ex. YAML).
This is inspired to [go-jsoneq](https://github.com/suzuki-shunsuke/go-jsoneq), and provides a general API to support various data formats (ex. YAML).

## Example

Expand Down
13 changes: 12 additions & 1 deletion dataeq/dataeq.go
Expand Up @@ -5,22 +5,30 @@ import (
)

type (
Marshal func(interface{}) ([]byte, error)
// Marshal converts data to a byte string.
Marshal func(interface{}) ([]byte, error)
// Unmarshal parses the encoded byte string and stores the result in the value pointed to by 2th argument.
// The 2th argument must be a pointer.
Unmarshal func([]byte, interface{}) error

// DataFormat allows to compare values as the data format.
// DataFormat must be created by the function `New`.
DataFormat struct {
marshal func(interface{}) ([]byte, error)
unmarshal func([]byte, interface{}) error
}
)

// New creates DataFormat by Marshal and Unmarshal.
// DataFormat must be created by this function.
func New(marshal Marshal, unmarshal Unmarshal) DataFormat {
return DataFormat{
marshal: marshal,
unmarshal: unmarshal,
}
}

// ConvertByte unmarshals a byte string to `interface{}`.
func (df DataFormat) ConvertByte(b []byte) (interface{}, error) {
var d interface{}
err := df.unmarshal(b, &d)
Expand All @@ -30,6 +38,8 @@ func (df DataFormat) ConvertByte(b []byte) (interface{}, error) {
return nil, err
}

// Convert converts value to byte string and unmarshals the byte string to `interface{}`.
// Convert can be used to normalize the value to compare with the other value.
func (df DataFormat) Convert(x interface{}) (interface{}, error) {
if a, ok := x.([]byte); ok {
return df.ConvertByte(a)
Expand All @@ -41,6 +51,7 @@ func (df DataFormat) Convert(x interface{}) (interface{}, error) {
return df.ConvertByte(b)
}

// 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
Expand Down
9 changes: 8 additions & 1 deletion dataeq/doc.go
@@ -1,2 +1,9 @@
// Package dataeq checks if two values are equal.
// 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.
//
// 1. convert value to a byte string by Marshal
// 2. unmarshal a byte string to `interface{}` by Unmarshal
// 3. compare two values by reflect.DeepEqual
//
// So dataeq requires two API for the data format, Marhsal and Unmarshal.
package dataeq