Skip to content

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

@the729

Description

@the729

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions