grafana / cuetsy

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

Enums within struct/object defaults use literal value as opposed to enum member #74

Closed codeincarnate closed 1 year ago

codeincarnate commented 1 year ago

As a part of grafana/grafana#59363 ran into an issue in which using an enum member within a default field will emit the literal value for that enum as opposed to accessing the member.

For the following cue description:

cellOptions: TableCellOptions | *{ type: TableCellDisplayMode & "auto" }

this is emitted:

cellOptions: {
  type: 'auto',
}

However, it's expected that this would be emitted instead:

cellOptions: {
  type: TableCellDisplayMode.Auto,
}
sdboyer commented 1 year ago

interesting, thanks for the issue!

First, let's make sure we're on the same page on what you're trying to express with the input CUE. I interpret the intention behind this code:

cellOptions: TableCellOptions | *{ type: TableCellDisplayMode & "auto" }

// added these from original source for reference
TableCellDisplayMode: "auto" | "color-text" | "color-background" @cuetsy(kind="enum",memberNames="Auto|ColorText|ColorBackground")

as:

I want the cellOptions field of some larger schema to be an instance of TableCellOptions.

TableCellOptions is a discriminated union with a tag/discriminator field named type, which we want to express in TS as an enum.

When receiving data for a schema containing this cellOptions field, if the data lacks a cellOptions field, then cellOptions should be a TableCellOptions of type auto, represented using the enum, and having no other field values.

Is that accurate?

codeincarnate commented 1 year ago

Hey Sam, yes that's accurate. The main issue with the above is that the first output causes a type error as it's not explicitly a TableCellDisplayMode member (even though the string is a member of the enum).

spinillos commented 1 year ago

It was fixed in the PR where we fixed the way to set the enums and it was useful to find another issues.