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

feat: support embed struct fields without tstype: ",extends" #35

Merged
merged 3 commits into from
Aug 25, 2023

Conversation

mtt0
Copy link
Contributor

@mtt0 mtt0 commented Aug 18, 2023

Backgroud

For use case of we must use embed struct field's (anonymous field's), but the json/yaml/... tag is not inline.

If we got

type Deployment struct {
    v1.TypeMeta `json:",inline"  tstype:",extends"`
    v1.ObjectMeta `json:"metadata"`
}

the serialized string be like

{
  "apiVersion": "v1",
  "kind": "Deployment",
  "metadata": {
    "name": "test"
  }
}

In this case, I need metadata json object, and v1.ObjectMeta's methods which Deployment type needs to compose of.

Currently tygo will ignore anonymous field. And tstype:",extends"'s behavior dosen't meet my needs.
Golang uses composition instead of inheritance, inline and tystype:",extends" match exactly in meaning and behavior, while anonymous fields behave differently when serializing and deserializing.

This pr is trying to support embed struct fields (anonymous field) in such case.

Example

package embed

import bookapp "github.com/gzuidhof/tygo/examples/bookstore"

// TokenType Built-in type alias
type TokenType string

// Reference struct type
type Reference struct {
	Foo string `json:"foo"`
}

type Base struct {
	ID string `json:"id"`
}

type StructEmbed struct {
	Base           `json:",inline" tstype:",extends"` // embed struct with `tstype:"extends"`
	TokenType      `json:"tokenType"`                 // built-in type field without `tstype:"extends"`
	Reference      `json:"reference"`                 // embed struct without `tstype:"extends"`
	OtherReference Reference                          `json:"other_reference"`
	Bar            string                             `json:"bar"`
	bookapp.Book   `json:"book"`                      // embed external struct without `tstype:"extends"`
	Chapter        bookapp.Chapter                    `json:"chapter"`
}

With tygo.yaml:

packages:
  - path: "github.com/gzuidhof/tygo/examples/embed"
    fallback_type: unknown
    type_mappings:
      bookapp.Book: "bookapp.Book"
      bookapp.Chapter: "bookapp.Chapter"
    frontmatter:
      | # We can define some additional text to put at the start of the file.
      import * as bookapp from "../bookstore"

Result:

// Code generated by tygo. DO NOT EDIT.
import * as bookapp from "../bookstore"

//////////
// source: embed.go

/**
 * TokenType Built-in type alias
 */
export type TokenType = string;
/**
 * Reference struct type
 */
export interface Reference {
  foo: string;
}
export interface Base {
  id: string;
}
export interface StructEmbed extends Base {
  tokenType: TokenType; // built-in type field without `tstype:"extends"`
  reference: Reference; // embed struct without `tstype:"extends"`
  other_reference: Reference;
  bar: string;
  book: bookapp.Book; // embed external struct without `tstype:"extends"`
  chapter: bookapp.Chapter;
}

@mtt0 mtt0 force-pushed the main branch 3 times, most recently from 64bd941 to dd27a84 Compare August 21, 2023 06:20
@mtt0 mtt0 marked this pull request as draft August 22, 2023 10:22
@mtt0 mtt0 marked this pull request as draft August 22, 2023 10:22
types defined in the Go file after the parsed file in the same
package will be ignored, fix this
@mtt0 mtt0 marked this pull request as ready for review August 22, 2023 12:33
@gzuidhof
Copy link
Owner

@0x-buidl would you perhaps be able to review this?

Of course if you're not available, let me know and I'll happily dive into it :)

@0x-buidl
Copy link
Contributor

Yeah, i'll make a review

@0x-buidl
Copy link
Contributor

@gzuidhof this PR is good too go.

@gzuidhof
Copy link
Owner

Thank you for the contribution @mtt0, and thank you @0x-buidl for the review :)

@gzuidhof gzuidhof merged commit 09625a8 into gzuidhof:main Aug 25, 2023
1 check passed
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.

None yet

3 participants