Skip to content

Commit 3b2f67b

Browse files
committedOct 28, 2021
chore: update gjson unmarshal
Change-Id: Ifdb9a7c06885fb6f2b665bdc3520e07c320b43ce
1 parent deea063 commit 3b2f67b

File tree

4 files changed

+26
-18
lines changed

4 files changed

+26
-18
lines changed
 

‎binding/bind_test.go

+15-2
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,20 @@ import (
1313
"testing"
1414
"time"
1515

16+
// "github.com/bytedance/go-tagexpr/v2/binding/gjson"
17+
vd "github.com/bytedance/go-tagexpr/v2/validator"
18+
"github.com/davecgh/go-spew/spew"
1619
"github.com/henrylee2cn/ameda"
1720
"github.com/henrylee2cn/goutil/httpbody"
1821
"github.com/stretchr/testify/assert"
1922

2023
"github.com/bytedance/go-tagexpr/v2/binding"
2124
)
2225

26+
func init() {
27+
// gjson.UseJSONUnmarshaler()
28+
}
29+
2330
func TestRawBody(t *testing.T) {
2431
type Recv struct {
2532
S []byte `raw_body:""`
@@ -1107,7 +1114,7 @@ func TestIssue25(t *testing.T) {
11071114

11081115
func TestIssue26(t *testing.T) {
11091116
type Recv struct {
1110-
Type string `json:"type,required" vd:"($=='update_target_threshold' && (target_threshold)$!='-1') || ($=='update_status' && (status)$!='-1')"`
1117+
Type string `json:"type,required" vd:"($=='update_target_threshold' && (TargetThreshold)$!='-1') || ($=='update_status' && (Status)$!='-1')"`
11111118
RuleName string `json:"rule_name,required" vd:"regexp('^rule[0-9]+$')"`
11121119
TargetThreshold string `json:"target_threshold" vd:"regexp('^-?[0-9]+(\\.[0-9]+)?$')"`
11131120
Status string `json:"status" vd:"$=='0' || $=='1'"`
@@ -1133,18 +1140,24 @@ func TestIssue26(t *testing.T) {
11331140

11341141
recv := new(Recv)
11351142
err := json.Unmarshal(b, recv)
1143+
assert.NoError(t, err)
1144+
err = vd.Validate(&recv, true)
1145+
assert.NoError(t, err)
1146+
t.Log(spew.Sdump(recv))
1147+
11361148
header := make(http.Header)
11371149
header.Set("Content-Type", "application/json")
11381150
header.Set("A", "from header")
11391151
cookies := []*http.Cookie{
11401152
{Name: "A", Value: "from cookie"},
11411153
}
1142-
// gjson.UseJSONUnmarshaler()
1154+
11431155
req := newRequest("/1", header, cookies, bytes.NewReader(b))
11441156
binder := binding.New(nil)
11451157
recv2 := new(Recv)
11461158
err = binder.BindAndValidate(&recv2, req, nil)
11471159
assert.NoError(t, err)
1160+
t.Log(spew.Sdump(recv2))
11481161
assert.Equal(t, recv, recv2)
11491162
}
11501163

‎binding/gjson/gjson.go

+7-12
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ package gjson
2424
import (
2525
"encoding/base64"
2626
"encoding/json"
27+
"errors"
2728
"fmt"
2829
"reflect"
2930
"strings"
@@ -66,18 +67,12 @@ func assign(jsval gjson.Result, goval reflect.Value) (err error) {
6667
switch goval.Kind() {
6768
default:
6869
case reflect.Ptr:
69-
if !goval.IsNil() {
70-
newval := reflect.New(goval.Elem().Type())
71-
if err = assign(jsval, newval.Elem()); err != nil {
72-
return err
73-
}
74-
goval.Elem().Set(newval.Elem())
75-
} else {
76-
newval := reflect.New(t.Elem())
77-
if err = assign(jsval, newval.Elem()); err != nil {
78-
return err
79-
}
80-
goval.Set(newval)
70+
if !ameda.InitPointer(goval) {
71+
return errors.New("v cannot be set")
72+
}
73+
newval := ameda.DereferencePtrValue(goval)
74+
if err = assign(jsval, newval); err != nil {
75+
return err
8176
}
8277
case reflect.Struct:
8378
runtimeTypeID := ameda.ValueFrom(goval).RuntimeTypeID()

‎binding/gjson/gjson_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ func TestMap(t *testing.T) {
6060
}
6161
}`)
6262

63-
x3 := X{}
63+
var x3 *X
6464
err = unmarshal(data, &x3)
6565
assert.NoError(t, err)
66-
assert.Equal(t, x, x3)
66+
assert.Equal(t, x, *x3)
6767
}
6868

6969
func TestStruct(t *testing.T) {

‎go.mod

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ module github.com/bytedance/go-tagexpr/v2
33
go 1.14
44

55
require (
6-
github.com/davecgh/go-spew v1.1.1 // indirect
6+
github.com/davecgh/go-spew v1.1.1
77
github.com/henrylee2cn/ameda v1.4.10
88
github.com/henrylee2cn/goutil v0.0.0-20210127050712-89660552f6f8
99
github.com/nyaruka/phonenumbers v1.0.55
1010
github.com/stretchr/testify v1.5.1
11-
github.com/tidwall/gjson v1.6.0
11+
github.com/tidwall/gjson v1.9.3
1212
google.golang.org/protobuf v1.27.1
1313
)

0 commit comments

Comments
 (0)
Please sign in to comment.