-
-
Notifications
You must be signed in to change notification settings - Fork 462
Add support for custom struct tags via environment variable #829
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package environment | ||
|
||
import "os" | ||
|
||
const ( | ||
ExprEnvGotag = "expr__gotag" | ||
DefaultGotag = "expr" | ||
) | ||
|
||
func GetGoTag() string { | ||
if v := os.Getenv(ExprEnvGotag); v != "" { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We definitely do now want to check os env in expr) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. okay, i'll try some other solutions, closing this now. |
||
return v | ||
} else { | ||
return DefaultGotag | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ package runtime | |
|
||
import ( | ||
"fmt" | ||
"github.com/expr-lang/expr/environment" | ||
"math" | ||
"reflect" | ||
|
||
|
@@ -65,7 +66,7 @@ func Fetch(from, i any) any { | |
fieldName := i.(string) | ||
value := v.FieldByNameFunc(func(name string) bool { | ||
field, _ := v.Type().FieldByName(name) | ||
switch field.Tag.Get("expr") { | ||
switch field.Tag.Get(environment.GetGoTag()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Better not introduce another configuration surface and use same conf pgk. |
||
case "-": | ||
return false | ||
case fieldName: | ||
|
@@ -223,7 +224,7 @@ func In(needle any, array any) bool { | |
panic(fmt.Sprintf("cannot use %T as field name of %T", needle, array)) | ||
} | ||
field, ok := v.Type().FieldByName(n.String()) | ||
if !ok || !field.IsExported() || field.Tag.Get("expr") == "-" { | ||
if !ok || !field.IsExported() || field.Tag.Get(environment.GetGoTag()) == "-" { | ||
return false | ||
} | ||
value := v.FieldByIndex(field.Index) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need to specify this from env?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried to set gotag in config like
expr.Compile("", expr.WithGotag("json"))
, but there is someexpr
usage that cannot reach the config value, like in lib.go. Even in where we can reach the config value, it feels code intrusive to pass the value all the way down there. So i picked env since it is easy to read.expr/builtin/lib.go
Lines 420 to 432 in 1c09e5e