Skip to content

Commit 0d61d4e

Browse files
committed
fix: fix ExprSelector.Field method
Change-Id: I1f298b644e6fbeac5476bc4df5d9e8252c805151
1 parent ba41dcc commit 0d61d4e

File tree

3 files changed

+37
-6
lines changed

3 files changed

+37
-6
lines changed

selector.go

+16-5
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,16 @@ func (f FieldSelector) Split() (paths []string, name string) {
4444
return nil, s
4545
}
4646

47+
// Parent returns the parent FieldSelector.
48+
func (f FieldSelector) Parent() (string, bool) {
49+
s := string(f)
50+
i := strings.LastIndex(s, FieldSeparator)
51+
if i < 0 {
52+
return "", false
53+
}
54+
return s[:i], true
55+
}
56+
4757
// String returns string type value.
4858
func (f FieldSelector) String() string {
4959
return string(f)
@@ -71,20 +81,21 @@ func (e ExprSelector) Name() string {
7181
return s[atIdx+1:]
7282
}
7383

74-
// Field returns the name of the field it belongs to.
84+
// Field returns the field selector it belongs to.
7585
func (e ExprSelector) Field() string {
7686
s := string(e)
7787
idx := strings.LastIndex(s, ExprNameSeparator)
7888
if idx != -1 {
7989
s = s[:idx]
8090
}
81-
idx = strings.LastIndex(s, FieldSeparator)
82-
if idx != -1 {
83-
return s[idx+1:]
84-
}
8591
return s
8692
}
8793

94+
// ParentField returns the parent field selector it belongs to.
95+
func (e ExprSelector) ParentField() (string, bool) {
96+
return FieldSelector(e.Field()).Parent()
97+
}
98+
8899
// Split returns the field selector and the expression name.
89100
func (e ExprSelector) Split() (field FieldSelector, name string) {
90101
s := string(e)

selector_test.go

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package tagexpr
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestExprSelector(t *testing.T) {
10+
es := ExprSelector("F1.Index")
11+
field, ok := es.ParentField()
12+
assert.True(t, ok)
13+
assert.Equal(t, "F1", field)
14+
}

tagexpr_test.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -738,7 +738,7 @@ func TestNilField(t *testing.T) {
738738
if !ok || !r {
739739
t.Fatal(path, r)
740740
}
741-
t.Log(path, r)
741+
t.Log("path:", path, "es:", es, "val:", r)
742742
cnt++
743743
return nil
744744
})
@@ -795,6 +795,7 @@ func TestIssue3(t *testing.T) {
795795
type C struct {
796796
Id string
797797
Index int32 `vd:"$"`
798+
P *int `vd:"$!=nil"`
798799
}
799800
type A struct {
800801
F1 *C
@@ -804,6 +805,7 @@ func TestIssue3(t *testing.T) {
804805
F1: &C{
805806
Id: "test",
806807
Index: 1,
808+
P: new(int),
807809
},
808810
}
809811
vm := New("vd")
@@ -813,6 +815,10 @@ func TestIssue3(t *testing.T) {
813815
assert.Equal(t, float64(1), eval(), path)
814816
case "F2.Index":
815817
assert.Equal(t, nil, eval(), path)
818+
case "F1.P":
819+
assert.Equal(t, true, eval(), path)
820+
case "F2.P":
821+
assert.Equal(t, false, eval(), path)
816822
}
817823
return nil
818824
})

0 commit comments

Comments
 (0)