github.com/sohaha/zlsgo@v1.7.13-0.20240501141223-10dd1a906f76/zjson/set_test.go (about)

     1  package zjson
     2  
     3  import (
     4  	"strconv"
     5  	"testing"
     6  
     7  	"github.com/sohaha/zlsgo/zstring"
     8  
     9  	"github.com/sohaha/zlsgo"
    10  )
    11  
    12  func TestSet(t *testing.T) {
    13  	var demoData = "{}"
    14  	var err error
    15  	var str string
    16  	tt := zlsgo.NewTest(t)
    17  
    18  	str, err = Set(demoData, "set", "new set")
    19  	tt.EqualExit(true, err == nil)
    20  	t.Log(str, err)
    21  
    22  	str, err = Set(str, "set.b", true)
    23  	tt.EqualExit(true, err == nil)
    24  	t.Log(str, err)
    25  
    26  	str, err = Set(str, "arr1", []string{"one"})
    27  	tt.EqualExit(true, err == nil)
    28  	t.Log(str, err)
    29  
    30  	str, err = SetRaw("", "arr2", "[1,2,3]")
    31  	tt.EqualExit(true, err == nil)
    32  	t.Log(str, err)
    33  
    34  	str, err = Set("", "arr2", "[1,2,3]")
    35  	tt.EqualExit(true, err == nil)
    36  	t.Log(str, err)
    37  
    38  	str, err = SetRaw(str, "arr3.:3", "[1,2,3]")
    39  	tt.EqualExit(true, err == nil)
    40  	t.Log(str, err)
    41  
    42  	str, err = SetRaw(str, "obj", `{"name":"pp"}`)
    43  	tt.EqualExit(true, err == nil)
    44  	t.Log(str, err)
    45  
    46  	str, err = Set(str, "set\\.ff", 1.66)
    47  	tt.EqualExit(true, err == nil)
    48  	t.Log(str, err)
    49  
    50  	str, err = SetRaw(str, "newSet", `"haha"`)
    51  	tt.EqualExit(true, err == nil)
    52  	t.Log(str, err)
    53  
    54  	_, err = SetRaw(str, "", `haha`)
    55  	tt.EqualExit(true, err != nil)
    56  	t.Log(err)
    57  
    58  	newSet := Get(str, "newSet").String()
    59  	t.Log(newSet)
    60  	newStr, err := Delete(str, "newSet")
    61  	tt.EqualExit(false, Get(newStr, "newSet").Exists())
    62  	t.Log(newStr, str, err)
    63  
    64  	strBytes := []byte("{}")
    65  
    66  	strBytes, err = SetBytes(strBytes, "setBytes", "new set")
    67  	tt.EqualExit(true, err == nil)
    68  	t.Log(string(strBytes), err)
    69  
    70  	strBytes, err = SetRawBytes(strBytes, "setRawBytes", []byte("Raw"))
    71  	tt.EqualExit(true, err == nil)
    72  	t.Log(string(strBytes), err)
    73  
    74  	strBytes, err = SetRawBytes(strBytes, "set\\.other", []byte("Raw"))
    75  	tt.EqualExit(true, err == nil)
    76  	t.Log(string(strBytes), err)
    77  
    78  	strBytes, err = SetRawBytes(strBytes, "set.other", []byte("Raw"))
    79  	tt.EqualExit(true, err == nil)
    80  	t.Log(string(strBytes), err)
    81  
    82  	_, _ = SetBytes(strBytes, "setBytes2.s", "new set")
    83  
    84  	strBytes, err = SetBytesOptions(strBytes, "setBytes3.op", "new set", &Options{Optimistic: true, ReplaceInPlace: true})
    85  
    86  	t.Log(string(strBytes), err)
    87  	_, _ = DeleteBytes(strBytes, "setRawBytes")
    88  
    89  	var j = struct {
    90  		Name string `json:"n"`
    91  	}{"isName"}
    92  	jj, err := Marshal(j)
    93  	t.Log(string(jj), err)
    94  	t.Log(Stringify(j))
    95  }
    96  
    97  func TestSetSt(tt *testing.T) {
    98  	var j = struct {
    99  		Name string `json:"n"`
   100  	}{"isName"}
   101  	t := zlsgo.NewTest(tt)
   102  	json, _ := Set("{}", "code", 200)
   103  	json, _ = Set(json, "code2", uint(200))
   104  	json, _ = Set(json, "int8", int8(8))
   105  	json, _ = Set(json, "int32", int32(200))
   106  	json, _ = Set(json, "int64", int64(200))
   107  	json, _ = Set(json, "uint8", uint8(8))
   108  	json, _ = Set(json, "uint32", uint32(200))
   109  	json, _ = Set(json, "uint64", uint64(200))
   110  	json, _ = SetOptions(json, "code2", 200.01, &Options{
   111  		Optimistic: true,
   112  	})
   113  	tt.Log(json)
   114  	json, _ = Set(json, "data", j)
   115  	tt.Log(json)
   116  	t.Equal("isName", Get(json, "data.n").String())
   117  }
   118  
   119  func BenchmarkSet(b *testing.B) {
   120  	s := zstring.Rand(100)
   121  	json := "{}"
   122  	b.ResetTimer()
   123  	for i := 0; i < b.N; i++ {
   124  		_, _ = Set(json, strconv.Itoa(i), s)
   125  	}
   126  }
   127  
   128  func BenchmarkSetBytes(b *testing.B) {
   129  	s := []byte(zstring.Rand(100))
   130  	json := []byte("{}")
   131  	b.ResetTimer()
   132  	for i := 0; i < b.N; i++ {
   133  		_, _ = SetBytes(json, strconv.Itoa(i), s)
   134  	}
   135  }