github.com/zhongdalu/gf@v1.0.0/g/encoding/gparser/gparser_unit_basic_test.go (about) 1 // Copyright 2017 gf Author(https://github.com/zhongdalu/gf). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/zhongdalu/gf. 6 7 package gparser_test 8 9 import ( 10 "github.com/zhongdalu/gf/g" 11 "github.com/zhongdalu/gf/g/encoding/gparser" 12 "github.com/zhongdalu/gf/g/test/gtest" 13 "testing" 14 ) 15 16 func Test_New(t *testing.T) { 17 data := []byte(`{"n":123456789, "m":{"k":"v"}, "a":[1,2,3]}`) 18 gtest.Case(t, func() { 19 j := gparser.New(data) 20 gtest.Assert(j.Get("n"), "123456789") 21 gtest.Assert(j.Get("m"), g.Map{"k": "v"}) 22 gtest.Assert(j.Get("a"), g.Slice{1, 2, 3}) 23 v := j.Value().(g.Map) 24 gtest.Assert(v["n"], 123456789) 25 }) 26 } 27 28 func Test_NewUnsafe(t *testing.T) { 29 data := []byte(`{"n":123456789, "m":{"k":"v"}, "a":[1,2,3]}`) 30 gtest.Case(t, func() { 31 j := gparser.NewUnsafe(data) 32 gtest.Assert(j.Get("n"), "123456789") 33 gtest.Assert(j.Get("m"), g.Map{"k": "v"}) 34 gtest.Assert(j.Get("m.k"), "v") 35 gtest.Assert(j.Get("a"), g.Slice{1, 2, 3}) 36 gtest.Assert(j.Get("a.1"), 2) 37 }) 38 } 39 40 func Test_Encode(t *testing.T) { 41 value := g.Slice{1, 2, 3} 42 gtest.Case(t, func() { 43 b, err := gparser.VarToJson(value) 44 gtest.Assert(err, nil) 45 gtest.Assert(b, []byte(`[1,2,3]`)) 46 }) 47 } 48 49 func Test_Decode(t *testing.T) { 50 data := []byte(`{"n":123456789, "m":{"k":"v"}, "a":[1,2,3]}`) 51 gtest.Case(t, func() { 52 j := gparser.New(data) 53 gtest.AssertNE(j, nil) 54 gtest.Assert(j.Get("n"), "123456789") 55 gtest.Assert(j.Get("m"), g.Map{"k": "v"}) 56 gtest.Assert(j.Get("m.k"), "v") 57 gtest.Assert(j.Get("a"), g.Slice{1, 2, 3}) 58 gtest.Assert(j.Get("a.1"), 2) 59 }) 60 } 61 62 func Test_SplitChar(t *testing.T) { 63 data := []byte(`{"n":123456789, "m":{"k":"v"}, "a":[1,2,3]}`) 64 gtest.Case(t, func() { 65 j := gparser.New(data) 66 j.SetSplitChar(byte('#')) 67 gtest.AssertNE(j, nil) 68 gtest.Assert(j.Get("n"), "123456789") 69 gtest.Assert(j.Get("m"), g.Map{"k": "v"}) 70 gtest.Assert(j.Get("m#k"), "v") 71 gtest.Assert(j.Get("a"), g.Slice{1, 2, 3}) 72 gtest.Assert(j.Get("a#1"), 2) 73 }) 74 } 75 76 func Test_ViolenceCheck(t *testing.T) { 77 data := []byte(`{"m":{"a":[1,2,3], "v1.v2":"4"}}`) 78 gtest.Case(t, func() { 79 j := gparser.New(data) 80 gtest.AssertNE(j, nil) 81 gtest.Assert(j.Get("m.a.2"), 3) 82 gtest.Assert(j.Get("m.v1.v2"), nil) 83 j.SetViolenceCheck(true) 84 gtest.Assert(j.Get("m.v1.v2"), 4) 85 }) 86 } 87 88 func Test_GetToVar(t *testing.T) { 89 data := []byte(`{"n":123456789, "m":{"k":"v"}, "a":[1,2,3]}`) 90 gtest.Case(t, func() { 91 var m map[string]string 92 var n int 93 var a []int 94 j := gparser.New(data) 95 gtest.AssertNE(j, nil) 96 97 j.GetToVar("n", &n) 98 j.GetToVar("m", &m) 99 j.GetToVar("a", &a) 100 101 gtest.Assert(n, "123456789") 102 gtest.Assert(m, g.Map{"k": "v"}) 103 gtest.Assert(a, g.Slice{1, 2, 3}) 104 }) 105 } 106 107 func Test_GetMap(t *testing.T) { 108 data := []byte(`{"n":123456789, "m":{"k":"v"}, "a":[1,2,3]}`) 109 gtest.Case(t, func() { 110 j := gparser.New(data) 111 gtest.AssertNE(j, nil) 112 gtest.Assert(j.GetMap("n"), nil) 113 gtest.Assert(j.GetMap("m"), g.Map{"k": "v"}) 114 gtest.Assert(j.GetMap("a"), nil) 115 }) 116 } 117 118 func Test_GetArray(t *testing.T) { 119 data := []byte(`{"n":123456789, "m":{"k":"v"}, "a":[1,2,3]}`) 120 gtest.Case(t, func() { 121 j := gparser.New(data) 122 gtest.AssertNE(j, nil) 123 gtest.Assert(j.GetArray("n"), g.Array{123456789}) 124 gtest.Assert(j.GetArray("m"), g.Array{g.Map{"k": "v"}}) 125 gtest.Assert(j.GetArray("a"), g.Array{1, 2, 3}) 126 }) 127 } 128 129 func Test_GetString(t *testing.T) { 130 data := []byte(`{"n":123456789, "m":{"k":"v"}, "a":[1,2,3]}`) 131 gtest.Case(t, func() { 132 j := gparser.New(data) 133 gtest.AssertNE(j, nil) 134 gtest.AssertEQ(j.GetString("n"), "123456789") 135 gtest.AssertEQ(j.GetString("m"), `{"k":"v"}`) 136 gtest.AssertEQ(j.GetString("a"), `[1,2,3]`) 137 gtest.AssertEQ(j.GetString("i"), "") 138 }) 139 } 140 141 func Test_GetStrings(t *testing.T) { 142 data := []byte(`{"n":123456789, "m":{"k":"v"}, "a":[1,2,3]}`) 143 gtest.Case(t, func() { 144 j := gparser.New(data) 145 gtest.AssertNE(j, nil) 146 gtest.AssertEQ(j.GetStrings("n"), g.SliceStr{"123456789"}) 147 gtest.AssertEQ(j.GetStrings("m"), g.SliceStr{`{"k":"v"}`}) 148 gtest.AssertEQ(j.GetStrings("a"), g.SliceStr{"1", "2", "3"}) 149 gtest.AssertEQ(j.GetStrings("i"), nil) 150 }) 151 } 152 153 func Test_GetInterfaces(t *testing.T) { 154 data := []byte(`{"n":123456789, "m":{"k":"v"}, "a":[1,2,3]}`) 155 gtest.Case(t, func() { 156 j := gparser.New(data) 157 gtest.AssertNE(j, nil) 158 gtest.AssertEQ(j.GetInterfaces("n"), g.Array{123456789}) 159 gtest.AssertEQ(j.GetInterfaces("m"), g.Array{g.Map{"k": "v"}}) 160 gtest.AssertEQ(j.GetInterfaces("a"), g.Array{1, 2, 3}) 161 }) 162 } 163 164 func Test_Len(t *testing.T) { 165 gtest.Case(t, func() { 166 p := gparser.New(nil) 167 p.Append("a", 1) 168 p.Append("a", 2) 169 gtest.Assert(p.Len("a"), 2) 170 }) 171 gtest.Case(t, func() { 172 p := gparser.New(nil) 173 p.Append("a.b", 1) 174 p.Append("a.c", 2) 175 gtest.Assert(p.Len("a"), 2) 176 }) 177 gtest.Case(t, func() { 178 p := gparser.New(nil) 179 p.Set("a", 1) 180 gtest.Assert(p.Len("a"), -1) 181 }) 182 } 183 184 func Test_Append(t *testing.T) { 185 gtest.Case(t, func() { 186 p := gparser.New(nil) 187 p.Append("a", 1) 188 p.Append("a", 2) 189 gtest.Assert(p.Get("a"), g.Slice{1, 2}) 190 }) 191 gtest.Case(t, func() { 192 p := gparser.New(nil) 193 p.Append("a.b", 1) 194 p.Append("a.c", 2) 195 gtest.Assert(p.Get("a"), g.Map{ 196 "b": g.Slice{1}, 197 "c": g.Slice{2}, 198 }) 199 }) 200 gtest.Case(t, func() { 201 p := gparser.New(nil) 202 p.Set("a", 1) 203 err := p.Append("a", 2) 204 gtest.AssertNE(err, nil) 205 gtest.Assert(p.Get("a"), 1) 206 }) 207 } 208 209 func Test_Convert(t *testing.T) { 210 gtest.Case(t, func() { 211 p := gparser.New(`{"name":"gf","bool":true,"int":1,"float":1,"ints":[1,2],"floats":[1,2],"time":"2019-06-12","person": {"name": "gf"}}`) 212 gtest.Assert(p.GetVar("name").String(), "gf") 213 gtest.Assert(p.GetString("name"), "gf") 214 gtest.Assert(p.GetBool("bool"), true) 215 gtest.Assert(p.GetInt("int"), 1) 216 gtest.Assert(p.GetInt8("int"), 1) 217 gtest.Assert(p.GetInt16("int"), 1) 218 gtest.Assert(p.GetInt32("int"), 1) 219 gtest.Assert(p.GetInt64("int"), 1) 220 gtest.Assert(p.GetUint("int"), 1) 221 gtest.Assert(p.GetUint8("int"), 1) 222 gtest.Assert(p.GetUint16("int"), 1) 223 gtest.Assert(p.GetUint32("int"), 1) 224 gtest.Assert(p.GetUint64("int"), 1) 225 gtest.Assert(p.GetInts("ints")[0], 1) 226 gtest.Assert(p.GetFloat32("float"), 1) 227 gtest.Assert(p.GetFloat64("float"), 1) 228 gtest.Assert(p.GetFloats("floats")[0], 1) 229 gtest.Assert(p.GetTime("time").Format("2006-01-02"), "2019-06-12") 230 gtest.Assert(p.GetGTime("time").Format("Y-m-d"), "2019-06-12") 231 gtest.Assert(p.GetDuration("time").String(), "0s") 232 name := struct { 233 Name string 234 }{} 235 err := p.GetToStruct("person", &name) 236 gtest.Assert(err, nil) 237 gtest.Assert(name.Name, "gf") 238 gtest.Assert(p.ToMap()["name"], "gf") 239 err = p.ToStruct(&name) 240 gtest.Assert(err, nil) 241 gtest.Assert(name.Name, "gf") 242 p.Dump() 243 244 p = gparser.New(`[0,1,2]`) 245 gtest.Assert(p.ToArray()[0], 0) 246 247 err = gparser.VarToStruct(`{"name":"gf"}`, &name) 248 gtest.Assert(err, nil) 249 gtest.Assert(name.Name, "gf") 250 }) 251 } 252 253 func Test_Convert2(t *testing.T) { 254 gtest.Case(t, func() { 255 xmlArr := []byte{60, 114, 111, 111, 116, 47, 62} 256 p := gparser.New(`<root></root>`) 257 arr, err := p.ToXml("root") 258 gtest.Assert(err, nil) 259 gtest.Assert(arr, xmlArr) 260 arr, err = gparser.VarToXml(`<root></root>`, "root") 261 gtest.Assert(err, nil) 262 gtest.Assert(arr, xmlArr) 263 264 arr, err = p.ToXmlIndent("root") 265 gtest.Assert(err, nil) 266 gtest.Assert(arr, xmlArr) 267 arr, err = gparser.VarToXmlIndent(`<root></root>`, "root") 268 gtest.Assert(err, nil) 269 gtest.Assert(arr, xmlArr) 270 271 p = gparser.New(`{"name":"gf"}`) 272 str, err := p.ToJsonString() 273 gtest.Assert(err, nil) 274 gtest.Assert(str, `{"name":"gf"}`) 275 str, err = gparser.VarToJsonString(`{"name":"gf"}`) 276 gtest.Assert(err, nil) 277 gtest.Assert(str, `{"name":"gf"}`) 278 279 jsonIndentArr := []byte{123, 10, 9, 34, 110, 97, 109, 101, 34, 58, 32, 34, 103, 102, 34, 10, 125} 280 arr, err = p.ToJsonIndent() 281 gtest.Assert(err, nil) 282 gtest.Assert(arr, jsonIndentArr) 283 arr, err = gparser.VarToJsonIndent(`{"name":"gf"}`) 284 gtest.Assert(err, nil) 285 gtest.Assert(arr, jsonIndentArr) 286 287 str, err = p.ToJsonIndentString() 288 gtest.Assert(err, nil) 289 gtest.Assert(str, "{\n\t\"name\": \"gf\"\n}") 290 str, err = gparser.VarToJsonIndentString(`{"name":"gf"}`) 291 gtest.Assert(err, nil) 292 gtest.Assert(str, "{\n\t\"name\": \"gf\"\n}") 293 294 yamlArr := []byte{124, 50, 10, 10, 32, 32, 110, 97, 109, 101, 58, 103, 102, 10} 295 p = gparser.New(` 296 name:gf 297 `) 298 arr, err = p.ToYaml() 299 gtest.Assert(err, nil) 300 gtest.Assert(arr, yamlArr) 301 arr, err = gparser.VarToYaml(` 302 name:gf 303 `) 304 gtest.Assert(err, nil) 305 gtest.Assert(arr, yamlArr) 306 307 tomlArr := []byte{110, 97, 109, 101, 32, 61, 32, 34, 103, 102, 34, 10} 308 p = gparser.New(` 309 name= "gf" 310 `) 311 arr, err = p.ToToml() 312 gtest.Assert(err, nil) 313 gtest.Assert(arr, tomlArr) 314 arr, err = gparser.VarToToml(` 315 name= "gf" 316 `) 317 gtest.Assert(err, nil) 318 gtest.Assert(arr, tomlArr) 319 }) 320 }