cue icon indicating copy to clipboard operation
cue copied to clipboard

cmd/cue: simplify `["k"]: v` to `"k"?: v`

Open cueckoo opened this issue 4 years ago • 1 comments

Originally opened by @myitcv in https://github.com/cuelang/cue/issues/1045

What version of CUE are you using (cue version)?

$ cue version
cue version +20ed9abd linux/amd64

Does this issue reproduce with the latest release?

Yes

What did you do?

exec cue fmt -s x.cue
cmp x.cue x.cue.golden

-- x.cue --
package x

["^t"]: 5
-- x.cue.golden --
package x

"^t"?: 5

What did you expect to see?

Test to pass.

What did you see instead?

> exec cue fmt -s x.cue
> cmp x.cue x.cue.golden
[diff -x.cue +x.cue.golden]
 package x

-["^t"]: 5
+"^t"?: 5

As discussed with @mpvl, this would be a useful simplification so that the user can see what they perhaps intended was not actually the case.

Because ["^t"] is equivalent to "^t"?, whereas the user probably intended [=~"^t"] in this case.

cueckoo avatar Jul 03 '21 10:07 cueckoo

Thanks for @mvdan for spotting the errors in the original description here. I've now tweaked the original description to demonstrate that an optional (as opposed to regular) field is the equivalent.

Whether we should rewrite via simplify here (which not everyone does), or have a vet rule, or something else, is not clear.

myitcv avatar Sep 19 '25 11:09 myitcv