Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add memberNames attr identifier to enhance kind="enum" #12

Closed
sdboyer opened this issue Aug 17, 2021 · 6 comments · Fixed by #17
Closed

Add memberNames attr identifier to enhance kind="enum" #12

sdboyer opened this issue Aug 17, 2021 · 6 comments · Fixed by #17
Assignees
Labels

Comments

@sdboyer
Copy link
Contributor

sdboyer commented Aug 17, 2021

By default, kind="enum" uses a CamelCasing-ish strategy on values to decide on member names to correspond to those values. This is a nice default, but it doesn't help when the values of the disjunct are numerics instead of strings, or if the user just plain wants some other pattern.

For that, we need cuetsy to support another attribute identifier. What i did here should work fine - a memberNames attr identifier, which expects a |-delimited list of strings, equal in count to the number of elements in the disjunction. memberNames will be required if the disjunction contains any numeric values.

CUE TS
package cuetsy

// Enum-level comment
// Foo: member-level comment
// Bar: member-level comment
AutoCamel: "foo" | "bar" @cuetsy(kind="enum")
// Enum-level comment
// Foo: member-level comment
// Bar: member-level comment
ManualCamel: "foo" | "bar" @cuetsy(kind="enum",memberNames="Foo|Bar")
Arbitrary: "foo" | "bar" @cuetsy(kind="enum",memberNames="Zip|Zap")
Numeric: 0 | 1 | 2 @cuetsy(kind="enum",memberNames="Zero|One|Two")
ErrMismatchLen: "a" | "b" | "c" @cuetsy(kind="enum",memberNames="a|b")
ErrNamelessNumerics: 0 | 1 | 2 @cuetsy(kind="enum")
/**
 * Enum-level comment
 **/
enum AutoCamel {
  // member-level comment
  Foo = "foo",
  // member-level comment
  Bar = "bar",
}
/**
 * Enum-level comment
 **/
enum ManualCamel {
  // member-level comment
  Foo = "foo",
  // member-level comment
  Bar = "bar",
}
enum Arbitrary {
  Zip = "foo",
  Zap = "bar",
}
enum Numeric {
  Zero = 0,
  One = 1,
  Two = 2,
}
@sdboyer sdboyer added kind/feature New feature or request component/generator labels Aug 17, 2021
@sdboyer
Copy link
Contributor Author

sdboyer commented Aug 17, 2021

@ying-jeanne this might be more straightforward to work on than trying to dynamically inject the cuetsy attributes

@sdboyer
Copy link
Contributor Author

sdboyer commented Aug 17, 2021

@ryantxu asked about comments over in #14. i've updated the OP accordingly.

@ryantxu
Copy link
Member

ryantxu commented Aug 17, 2021

// TS authors: "you can do this, but it's probably silly, maybe don't?"
Heterogeneous: 0 | 1 | "foo" @cuetsy(kind="enum",memberNames="Zero|One|Foo")

Lets not promote that one :)

The "number" case is an artifact of defining an enum directly -- it translates to numbers, but I think is something we should avoid generally. (and need good ways to map from numbers to strings in migrations)

@sdboyer
Copy link
Contributor Author

sdboyer commented Aug 17, 2021

Fair enough, removed the heterogeneous case :) It'll still be supported, necessarily, but no need to promote it.

Is the concern about numeric enums specific to what we want to define as best practices in Grafana, or does it extend to TS more widely?

@ying-jeanne
Copy link
Collaborator

ying-jeanne commented Aug 18, 2021

@sdboyer Make sense, I could take over this request, I don't have right to assign myself to the issue somehow :).

@sdboyer
Copy link
Contributor Author

sdboyer commented Aug 18, 2021

@ying-jeanne assigned! will add you to repo, too

@ying-jeanne ying-jeanne added this to To do in Grafana Backend (DO NOT USE!) via automation Aug 30, 2021
@ying-jeanne ying-jeanne added this to the 8.2.0 milestone Aug 30, 2021
@ying-jeanne ying-jeanne moved this from To do to Done in Grafana Backend (DO NOT USE!) Aug 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
No open projects
Development

Successfully merging a pull request may close this issue.

3 participants