Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

How to properly dereference a pointer field in environment? #835

Answered by antonmedv
the729 asked this question in General
Discussion options

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?

You must be logged in to vote

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

Comment options

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' )
You must be logged in to vote
0 replies
Answer selected by antonmedv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #828 on September 18, 2025 11:41.

AltStyle によって変換されたページ (->オリジナル) /