Skip to content

Commit c5f317c

Browse files
committed
更新
1 parent eabe934 commit c5f317c

2 files changed

Lines changed: 77 additions & 10 deletions

File tree

array/array.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -408,12 +408,12 @@ func (this *Array) Set(value any, path ...any) (*Array, error) {
408408

409409
switch {
410410
case sourceValue.Kind() == reflect.Map:
411-
sourceType := sourceValue.Type()
411+
sourceType := sourceValue.Type()
412412

413-
pathSegValue, ok := this.convertTo(sourceType.Key(), path[target])
414-
if !ok {
415-
return nil, fmt.Errorf("convert failed to resolve path segment '%v': field '%v' was error", target, pathSeg)
416-
}
413+
pathSegValue, ok := this.convertTo(sourceType.Key(), path[target])
414+
if !ok {
415+
return nil, fmt.Errorf("convert failed to resolve path segment '%v': field '%v' was error", target, pathSeg)
416+
}
417417

418418
if target == len(path)-1 {
419419
valueValue, ok := this.convertTo(sourceType.Elem(), value)

array/array_test.go

Lines changed: 72 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ var (
5050
789.156,
5151
},
5252
},
53+
"hhTy66": &map[int]any{
54+
777: &[]float64{
55+
12.3,
56+
32.5,
57+
22.56,
58+
789.156,
59+
},
60+
},
5361
"kJh21ay": map[string]any{
5462
"Hjk2": "fccDcc",
5563
"23rt": "^hgcF5c",
@@ -58,6 +66,12 @@ var (
5866
1231,
5967
},
6068
},
69+
"kJh21ay22": map[int64]any{
70+
33: []any{
71+
"adfa",
72+
1231,
73+
},
74+
},
6175
},
6276
}
6377
)
@@ -846,7 +860,9 @@ func Test_FlattenIncludeEmpty(t *testing.T) {
846860
func Test_JSONPointer(t *testing.T) {
847861
assert := assertT(t)
848862

849-
json1, _ := ParseJSON([]byte(`{"foo":[{"bar":"1"},{"bar":"2"}]}`))
863+
data := []byte(`{"foo":[{"bar":"1"},{"bar":"2"}]}`)
864+
865+
json1, _ := ParseJSON(data)
850866

851867
testData := []struct {
852868
key string
@@ -868,16 +884,43 @@ func Test_JSONPointer(t *testing.T) {
868884
`{"bar":"2"}`,
869885
"map[string]any",
870886
},
887+
{
888+
"/foo/5",
889+
`null`,
890+
"not find",
891+
},
892+
{
893+
"/",
894+
`null`,
895+
"null",
896+
},
871897
}
872898

873899
for _, v := range testData {
874900
check, err := json1.JSONPointer(v.key)
875-
if err != nil {
876-
t.Fatal(err)
877-
}
901+
if err != nil {
902+
t.Fatal(err)
903+
}
904+
905+
assert(check.String(), v.expected, v.msg)
906+
}
907+
908+
var dst any
909+
json.Unmarshal(data, &dst)
910+
911+
for _, v := range testData {
912+
check, err := JSONPointer(dst, v.key)
913+
if err != nil {
914+
t.Fatal(err)
915+
}
878916

879917
assert(check.String(), v.expected, v.msg)
880918
}
919+
920+
_, err := json1.JSONPointer("foo/1")
921+
if err == nil {
922+
t.Error("value should not have been found in foo")
923+
}
881924
}
882925

883926
func Test_Set(t *testing.T) {
@@ -907,7 +950,7 @@ func Test_Set(t *testing.T) {
907950
t.Errorf("Unexpected value: %v != %v", act, exp)
908951
}
909952

910-
// ========
953+
// ========
911954

912955
arrData2 := map[string]any{
913956
"a": 123,
@@ -1023,6 +1066,11 @@ func Test_SetMap(t *testing.T) {
10231066
t.Fatal(err)
10241067
}
10251068

1069+
_, err = obj2.Set(133.122333, "b", "kJh21ay22", "ftd")
1070+
if err == nil {
1071+
t.Error("Set should error")
1072+
}
1073+
10261074
res2 := fmt.Sprintf("%v", obj2.Sub("b.hhTy3").Value())
10271075

10281076
check2 := `&map[111:hccccc 222:hddddd 333:map[qq1:qq1ccccc qq2:qq2ddddd qq3:qq3fffff] 666:133.122333]`
@@ -1180,6 +1228,9 @@ func Test_BadIndexes(t *testing.T) {
11801228
if act := obj2.Sub("b.ddd").Index(4).Value(); act != nil {
11811229
t.Errorf("Unexpected value returned: %v != %v", nil, act)
11821230
}
1231+
if act := obj2.Sub("b.hhTy66.777").Index(4).Value(); act != nil {
1232+
t.Errorf("Unexpected value returned: %v != %v", nil, act)
1233+
}
11831234

11841235
oo := obj2.Sub("b.ddd")
11851236

@@ -1285,6 +1336,11 @@ func Test_Deletes(t *testing.T) {
12851336
if actual2 := jsonParsed2.Sub("b.t666").String(); actual2 != expected2 {
12861337
t.Errorf("Unexpected result from deletes: %v != %v", actual2, expected2)
12871338
}
1339+
1340+
jsonParsed3 := New(nil)
1341+
if err := jsonParsed3.Delete("b", "ff", 333); err == nil {
1342+
t.Error("data should return error")
1343+
}
12881344
}
12891345

12901346
func Test_DeletesWithSlices(t *testing.T) {
@@ -1417,6 +1473,17 @@ func Test_BasicWithDecoder(t *testing.T) {
14171473
checkNumber("test.float", "6.66")
14181474
}
14191475

1476+
func Test_BadWithDecoder(t *testing.T) {
1477+
sample := []byte(`{"test":{"int":10, "float":6.66}`)
1478+
dec := json.NewDecoder(bytes.NewReader(sample))
1479+
dec.UseNumber()
1480+
1481+
_, err := ParseJSONDecoder(dec)
1482+
if err == nil {
1483+
t.Error("data should not have been found in ParseJSONDecoder")
1484+
}
1485+
}
1486+
14201487
func Test_isPathShadowedInDeepMap(t *testing.T) {
14211488
assert := assertT(t)
14221489

0 commit comments

Comments
 (0)