tkrajina / typescriptify-golang-structs

A Golang struct to TypeScript class/interface converter
Apache License 2.0
505 stars 87 forks source link

Embedded structs with JSON tags are completely ignored #32

Closed shackra closed 3 years ago

shackra commented 3 years ago

In Go I have the following struct

type Token struct {
    *jwt.Payload // https://godoc.org/github.com/gbrlsnchs/jwt#Payload
    User         string              `json:"user"`
    Subcripcion  string              `json:"subcripcion"`
    Rules        map[string][]string `json:"rules" ts_type:"Record<string, Array<string>>"`
}

When you marshal that struct into JSON, the fields on *jwt.Payload get marshalled too if they have any data at all, however, typescriptify ignores it due to lack of the JSON tag, if you add the tag, the fields are included as part of another type, like so:

export interface Payload {
    iss?: string;
    sub?: string;
    aud?: string[];
    exp?: Time;
    nbf?: Time;
    iat?: Time;
    jti?: string;
}
export interface Token {
    payload?: Payload;
    user: string;
    subcripcion: string;
    rules: Record<string, Array<string>>;
}

I believe we should imitate how the fields end after a marshalling and output this result instead:

export interface Token {
    iss?: string;
    sub?: string;
    aud?: string[];
    exp?: Time;
    nbf?: Time;
    iat?: Time;
    jti?: string;
    user: string;
    subcripcion: string;
    rules: Record<string, Array<string>>;
}
tkrajina commented 3 years ago

Yes, I agree. If you want to experiment with this, it is handled here https://github.com/tkrajina/typescriptify-golang-structs/blob/master/typescriptify/typescriptify.go#L87 it handles normal anonymous structs, but your one is is a pointer to a struct.

tkrajina commented 3 years ago

Fixed in dev.