[Request/Question] Support proxies for `expr.Env`
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 5/5
- Thời gian dự kiến
- Hơn một tuần
- Mức phù hợp với người mới
- 35/100
Hướng nghiên cứu
Bắt đầu từ việc xử lý expr.Env và lần theo lookup cho biểu thức ví dụ foo.bar[keyName], bao gồm cả đường dẫn ProxyEnv/GetValue được đề xuất. Trước tiên, hãy giải quyết hành vi của interface và proxy lồng nhau; được xem là hoàn thành khi thiết kế được chọn hỗ trợ các giá trị động ở root và các giá trị lồng nhau mà không yêu cầu bên gọi xây dựng lại các map.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Greetings.
I've a question - does expr support having proxies as expr.Env (something similar to Proxy feature in JS)?
I have a situation where I've to deal with a chain of objects as env that have prototype inheritance (also like in JS).
As env itself is dynamic (might depend on context, environment vars, etc), in order to build expr.Env each time I need to run an expression - it's necessary to traverse a whole prototype chain and create a new map.
Feature Benefits
This feature will allow to handle complex cases such as:
- Cases when value have to be pulled dynamically in runtime.
- To abstract away env when it has a complex structure, such as prototype chain.
- Avoid allocating additional maps or structs for env.
Proposed solutions
Global & Nested Proxy
Ideally, to handle such cases, expr.Env could consume a ProxyEnv interface that will be called to get a variable:
// feel free to provide a better name
type ProxyEnv interface {
GetValue(path string) (bool, any)
}
If returned value implements ProxyEnv, it also should be treated as proxy:
func example() {
// Let's assume expression is `foo.bar[keyName]`
keyName, _ := getValue(env, "keyName")
foo, _ := getValue(env, "foo")
bar, _ := getValue(bar, "bar")
result, _ := getValue(bar, keyName)
}
func getValue(env any, key string) (any, bool) {
switch t := env.(type) {
case ProxyEnv:
return t.GetValue(key)
case map[string]any:
v, ok := t[key]
return v, ok
default:
// use reflection
return getUsingReflect(env, key)
}
}
Pros
- Easier to implement
Cons
- Additional type casting
Alternative Solutions
Please feel free to provide a better more efficient solution.
- Ngôn ngữ chính
- Go
- Star
- 8k
- Fork
- 529
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Hướng dẫn đóng góp
Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của expr-lang/expr
-
docs needed
Độ khó 1/5 1-3 giờ Mức phù hợp với người mới 62/100
-
`find`, `findIndex`, `first` and `get` are type-checked as the element type where they return nil Đang mở
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 65/100
-
Release latest master branch Đang mở
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 35/100
-
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 35/100
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 55/100
Tất cả issue của expr-lang/expr
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
-
bug group: validation priority: low
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
codecheckers/chekhov#51 ·
-
Creating worktree from an existing remote branch with a slash in it, has unexpected behaviour Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100