Skip to content

Commit 4d852c8

Browse files
committed
internal/encoding/gotypes: disjunctions of many kinds are not TODOs
We are aware that we are not handling these for now. Much like how `#structOne | #structTwo` generates `map[string]any`, disjunctions of different kinds like `string | int` generate `any`. We can do better in the future if we wish to, but there is no good way to represent sum types in Go today. Signed-off-by: Daniel Martí <[email protected]> Change-Id: I92685ff0287003e4a39989d92ae122cdbed36ca6 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1207250 Unity-Result: CUE porcuepine <[email protected]> TryBot-Result: CUEcueckoo <[email protected]> Reviewed-by: Roger Peppe <[email protected]>
1 parent 97b24b2 commit 4d852c8

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

cmd/cue/cmd/testdata/script/exp_gengotypes.txtar

+4-4
Original file line numberDiff line numberDiff line change
@@ -638,13 +638,13 @@ type Types struct {
638638

639639
Duration string/* CUE time.Duration */ `json:"Duration,omitempty"`
640640

641-
StringOrInt any/* TODO: IncompleteKind: (int|string) */ `json:"StringOrInt,omitempty"`
641+
StringOrInt any/* CUE disjunction: (int|string) */ `json:"StringOrInt,omitempty"`
642642

643-
StringOrIntDefault any/* TODO: IncompleteKind: (int|string) */ `json:"StringOrIntDefault,omitempty"`
643+
StringOrIntDefault any/* CUE disjunction: (int|string) */ `json:"StringOrIntDefault,omitempty"`
644644

645-
NullOrStruct any/* TODO: IncompleteKind: (null|struct) */ `json:"NullOrStruct,omitempty"`
645+
NullOrStruct any/* CUE disjunction: (null|struct) */ `json:"NullOrStruct,omitempty"`
646646

647-
NullOrString any/* TODO: IncompleteKind: (null|string) */ `json:"NullOrString,omitempty"`
647+
NullOrString any/* CUE disjunction: (null|string) */ `json:"NullOrString,omitempty"`
648648

649649
NumericBounds any/* CUE number; int64 or float64 */ `json:"NumericBounds,omitempty"`
650650

internal/encoding/gotypes/generate.go

+6
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,12 @@ func (g *generator) emitType(val cue.Value, optional bool) error {
316316
// TODO: uint64 would be marginally better than int64 for unsigned integer types
317317

318318
default:
319+
// A disjunction of various kinds cannot be represented in Go, as it does not have sum types.
320+
// Also see the potential approaches in the TODO about disjunctions of structs.
321+
if op, _ := val.Expr(); op == cue.OrOp {
322+
g.appendf("any /* CUE disjunction: %s */", k)
323+
break
324+
}
319325
g.appendf("any /* TODO: IncompleteKind: %s */", k)
320326
}
321327
return nil

0 commit comments

Comments
 (0)