expr.ConstExpr panics if function was defined with expr.Function instead of env
まだ誰も着手していません。
評価
調査の方向性
expr.ConstExpr、expr.Function、そして struct 環境を使用して Go Playground の例を再現します。ConstExpr が関数を解決する方法を追跡し、コンパイルと実行が、文書化されている単一シグネチャの制限を維持しながら、両方の identity 呼び出しを panic なしで処理することを検証します。
索引モデルが issue の本文から書いたものです。
説明
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)
}
- 主要言語
- Go
- スター
- 8k
- フォーク
- 529
- PR マージ指標
- 30日以内にマージされた PR はありません
コントリビューションガイド
このリポジトリのコントリビューションガイドは索引されていません
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
expr-lang/expr のほかの issue
-
docs needed
難易度 1/5 1〜3時間 初心者へのやさしさ 62/100
-
難易度 4/5 3〜5日 初心者へのやさしさ 35/100
-
難易度 5/5 1週間以上 初心者へのやさしさ 35/100
-
難易度 3/5 1〜2日 初心者へのやさしさ 55/100
-
log2 builtin オープン
難易度 3/5 1〜2日 初心者へのやさしさ 48/100
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
crossplane/crossplane#7859 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
bazel-contrib/rules_go#4721 · コメント 2 件 ·
-
needs-triage
難易度 1/5 1時間未満 初心者へのやさしさ 92/100
-
bug carvel-triage
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
carvel-dev/kapp-controller#1861 ·
-
area/logging kind/bug
難易度 2/5 1〜3時間 初心者へのやさしさ 86/100