Skip to content

Conversation

NickYadance
Copy link

Fix: #234

Add support for custom struct tags via environment variable, set gotags to read by system env variables.

To easily reference field by custom go tags like json, mapstructre, which is especially useful when dealing with generated go structs like protobuf.

@NickYadance
Copy link
Author

@antonmedv how do you think about setting custom gotag from system env ?

)

func GetGoTag() string {
if v := os.Getenv(ExprEnvGotag); v != "" {
Copy link
Member

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?)

Copy link
Author

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 some expr 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

case reflect.Struct:
fieldName := i.(string)
value := v.FieldByNameFunc(func(name string) bool {
field, _ := v.Type().FieldByName(name)
switch field.Tag.Get("expr") {
case "-":
return false
case fieldName:
return true
default:
return name == fieldName
}
})

@@ -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()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better not introduce another configuration surface and use same conf pgk.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Enhance expr.Env() options on structs
2 participants