-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge_test.go
53 lines (47 loc) · 2.16 KB
/
merge_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
// #############################################################################
// # File: merge_test.go #
// # Project: mergejson #
// # Created Date: 2023/10/09 09:42:56 #
// # Author: realjf #
// # ----- #
// # Last Modified: 2023/10/09 10:30:53 #
// # Modified By: realjf #
// # ----- #
// # Copyright (c) 2023 realjf #
// #############################################################################
package mergejson_test
import (
"fmt"
"testing"
"github.com/realjf/mergejson"
)
func TestMergeJson(t *testing.T) {
// merge map: same key name but different value
a, err := mergejson.MergeJson([]byte(`{"user":{ "name": "hello" }}`), []byte(`{"user":{ "name": "world" }}`))
if err != nil {
t.Fatal(err)
return
}
fmt.Printf("same key name but different value: %s\n", a)
// merge map: same map but a different number of keys(src < dst)
a1, err := mergejson.MergeJson([]byte(`{"user":{ "name": "hello" }}`), []byte(`{"user":{ "name": "world", "age": 12 }}`))
if err != nil {
t.Fatal(err)
return
}
fmt.Printf("same key name but a different number of keys(src < dst): %s\n", a1)
// merge map: same key name but a different number of keys(src > dst)
a2, err := mergejson.MergeJson([]byte(`{"user":{ "name": "hello", "age": 12}}`), []byte(`{"user":{ "name": "world" }}`))
if err != nil {
t.Fatal(err)
return
}
fmt.Printf("same key name but a different number of keys(src > dst): %s\n", a2)
// merge slice
a3, err := mergejson.MergeJson([]byte(`{"user":{ "name": "hello", "lucky_num": [1,2,3,4]}}`), []byte(`{"user":{ "name": "world", "lucky_num": [5,6,7]}}`))
if err != nil {
t.Fatal(err)
return
}
fmt.Printf("same key name but a different number of keys(src > dst): %s\n", a3)
}