-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcomposite_test.go
85 lines (73 loc) · 2.02 KB
/
composite_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
package jsontool
import (
"fmt"
"os"
"strings"
"testing"
)
func TestGetVal(t *testing.T) {
i := 100.0
r := GetVal[float64](i)
fmt.Println(r)
}
func TestTransform(t *testing.T) {
RegisterRule(`.*`, func(path string, value any) (ok bool, ps []string, vs []any) {
ok = GetVal[string](value) == "H"
ps = append(ps, path)
vs = append(vs, "HH")
return
})
RegisterRule(`^array\.`, func(path string, value any) (ok bool, ps []string, vs []any) {
ok = true
ps = append(ps, "")
vs = append(vs, nil)
return
})
data, err := os.ReadFile("./data/FlattenTest.json")
if err != nil {
panic(err)
}
fmt.Println(FmtStr(TransformUnderFirstRule(nil, data), "\t"))
}
func TestComposite(t *testing.T) {
data, err := os.ReadFile("./data/FlattenTest.json")
if err != nil {
panic(err)
}
m, err := Flatten(data)
fmt.Println(len(m), err)
str := Composite(m, nil)
os.WriteFile("./data/FlattenTest_Composite_1.json", []byte(str), os.ModePerm)
// str = Composite(m,
// func(path string, value any) (string, any, bool) {
// if strings.Contains(path, "object") {
// if value == "b" {
// return NewSibling(path, "ABC"), value.(string) + "ABC", false
// }
// return path, value, false
// }
// return "", nil, false
// })
// fmt.Println(str)
str = Composite2(m, func(path string, value any) (p []string, v []any) {
if strings.Contains(path, "object") {
if value == "b" || value == "F" {
return []string{
NewChild(NewSibling(path, "DEF"), "abc"),
NewChild(NewSibling(path, "DEF"), "def"),
},
[]any{
value.(string) + "ABC",
value.(string) + "DEF",
}
}
return []string{path}, []any{value}
}
return nil, nil
})
fmt.Println(FmtStr(str, " "))
// str = CompositeIncl(m, "array.0.c", "object.a", "object1.object11.a")
// os.WriteFile("./data/FlattenTest_CompositeIncl.json", []byte(str), os.ModePerm)
// str = CompositeExcl(m, "array.0.c", "object.a", "object1.object11.a")
// os.WriteFile("./data/FlattenTest_CompositeExcl.json", []byte(str), os.ModePerm)
}