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

Generate "default objects" #6

Closed
sdboyer opened this issue Mar 9, 2021 · 2 comments
Closed

Generate "default objects" #6

sdboyer opened this issue Mar 9, 2021 · 2 comments

Comments

@sdboyer
Copy link
Contributor

sdboyer commented Mar 9, 2021

Problem

Currently, we disregard CUE default values. This needs to change, though there's some flexibility here on exactly how we do it.

Proposed Solution

@ryantxu can say more about exactly how he thinks this ought to be shaped, but...

We should optionally generate:

  • A const with default values assigned for the type
  • A default-setter func:<typename>setDefaults(t <typename>)
  • An *isDefault() func that checks whether an instance of the type equal has the default values.

Simple example:

Foo: {
  Bar: string | *"ohai*
  Baz: int | *4
} @cuetsy(targetType="interface")
export interface Foo {
  Bar: string;
  Baz: int;
}

export const FooDefault {
  Bar: 'ohai';
  Baz: 4
}

export function FooIsDefault(obj: Foo): bool {
  ...
}

export function FooSetDefaults(obj: Foo): Foo {
  ...
}

i feel like there's probably a more elegant generic way of expressing the funcs, maybe even the default, perhaps leveraging some Partials. 🤷

This will be tricky. For one, i think we'll have to read through CUE references in a way we don't now. e.g.:

AType: "foo" | "bar" | *"baz" @cuetsy(targetType="type")
BType: {
  Composed: AType
} @cuetsy(targetType="interface")

For BType, the exported const would need to traverse the reference to AType to figure out the right default value for the exported const's Composed field.

Also, related, we can't assume that even required fields will have a default. I believe that means that the exported const isn't actually guaranteed to satisfy the exported interface. (That may just be more of an oddity than problematic, though).

In general, though, we don't need this to cover all possible CUE constructions perfectly, as an operating assumption in cuetsy, at least for now, is that folks are going to be checking the generated TS on the other side, and they can choose to rewrite their CUE if it doesn't come out right. (Yup, we iz prototyping!)

@ryantxu
Copy link
Member

ryantxu commented Mar 10, 2021

I don't see any need for cuetsy to worry about the functions:

export function FooIsDefault(obj: Foo): bool {
export function FooSetDefaults(obj: Foo): Foo {

This can be solved using pretty standard strategies like:

The important part for cuetsy is to generate the const object:

export const fooDefault {
  Bar: 'ohai';
  Baz: 4
}

@sdboyer
Copy link
Contributor Author

sdboyer commented Aug 23, 2021

Fixed by #7

@sdboyer sdboyer closed this as completed Aug 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants