Skip to content

[Proposal] Include only struct fields that contains specified tags and allow to exclude empty structs #72

@fkhadra

Description

@fkhadra

Hey, currently the only way to exclude some structs from being generated is to provide a list of files to exclude. However, this is not always convenient, often structs live close to the code where they are used.

I'd like to propose an alternative approach. The idea would be to allow the user to generate typescript types only for fields that matches certain struct tags.

In practice, it would looks like this:

packages:
    - path: "pathToMyPackage"
    output_path: "src/types/index.ts"
    optional_type: "default"
    # only includes struct fields with a json tag
    match_tags:
      - "json"
  • Input
package pkg

type User struct {
	Username         string `json:"username"`
	Password          string
}
  • output
export interface User {
  username: string;
}

For my use case, I'd like to exposes only struct with json tags, I'm currently using a fork of your library for my project https://github.com/fkhadra/tygo. My implementation support any specific struct tag ofc, it's not limited to json.

I also added a feature to prevent empty structs from being generated.

packages:
    - path: "pathToMyPackage"
    output_path: "src/types/index.ts"
    optional_type: "default"
    exclude_empty_structs: true
    match_tags:
      - "json"

If you think that those features are worth adding to the library, I would be happy to open a pull request.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions