Skip to content
This repository has been archived by the owner on Nov 18, 2021. It is now read-only.

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

Answered by mpvl
NReilingh asked this question in Q&A
Discussion options

You must be logged in to vote

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: 2 comments 1 reply

Comment options

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

Answer selected by NReilingh
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants