Skip to content

Commit e114b15

Browse files
committed
feat: add proxies support for OpFetch
1 parent 593f93f commit e114b15

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

vm/runtime/runtime.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,27 @@ import (
1010
"github.com/expr-lang/expr/internal/deref"
1111
)
1212

13+
// Proxy is an interface that allows intercepting object property read and write operations.
14+
type Proxy interface {
15+
ProxyGetter
16+
}
17+
18+
// ProxyGetter is an interface that allows intercepting object property access.
19+
type ProxyGetter interface {
20+
// GetProperty returns the value of the property with the given key.
21+
GetProperty(key string) (any, bool)
22+
}
23+
1324
func Fetch(from, i any) any {
25+
if proxy, ok := from.(ProxyGetter); ok {
26+
r, ok := proxy.GetProperty(i.(string))
27+
if !ok {
28+
panic(fmt.Sprintf("cannot fetch %v from %T", i, from))
29+
}
30+
31+
return r
32+
}
33+
1434
v := reflect.ValueOf(from)
1535
if v.Kind() == reflect.Invalid {
1636
panic(fmt.Sprintf("cannot fetch %v from %T", i, from))

0 commit comments

Comments
 (0)