-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathconvert_test.go
110 lines (89 loc) · 2.37 KB
/
convert_test.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package mendoza_test
import (
"github.com/sanity-io/mendoza"
"github.com/stretchr/testify/require"
"testing"
)
type CustomObject struct {
attrs map[string]interface{}
}
func TestConvertObject(t *testing.T) {
opts := mendoza.DefaultOptions.WithConvertFunc(func(value interface{}) interface{} {
if value, ok := value.(CustomObject); ok {
return value.attrs
}
if value, ok := value.(CustomArray); ok {
return value.values
}
return value
})
customLeft := CustomObject{
attrs: map[string]interface{}{
"a": "abcdefgh",
},
}
customRight := CustomObject{
attrs: map[string]interface{}{
"a": "abcdefgh",
"b": 123.0,
},
}
t.Run("TopLevel", func(t *testing.T) {
left := customLeft
right := customRight
result := customRight.attrs
patch, err := opts.CreatePatch(left, right)
require.NoError(t, err)
newRight := opts.ApplyPatch(left, patch)
require.EqualValues(t, result, newRight)
})
t.Run("Nested", func(t *testing.T) {
left := map[string]interface{}{"a": customLeft}
right := map[string]interface{}{"a": customRight}
result := map[string]interface{}{"a": customRight.attrs}
patch, err := opts.CreatePatch(left, right)
require.NoError(t, err)
newRight := opts.ApplyPatch(left, patch)
require.EqualValues(t, result, newRight)
})
}
type CustomArray struct {
values []interface{}
}
func TestConvertArray(t *testing.T) {
opts := mendoza.DefaultOptions.WithConvertFunc(func(value interface{}) interface{} {
if value, ok := value.(CustomArray); ok {
return value.values
}
return value
})
customLeft := CustomArray{
[]interface{}{map[string]interface{}{
"a": "abcdefgh",
}},
}
customRight := CustomArray{
[]interface{}{map[string]interface{}{
"a": "abcdefgh",
"b": 123.0,
}},
}
t.Run("TopLevel", func(t *testing.T) {
left := customLeft
right := customRight
result := customRight.values
patch, err := opts.CreatePatch(left, right)
require.NoError(t, err)
newRight := opts.ApplyPatch(left, patch)
require.EqualValues(t, result, newRight)
})
t.Run("Nested", func(t *testing.T) {
left := map[string]interface{}{"a": customLeft}
right := map[string]interface{}{"a": customRight}
result := map[string]interface{}{"a": customRight.values}
patch, err := opts.CreatePatch(left, right)
require.NoError(t, err)
newRight := opts.ApplyPatch(left, patch)
require.EqualValues(t, result, newRight)
})
}