Documentation
¶
Overview ¶
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.
Example ¶
package main import ( "fmt" "log" "github.com/suzuki-shunsuke/go-dataeq/dataeq" ) type ( Foo struct { Foo string `json:"foo"` } ) func checkResult(b bool, err error) { if err != nil { log.Fatal(err) } fmt.Println(b) } func main() { b, err := dataeq.JSON.Equal( Foo{Foo: "bar"}, map[string]string{"foo": "bar"}, ) checkResult(b, err) // when a type of value is []byte, it is treated as JSON string b, err = dataeq.JSON.Equal( Foo{Foo: "bar"}, []byte(`{"foo": "bar"}`), ) checkResult(b, err) }
Output: true true
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
Functions ¶
This section is empty.
Types ¶
type DataFormat ¶
type DataFormat struct {
// contains filtered or unexported fields
}
DataFormat allows to compare values as the data format. DataFormat must be created by the function `New`.
func New ¶
func New(marshal Marshal, unmarshal Unmarshal) DataFormat
New creates DataFormat by Marshal and Unmarshal. DataFormat must be created by this function.
func (DataFormat) Convert ¶
func (df DataFormat) Convert(x interface{}) (interface{}, error)
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 (DataFormat) ConvertByte ¶
func (df DataFormat) ConvertByte(b []byte) (interface{}, error)
ConvertByte unmarshals a byte string to `interface{}`.
func (DataFormat) Equal ¶
func (df DataFormat) Equal(x, y interface{}) (bool, error)
Equal returns true if two arguments are equal.