-
-
Notifications
You must be signed in to change notification settings - Fork 463
-
I have the following struct that serves as an environment input:
type struct InputStruct {
Enabled *bool `json:"enabled"`
}
var v InputStruct
// unmarshal json into struct v
// ...
env := map[string]any {
"v": v,
}
I need to write an expression that evaluates the Enabled field and returns:
- "default" when the pointer is nil
- "enabled" when the dereferenced value is true
- "disabled" when the dereferenced value is false
The best solution I've come up with so far is:
v.Enabled == nil ? 'default' : ( (v.Enabled??'nil') ? 'enabled' : 'disabled' )
Is this the most idiomatic way to handle this case in Expr?
Beta Was this translation helpful? Give feedback.
All reactions
Answered by
antonmedv
Sep 18, 2025
This is the bug in expr compilation as it can't dereference inside "if".
I created a issue to track all deref problems: #836
Workaround is to "trigger" dereferencing:
v.Enabled == nil ? 'default' : ( v.Enabled == true ? 'enabled' : 'disabled' )
Replies: 1 comment
-
This is the bug in expr compilation as it can't dereference inside "if".
I created a issue to track all deref problems: #836
Workaround is to "trigger" dereferencing:
v.Enabled == nil ? 'default' : ( v.Enabled == true ? 'enabled' : 'disabled' )
Beta Was this translation helpful? Give feedback.
All reactions
0 replies
Answer selected by
antonmedv
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment