github.com/GuanceCloud/cliutils@v1.1.21/pipeline/ptinput/funcs/fn_gjson_test.go (about) 1 // Unless explicitly stated otherwise all files in this repository are licensed 2 // under the MIT License. 3 // This product includes software developed at Guance Cloud (https://www.guance.com/). 4 // Copyright 2021-present Guance, Inc. 5 6 package funcs 7 8 import ( 9 "testing" 10 "time" 11 12 "github.com/GuanceCloud/cliutils/pipeline/ptinput" 13 "github.com/GuanceCloud/cliutils/point" 14 "github.com/stretchr/testify/assert" 15 ) 16 17 func TestGJSON(t *testing.T) { 18 Cases := []*funcCase{ 19 { 20 in: `{ 21 "name": {"first": "Tom", "last": "Anderson"}, 22 "age": 37, 23 "children": ["Sara","Alex","Jack"], 24 "fav.movie": "Deer Hunter", 25 "friends": [ 26 {"first": "Dale", "last": "Murphy", "age": 44, "nets": ["ig", "fb", "tw"]}, 27 {"first": "Roger", "last": "Craig", "age": 68, "nets": ["fb", "tw"]}, 28 {"first": "Jane", "last": "Murphy", "age": 47, "nets": ["ig", "tw"]} 29 ] 30 }`, 31 script: `gjson(_, "age")`, 32 expected: float64(37), 33 key: "age", 34 }, 35 { 36 in: `{ 37 "name": {"first": "Tom", "last": "Anderson"}, 38 "age":37, 39 "children": ["Sara","Alex","Jack"], 40 "fav.movie": "Deer Hunter", 41 "friends": [ 42 {"first": "Dale", "last": "Murphy", "age": 44, "nets": ["ig", "fb", "tw"]}, 43 {"first": "Roger", "last": "Craig", "age": 68, "nets": ["fb", "tw"]}, 44 {"first": "Jane", "last": "Murphy", "age": 47, "nets": ["ig", "tw"]} 45 ] 46 }`, 47 script: `gjson(_, "children")`, 48 expected: `["Sara","Alex","Jack"]`, 49 key: "children", 50 }, 51 { 52 in: `{ 53 "name": {"first": "Tom", "last": "Anderson"}, 54 "age":37, 55 "children": ["Sara","Alex","Jack"], 56 "fav.movie": "Deer Hunter", 57 "friends": [ 58 {"first": "Dale", "last": "Murphy", "age": 44, "nets": ["ig", "fb", "tw"]}, 59 {"first": "Roger", "last": "Craig", "age": 68, "nets": ["fb", "tw"]}, 60 {"first": "Jane", "last": "Murphy", "age": 47, "nets": ["ig", "tw"]} 61 ] 62 }`, 63 script: `gjson(_, "name")`, 64 expected: `{"first": "Tom", "last": "Anderson"}`, 65 key: "name", 66 }, 67 { 68 in: `{ 69 "name": {"first": "Tom", "last": "Anderson"}, 70 "age":37, 71 "children": ["Sara","Alex","Jack"], 72 "fav.movie": "Deer Hunter", 73 "friends": [ 74 {"first": "Dale", "last": "Murphy", "age": 44, "nets": ["ig", "fb", "tw"]}, 75 {"first": "Roger", "last": "Craig", "age": 68, "nets": ["fb", "tw"]}, 76 {"first": "Jane", "last": "Murphy", "age": 47, "nets": ["ig", "tw"]} 77 ] 78 }`, 79 script: `gjson(_, "name") 80 gjson(name, "first")`, 81 expected: `Tom`, 82 key: "first", 83 }, 84 { 85 in: `{ 86 "name": {"first": "Tom", "last": "Anderson"}, 87 "age":37, 88 "children": ["Sara","Alex","Jack"], 89 "fav.movie": "Deer Hunter", 90 "friends": [ 91 {"first": "Dale", "last": "Murphy", "age": 44, "nets": ["ig", "fb", "tw"]}, 92 {"first": "Roger", "last": "Craig", "age": 68, "nets": ["fb", "tw"]}, 93 {"first": "Jane", "last": "Murphy", "age": 47, "nets": ["ig", "tw"]} 94 ] 95 }`, 96 script: `gjson(_, "friends") 97 gjson(friends, "1.first", "f_first")`, 98 expected: "Roger", 99 key: "f_first", 100 }, 101 { 102 in: `{ 103 "name": {"first": "Tom", "last": "Anderson"}, 104 "age":37, 105 "children": ["Sara","Alex","Jack"], 106 "fav.movie": "Deer Hunter", 107 "friends": [ 108 {"first": "Dale", "last": "Murphy", "age": 44, "nets": ["ig", "fb", "tw"]}, 109 {"first": "Roger", "last": "Craig", "age": 68, "nets": ["fb", "tw"]}, 110 {"first": "Jane", "last": "Murphy", "age": 47, "nets": ["ig", "tw"]} 111 ] 112 }`, 113 script: `gjson(_, "friends", "friends") 114 gjson(friends, "1.nets.1", "f_nets")`, 115 expected: "tw", 116 key: "f_nets", 117 }, 118 { 119 in: `[ 120 {"first": "Dale", "last": "Murphy", "age": 44, "nets": ["ig", "fb", "tw"]}, 121 {"first": "Roger", "last": "Craig", "age": 68, "nets": ["fb", "tw"]}, 122 {"first": "Jane", "last": "Murphy", "age": 47, "nets": ["ig", "tw"]} 123 ]`, 124 script: `gjson(_, "0.nets.2", "f_nets")`, 125 expected: "tw", 126 key: "f_nets", 127 }, 128 } 129 for idx, tc := range Cases { 130 t.Run(tc.name, func(t *testing.T) { 131 runner, err := NewTestingRunner(tc.script) 132 133 if err != nil && tc.fail { 134 return 135 } else if err != nil || tc.fail { 136 assert.Equal(t, nil, err) 137 assert.Equal(t, tc.fail, err != nil) 138 } 139 140 pt := ptinput.NewPlPoint( 141 point.Logging, "test", nil, map[string]any{"message": tc.in}, time.Now()) 142 errR := runScript(runner, pt) 143 if errR != nil { 144 t.Fatal(errR.Error()) 145 } 146 147 r, _, e := pt.Get(tc.key) 148 assert.NoError(t, e) 149 if tc.key == "[2].age" { 150 t.Log(1) 151 } 152 assert.Equal(t, tc.expected, r) 153 154 t.Logf("[%d] PASS", idx) 155 }) 156 } 157 }