-
-
Notifications
You must be signed in to change notification settings - Fork 461
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
Conversation
@antonmedv how do you think about setting custom gotag from system env ? |
) | ||
|
||
func GetGoTag() string { | ||
if v := os.Getenv(ExprEnvGotag); v != "" { |
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 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.
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 | |
} | |
}) |
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 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.
) | ||
|
||
func GetGoTag() string { | ||
if v := os.Getenv(ExprEnvGotag); v != "" { |
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.
We definitely do now want to check os env in expr)
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.
okay, i'll try some other solutions, closing this now.
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.