tkrajina/typescriptify-golang-structs

Embedded structs with JSON tags are completely ignored

shackra opened this issue · 2 comments

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>>;
}

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.

Fixed in dev.