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

Nested Structure generation without defaults #23

Merged
merged 1 commit into from
Sep 16, 2021
Merged

Nested Structure generation without defaults #23

merged 1 commit into from
Sep 16, 2021

Conversation

ying-jeanne
Copy link
Collaborator

@ying-jeanne ying-jeanne commented Aug 26, 2021

This PR is to generate the Nested Structure from cue to typescript. It doesn't fully support the default generation for the following case yet.

NestedFoo: {
  Bar: string | *"ohai"
  Foo: {
    Bar: string | *"ohai"
    Baz: AType
  }
} @cuetsy(kind="interface")

The expected result should be

export const nestedFooDefault: NestedFoo = {
  Bar: 'ohai',
  Foo: {
    Bar: 'ohai'
  }
}

instead of

export const nestedFooDefault: NestedFoo = {
  Bar: 'ohai',
}

@ying-jeanne ying-jeanne linked an issue Aug 27, 2021 that may be closed by this pull request
@ying-jeanne ying-jeanne marked this pull request as ready for review August 30, 2021 08:40
@ying-jeanne ying-jeanne changed the title Nested Structure [WIP] Nested Structure Sep 2, 2021
encoder/generator.go Outdated Show resolved Hide resolved
@ying-jeanne ying-jeanne changed the title [WIP] Nested Structure [Ready] Nested Structure Sep 15, 2021
Copy link
Contributor

@sdboyer sdboyer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Eh, let's just cut the generated comment thing. i think it creates more confusion than it resolves.

encoder/tests/interfaces Outdated Show resolved Hide resolved
encoder/tests/defaultsInterface Outdated Show resolved Hide resolved
@ying-jeanne ying-jeanne changed the title [Ready] Nested Structure Nested Structure generation without defaults Sep 15, 2021
Copy link
Contributor

@sdboyer sdboyer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just some nittish issues about the way the printing function handles nesting

// Render a string containing a Typescript semantic equivalent to the provided
// Value, if possible.
//
// The provided Value must be a simple expression (loosely defined, until
// something more precise is understood); e.g., this will NOT render a struct
// literal.
func tsprintField(v cue.Value) (string, error) {
func tsprintField(v cue.Value, optionals ...int) (string, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this need to be variadic? Unless i'm missing something, it seems like it should be sufficient to just make it a single int corresponding to the nesting level.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, just want to do something equivalent to default parameter to avoid updating everywhere :D, apparently in go there is no such thing... This is the equivalent version i found in go...
Will do something static.

encoder/generator.go Outdated Show resolved Hide resolved
@sdboyer
Copy link
Contributor

sdboyer commented Sep 16, 2021 via email

@sdboyer sdboyer merged commit a78a97b into main Sep 16, 2021
@sdboyer sdboyer deleted the 19 branch September 16, 2021 14:05
@ying-jeanne
Copy link
Collaborator Author

It's fine to just expect all regular calls to pass 0 as a second parameter. If it really bugs you, you can always make a separate "inner" function for the recursive call that has a level parameter, and the current tsprintField will just call that inner one, adding the 0 as a second parameter.

On Thu, Sep 16, 2021 at 9:55 AM ying-jeanne @.> wrote: @.* commented on this pull request. ------------------------------ In encoder/generator.go <#23 (comment)>: > // Render a string containing a Typescript semantic equivalent to the provided // Value, if possible. // // The provided Value must be a simple expression (loosely defined, until // something more precise is understood); e.g., this will NOT render a struct // literal. -func tsprintField(v cue.Value) (string, error) { +func tsprintField(v cue.Value, optionals ...int) (string, error) { yes, just want to do something equivalent to default parameter to avoid update everywhere :D, apparently in go there is no such thing. Will do something static. — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub <#23 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAAFIX7O3CW6TSIADO6IIX3UCHZOTANCNFSM5C3M57TA .

probably better as you said, i don't really like this version neither since it fixed the rest of input as int also...

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

Successfully merging this pull request may close these issues.

Make nested struct generation works
2 participants