Skip to content

Commit

Permalink
Allow enum unions
Browse files Browse the repository at this point in the history
  • Loading branch information
spinillos committed Mar 15, 2023
1 parent b26edcb commit 55745e2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
16 changes: 15 additions & 1 deletion generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -661,14 +661,28 @@ func (g *generator) genInterfaceField(v cue.Value) (*typeRef, error) {
func(v cue.Value) bool { return targetsKind(cue.Dereference(v), TypeEnum) },
) {
op, args := v.Expr()
if op != cue.OrOp {
if op == cue.AndOp {
return g.genEnumReference(v)
}

// Check if has defaults
for _, a := range args {
if a.IncompleteKind() == cue.TopKind {
return g.genEnumReference(v)
}
}

// Check if it is a union
isUnion := true
for _, a := range args {
if a.Kind() != a.IncompleteKind() {
isUnion = false
}
}

if isUnion {
return g.genEnumReference(v)
}
}

// One path for when there's a ref to a cuetsy node, and a separate one otherwise
Expand Down
12 changes: 8 additions & 4 deletions testdata/imports/compose_enums.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ Compose: {
localnumover: LocalEnumNumericD & (*2 | _)
impstr: dep.DepEnum & (*"bar" | _)
impstrd: dep.DepEnumD
impstrover: dep.DepEnumD & (*"bar" | _)
impstrover: dep.DepEnumD | *"bar"
impnum: dep.DepEnumNumeric & (*3 | _)
impnumd: dep.DepEnumNumericD
impnumover: dep.DepEnumNumericD & (*3 | _)
impnumover: dep.DepEnumNumericD | *3
union: (dep.DepEnumNumericD & 2) | (dep.DepEnumNumericD & 3)
unionStrings: (LocalEnum & "bar") | (LocalEnum & "foo") | (LocalEnum & "baz")
} @cuetsy(kind="interface")

-- dep/file.cue --
Expand Down Expand Up @@ -77,15 +79,17 @@ export interface Compose {
localstr: LocalEnum;
localstrd: LocalEnumD;
localstrover: LocalEnumD;
union: dep.DepEnumNumericD;
unionStrings: LocalEnum;
}

export const defaultCompose: Partial<Compose> = {
impnum: dep.DepEnumNumeric.Three,
impnumd: dep.DepEnumNumericD.One,
impnumover: dep.DepEnumNumericD.Three,
impnumover: 3,
impstr: dep.DepEnum.Bar,
impstrd: dep.DepEnumD.Foo,
impstrover: dep.DepEnumD.Bar,
impstrover: 'bar',
localnum: LocalEnumNumeric.Two,
localnumd: LocalEnumNumericD.One,
localnumover: LocalEnumNumericD.Two,
Expand Down

0 comments on commit 55745e2

Please sign in to comment.