/
dataeq.go
58 lines (52 loc) · 1.51 KB
/
dataeq.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package dataeq
import (
"reflect"
)
type (
// 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,
}
}
// 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{}, dst interface{}) error {
if a, ok := x.([]byte); ok {
return df.unmarshal(a, dst)
}
b, err := df.marshal(x)
if err != nil {
return err
}
return df.unmarshal(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
}
var a interface{}
if err := df.Convert(x, &a); err != nil {
return false, err
}
var b interface{}
if err := df.Convert(y, &b); err != nil {
return false, err
}
return reflect.DeepEqual(a, b), nil
}