Skip to content

Commit b4f2126

Browse files
committed
fix: #4
1 parent 7d6e22c commit b4f2126

File tree

2 files changed

+76
-7
lines changed

2 files changed

+76
-7
lines changed

ognl.go

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,21 @@ import (
99
)
1010

1111
var ErrInvalidStructure = errors.New("the structure cannot continue")
12+
1213
var ErrSliceSubscript = errors.New("invalid slice subscript")
14+
1315
var ErrMapKeyMustString = errors.New("map key must be string")
16+
1417
var ErrMapKeyMustInt = errors.New("map key must be int")
18+
1519
var ErrIndexOutOfBounds = errors.New("index out of bounds")
20+
1621
var ErrStructIndexOutOfBounds = errors.New("struct index out of bounds")
22+
1723
var ErrParseInt = errors.New("parse int error")
24+
1825
var ErrUnableExpand = errors.New("unable to expand")
26+
1927
var ErrInvalidValue = errors.New("invalid value")
2028

2129
// Type is Result type
@@ -535,16 +543,20 @@ func parseString(t reflect.Type, v reflect.Value, value string) (interface{}, Ty
535543
return nil, Invalid, ErrSliceSubscript
536544

537545
case reflect.Struct:
538-
v := v.FieldByName(value)
539-
if !v.IsValid() {
546+
rv := v.FieldByName(value)
547+
if !rv.IsValid() {
540548
return nil, Invalid, nil
541549
}
542550

543-
if v.CanInterface() {
544-
return v.Interface(), Type(v.Kind()), nil
551+
if rv.CanInterface() {
552+
return rv.Interface(), Type(rv.Kind()), nil
545553
}
546554

547-
return reflect.NewAt(v.Type(), unsafe.Pointer(v.UnsafeAddr())).Elem().Interface(), Type(v.Kind()), nil
555+
cp := reflect.New(v.Type()).Elem()
556+
cp.Set(v)
557+
rv = cp.FieldByName(value)
558+
559+
return reflect.NewAt(rv.Type(), unsafe.Pointer(rv.UnsafeAddr())).Elem().Interface(), Type(rv.Kind()), nil
548560

549561
default:
550562
return nil, Invalid, ErrInvalidStructure
@@ -598,9 +610,14 @@ func parseInt(t reflect.Type, v reflect.Value, tokenValue int) (interface{}, Typ
598610
return nil, Invalid, nil
599611
}
600612

601-
if v.CanInterface() {
613+
if value.CanInterface() {
602614
return value.Interface(), Type(value.Kind()), nil
603615
}
616+
617+
cp := reflect.New(v.Type()).Elem()
618+
cp.Set(v)
619+
value = cp.Field(tokenValue)
620+
604621
return reflect.NewAt(value.Type(), unsafe.Pointer(value.UnsafeAddr())).Elem().Interface(), Type(value.Kind()), nil
605622

606623
default:
@@ -663,6 +680,10 @@ func deployment(t reflect.Type, v reflect.Value) ([]interface{}, Type, error) {
663680
if value.CanInterface() {
664681
ret = append(ret, value.Interface())
665682
} else {
683+
cp := reflect.New(v.Type()).Elem()
684+
cp.Set(v)
685+
value = cp.Field(i)
686+
666687
ret = append(ret, reflect.NewAt(value.Type(), unsafe.Pointer(value.UnsafeAddr())).Elem().Interface())
667688
}
668689
}

ognl_test.go

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package ognl
22

33
import (
4-
"github.com/stretchr/testify/assert"
54
"testing"
5+
6+
"github.com/stretchr/testify/assert"
67
)
78

89
type Mock struct {
@@ -106,6 +107,17 @@ func TestGet(t *testing.T) {
106107
}
107108
}
108109

110+
for index, v := range test {
111+
vv := Get(*t1, v.query)
112+
if !assert.Equal(t, v.effective, vv.Effective()) {
113+
t.Errorf("effective fault expected:%t, got:%t, index:%d", v.effective, vv.Effective(), index)
114+
return
115+
}
116+
if !assert.Equal(t, v.value, vv.Value()) {
117+
t.Errorf("no equal index:%d, query:%s, expected:%v, got:%v", index, v.query, v.value, vv.Value())
118+
}
119+
}
120+
109121
p := Parse(t1)
110122
for index, v := range test {
111123
vv := p.Get(v.query)
@@ -118,6 +130,18 @@ func TestGet(t *testing.T) {
118130
}
119131
}
120132

133+
p = Parse(*t1)
134+
for index, v := range test {
135+
vv := p.Get(v.query)
136+
if !assert.Equal(t, v.effective, vv.Effective()) {
137+
t.Errorf("effective fault expected:%t, got:%t", v.effective, vv.Effective())
138+
return
139+
}
140+
if !assert.Equal(t, v.value, vv.Value()) {
141+
t.Errorf("no equal index:%d, query:%s, expected:%v, got:%v", index, v.query, v.value, vv.Value())
142+
}
143+
}
144+
121145
test = []struct {
122146
query string
123147
value interface{}
@@ -147,6 +171,18 @@ func TestGet(t *testing.T) {
147171
}
148172
}
149173

174+
g1 = Get(*t1, "List")
175+
for index, v := range test {
176+
vv := g1.Get(v.query)
177+
if !assert.Equal(t, v.effective, vv.Effective()) {
178+
t.Errorf("effective fault expected:%t, got:%t", v.effective, vv.Effective())
179+
return
180+
}
181+
if !assert.Equal(t, v.value, vv.Value()) {
182+
t.Errorf("no equal index:%d, query:%s, expected:%v, got:%v", index, v.query, v.value, vv.Value())
183+
}
184+
}
185+
150186
test = []struct {
151187
query string
152188
value interface{}
@@ -166,4 +202,16 @@ func TestGet(t *testing.T) {
166202
t.Errorf("no equal index:%d, query:%s, expected:%v, got:%v", index, v.query, v.value, vv.Value())
167203
}
168204
}
205+
206+
g1 = Get(*t1, "")
207+
for index, v := range test {
208+
vv := g1.Get(v.query)
209+
if !assert.Equal(t, v.effective, vv.Effective()) {
210+
t.Errorf("effective fault expected:%t, got:%t", v.effective, vv.Effective())
211+
return
212+
}
213+
if !assert.Equal(t, v.value, vv.Values()) {
214+
t.Errorf("no equal index:%d, query:%s, expected:%v, got:%v", index, v.query, v.value, vv.Value())
215+
}
216+
}
169217
}

0 commit comments

Comments
 (0)