grafana / cuetsy

Experimental CUE->TypeScript exporter
Apache License 2.0
106 stars 8 forks source link

Fix enum reference #72

Closed spinillos closed 1 year ago

spinillos commented 1 year ago

When we reference an enum value, cuetsy was setting only the enum class missing the value.

For example:

Enum: "value1" | "value2" @cuetsy(kind="enum")

MyClass: {
  value: Enum.Value1
} @cuetsy(kind="interface")

was generating:

export enum Enum {
  Value1 = "value1",
  Value2 = "value2",
}

export interface MyClass {
  value: Enum;
}

and now it generates:

export interface MyClass {
  value: Enum.Value1;
}
sdboyer commented 1 year ago

We explored doing this with the original approach to cuetsy - basically, having enums in CUE be structs - as that seemed most obviously comparable to the way that enums feel to folks writing TypeScript.

But it doesn't work out. The enum field names that we choose to generate in cuetsy do not exist in CUE; CUE (correctly) interprets Enum.Value1 as a reference, and that reference does not exist. Nor can it, because Enum is string-kinded, not struct-kinded.