expr.ConstExpr panics if function was defined with expr.Function instead of env
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 3/5
- Thời gian dự kiến
- 1-2 ngày
- Mức phù hợp với người mới
- 40/100
Hướng nghiên cứu
Tái hiện ví dụ Go Playground bằng cách sử dụng expr.ConstExpr, expr.Function và một môi trường struct. Theo dõi cách ConstExpr phân giải hàm và xác minh rằng quá trình biên dịch và thực thi xử lý cả hai lệnh gọi identity mà không panic, đồng thời giữ nguyên giới hạn một chữ ký đã được ghi rõ trong tài liệu.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Short description: expr.ConstExpr requires that the given function was defined in the environment and does not take into account those functions defined with expr.Function.
Why it matters: This prevents usage of structs as environment in many cases, since it's impractical to define all the functions in the environment struct so we resort to add functionality with expr.Function.
Workaround: It is possible to directly define the signature in the configuration if we directly set the value for the function in the expr/conf.(*Config).ConstFns.
Limitations: a ConstExpr function can have a single signature because for each function name, we can only assign one value in ConstFns. Maybe worth creating a Feature Request issue?
Example (Go Playground)
package main
import (
"fmt"
"github.com/expr-lang/expr"
)
type MyEnv struct {
Value string
}
func identity(x any) any {
return x
}
func main() {
code := `[identity(1), identity(Value)]`
options := []expr.Option{
expr.Env(MyEnv{}),
expr.Function(
"identity",
func(params ...any) (any, error) {
return identity(params[0]), nil
},
identity,
),
expr.ConstExpr("identity"), // Mark identity func as constant expression.
}
program, err := expr.Compile(code, options...)
if err != nil {
panic(err)
}
env := MyEnv{
Value: "thing",
}
output, err := expr.Run(program, env)
if err != nil {
panic(err)
}
fmt.Println(output)
}
Workaround for single-signature (Go Playground)
package main
import (
"fmt"
"reflect"
"github.com/expr-lang/expr"
"github.com/expr-lang/expr/conf"
)
type MyEnv struct {
Value string
}
func identity(x any) any {
return x
}
func main() {
code := `[identity(1), identity(Value)]`
options := []expr.Option{
expr.Env(MyEnv{}),
expr.Function(
"identity",
func(params ...any) (any, error) {
return identity(params[0]), nil
},
identity,
),
func(c *conf.Config) {
c.ConstFns["identity"] = reflect.ValueOf(identity) // Mark identity func as constant expression.
},
}
program, err := expr.Compile(code, options...)
if err != nil {
panic(err)
}
env := MyEnv{
Value: "thing",
}
output, err := expr.Run(program, env)
if err != nil {
panic(err)
}
fmt.Println(output)
}
- 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ó 1/5 Dưới một giờ Mức phù hợp với người mới 60/100
github/gh-aw-mcpg#13748 ·
-
agentic-workflows
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
-
needs-triage
Độ 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 65/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
googleapis/librarian#7670 · 2 bình luận ·