Skip to content

Commit

Permalink
correct enum cases
Browse files Browse the repository at this point in the history
  • Loading branch information
ying-jeanne committed Sep 2, 2021
1 parent 918b679 commit 0754b5e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 17 deletions.
29 changes: 27 additions & 2 deletions encoder/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,16 +247,41 @@ func (g *generator) genEnum(name string, v cue.Value) {
}

func validateAndGetDefault(v cue.Value) (cue.Value, bool, error) {
defs := []cue.Value{}
ops, vvals := v.Expr()
fmt.Printf("........ the v expr() is: %v and ops is : %v\n", vvals, ops)
if ops == cue.OrOp {
for _, vval := range vvals {
if inst, path := vval.Reference(); len(path) > 0 {
fmt.Println(">>>>>>>>>>>>>>>>> I am here >>>>>>>>>>>>>>")
if def, ok := inst.Lookup(path...).Default(); ok {

defs = append(defs, def)
}
}
}
}

def, ok := v.Default()
if ok {
label, _ := def.Label()
defs = append(defs, def)
label, _ := v.Label()
op, dvals := def.Expr()
if len(dvals) > 1 && op == cue.OrOp {
return def, true, valError(v, "%s has multiple defaults which is not allowed", label)
}
return def, true, nil
}
if len(defs) >= 1 {
for _, def := range defs[1:] {
if !defs[0].Equals(def) {
label, _ := v.Label()
return def, true, valError(v, "%s has multiple defaults which is not allowed", label)
}
}
return defs[0], true, nil
}
return def, false, nil

}

func getDefaultEnumValue(v cue.Value) (string, error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Seems CUE is picking the default before eval it, it seems we can get the referen
check whether default exist there, if yes if the default also exist for the current cue object error out

Or should we just forbidden the disjunction between Enum and other types??
Now Enum | Enum is not export at all, should we support Enum | Enum?
-- cue --
package cuetsy

Expand All @@ -15,15 +16,4 @@ Foo: {
} @cuetsy(kind="interface")

-- err --
export enum AOrEnum {
Bar = 'bar',
Baz = 'baz',
Foo = 'foo',
}
export const aOrEnumDefault: AOrEnum = AOrEnum.Baz
export interface Foo {
Bar: AOrEnum | 'ohai';
}
export const fooDefault: Foo = {
Bar: 'ohai',
}
Bar has multiple defaults which is not allowed
13 changes: 10 additions & 3 deletions encoder/tests/conflictInterfaceDeferror.txtar
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
-- cue --
AType: *4 | int @cuetsy(kind="type")
Foo: {
Bar: string | *"ohai"
Baz: int | *4 | *7
Baz: AType | *7
} @cuetsy(kind="interface")

-- err --
Baz has multiple defaults which is not allowed
export type AType = number;
export const aTypeDefault: AType = 4
export interface Foo {
Baz: number;
}
export const fooDefault: Foo = {
Baz: 7,
}

0 comments on commit 0754b5e

Please sign in to comment.