Skip to content

Can I set a value conditionally based on the existence of an optional field? #887

Answered by cueckoo
cueckoo asked this question in Q&A
Discussion options

You must be logged in to vote

Original reply by @mpvl in cuelang/cue#887 (comment)

With the API or in CUE directly?

In CUE directly you can write:

#Type: {
	optional?: string
	result: optional != _|_
  }
  a: #Type
  a: optional: "foo"

Comparing against bottom is not specified in the spec, or at the very least it is poorly defined. I've written (but still need to publish) a relatively minor change, replacing this with builtins like isdefined(optional). But for now this works.

In the API you can do a LookupPath and see whether the resulting value exists.

Replies: 1 comment 1 reply

Comment options

cueckoo
Jul 3, 2021
Collaborator Author

You must be logged in to vote
1 reply
@cueckoo
Comment options

cueckoo Jul 3, 2021
Collaborator Author

Answer selected by cueckoo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant