Skip to content

Commit d9da212

Browse files
committed
cmd/cue: go get Go fields with the json omitempty tag as optional
"omitzero" was added in Go 1.24 as an improvement over "omitempty", which works in a more intuitive way with structs and arrays, and supports calling an IsZero method for custom implementations such as time.Time. Fixes #3869. Signed-off-by: Daniel Martí <[email protected]> Change-Id: I9b4c58544a86cf961434d581012aafd0fd91a2c6 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1213251 Reviewed-by: Roger Peppe <[email protected]> TryBot-Result: CUEcueckoo <[email protected]> Unity-Result: CUE porcuepine <[email protected]>
1 parent be6c3db commit d9da212

File tree

2 files changed

+4
-1
lines changed

2 files changed

+4
-1
lines changed

Diff for: cmd/cue/cmd/get_go.go

+3
Original file line numberDiff line numberDiff line change
@@ -1474,8 +1474,11 @@ func (e *extractor) isOptional(f *types.Var, doc *ast.CommentGroup, tag string)
14741474
}
14751475
}
14761476

1477+
// Go 1.24 added the "omitzero" option to encoding/json, an improvement over "omitempty".
1478+
// Note that, as of mid 2025, YAML libraries don't seem to have picked up "omitzero" yet.
14771479
// TODO: also when the type is a list or other kind of pointer.
14781480
return hasFlag(tag, "json", "omitempty", 1) ||
1481+
hasFlag(tag, "json", "omitzero", 1) ||
14791482
hasFlag(tag, "yaml", "omitempty", 1)
14801483
}
14811484

Diff for: cmd/cue/cmd/testdata/script/get_go_types.txtar

+1-1
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ import (
331331
CustomJSON: #CustomJSON
332332
} @go(,struct{CustomJSON})
333333
optionalOmitEmptyJSON?: string @go(OptionalOmitEmptyJSON)
334-
optionalOmitZeroJSON: time.Time @go(OptionalOmitZeroJSON)
334+
optionalOmitZeroJSON?: time.Time @go(OptionalOmitZeroJSON)
335335
optionalOmitEmptyYAML?: string @go(OptionalOmitEmptyYAML)
336336

337337
// +optional

0 commit comments

Comments
 (0)