Skip to content

Commit 42a8d46

Browse files
committed
feat:anonymous monitoring
1 parent 404a740 commit 42a8d46

File tree

2 files changed

+74
-4
lines changed

2 files changed

+74
-4
lines changed

ognl.go

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ func parseString(t reflect.Type, v reflect.Value, value string) (interface{}, Ty
524524
if !v.Elem().IsValid() {
525525
return nil, Invalid, nil
526526
}
527-
return parseString(t, v.Elem(), value)
527+
return parseString(t.Elem(), v.Elem(), value)
528528

529529
case reflect.Map:
530530
// MUST map key is string
@@ -549,14 +549,29 @@ func parseString(t reflect.Type, v reflect.Value, value string) (interface{}, Ty
549549
}
550550

551551
if rv.CanInterface() {
552+
nv, nt, ne := parseString(reflect.TypeOf(rv.Interface()), reflect.ValueOf(rv.Interface()), value)
553+
if ne == nil && nt != Invalid {
554+
return nv, nt, ne
555+
}
552556
return rv.Interface(), Type(rv.Kind()), nil
553557
}
554558

555559
cp := reflect.New(v.Type()).Elem()
556560
cp.Set(v)
557561
rv = cp.FieldByName(value)
558562

559-
return reflect.NewAt(rv.Type(), unsafe.Pointer(rv.UnsafeAddr())).Elem().Interface(), Type(rv.Kind()), nil
563+
res := reflect.NewAt(rv.Type(), unsafe.Pointer(rv.UnsafeAddr())).Elem().Interface()
564+
565+
rt, _ := t.FieldByName(value)
566+
567+
if rt.Anonymous {
568+
nv, nt, ne := parseString(reflect.TypeOf(res), reflect.ValueOf(res), value)
569+
if ne == nil && nt != Invalid {
570+
return nv, nt, ne
571+
}
572+
}
573+
574+
return res, Type(rv.Kind()), nil
560575

561576
default:
562577
return nil, Invalid, ErrInvalidStructure
@@ -575,7 +590,7 @@ func parseInt(t reflect.Type, v reflect.Value, tokenValue int) (interface{}, Typ
575590
if !v.Elem().IsValid() {
576591
return nil, Invalid, nil
577592
}
578-
return parseInt(t, v.Elem(), tokenValue)
593+
return parseInt(t.Elem(), v.Elem(), tokenValue)
579594
case reflect.Map:
580595
// MUST map key is int
581596
if t.Key().Kind() != reflect.Int {
@@ -611,14 +626,29 @@ func parseInt(t reflect.Type, v reflect.Value, tokenValue int) (interface{}, Typ
611626
}
612627

613628
if value.CanInterface() {
629+
nv, nt, ne := parseInt(reflect.TypeOf(value), reflect.ValueOf(value), tokenValue)
630+
if ne == nil && nt != Invalid {
631+
return nv, nt, ne
632+
}
614633
return value.Interface(), Type(value.Kind()), nil
615634
}
616635

617636
cp := reflect.New(v.Type()).Elem()
618637
cp.Set(v)
619638
value = cp.Field(tokenValue)
620639

621-
return reflect.NewAt(value.Type(), unsafe.Pointer(value.UnsafeAddr())).Elem().Interface(), Type(value.Kind()), nil
640+
res := reflect.NewAt(value.Type(), unsafe.Pointer(value.UnsafeAddr())).Elem().Interface()
641+
642+
rt := t.Field(tokenValue)
643+
644+
if rt.Anonymous {
645+
nv, nt, ne := parseInt(reflect.TypeOf(res), reflect.ValueOf(res), tokenValue)
646+
if ne == nil && nt != Invalid {
647+
return nv, nt, ne
648+
}
649+
}
650+
651+
return res, Type(value.Kind()), nil
622652

623653
default:
624654
return nil, Invalid, ErrParseInt

ognl_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,3 +215,43 @@ func TestGet(t *testing.T) {
215215
}
216216
}
217217
}
218+
219+
type Repetition struct {
220+
Repetition string
221+
}
222+
223+
type RepetitionDetail struct {
224+
*Repetition
225+
}
226+
227+
func TestRepetition(t *testing.T) {
228+
229+
r := RepetitionDetail{
230+
Repetition: &Repetition{
231+
Repetition: "r1",
232+
},
233+
}
234+
235+
vv := Get(r, "Repetition").Value()
236+
assert.Equal(t, vv, "r1")
237+
}
238+
239+
type repetition struct {
240+
repetition string
241+
}
242+
243+
type repetitionDetail struct {
244+
*repetition
245+
}
246+
247+
func TestRepetition2(t *testing.T) {
248+
249+
r := repetitionDetail{
250+
repetition: &repetition{
251+
repetition: "r2",
252+
},
253+
}
254+
255+
vv := Get(r, "repetition").Value()
256+
assert.Equal(t, vv, "r2")
257+
}

0 commit comments

Comments
 (0)