tkrajina / typescriptify-golang-structs

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

Embedded struct with the same name produces same class #54

Open loeffel-io opened 2 years ago

loeffel-io commented 2 years ago

User

type User struct {
    makeless_go_model.User

    UserRoleId *iota.UserRole `gorm:"not null,index" json:"userRoleId"`
    UserRole   *UserRole      `json:"userRole"`
}
/* Do not change, this code is generated from Golang structs */

export class UserRole {
    id?: number;
    name?: string;

    static createFrom(source: any = {}) {
        return new UserRole(source);
    }

    constructor(source: any = {}) {
        if ('string' === typeof source) source = JSON.parse(source);
        this.id = source["id"];
        this.name = source["name"];
    }
}
export class Token {
    id: number;
    createdAt: Time;
    updatedAt: Time;
    deletedAt?: DeletedAt;
    token?: string;
    note?: string;
    userId?: number;
    user?: User;
    teamId?: number;
    team?: Team;

    static createFrom(source: any = {}) {
        return new Token(source);
    }

    constructor(source: any = {}) {
        if ('string' === typeof source) source = JSON.parse(source);
        this.id = source["id"];
        this.createdAt = this.convertValues(source["createdAt"], Time);
        this.updatedAt = this.convertValues(source["updatedAt"], Time);
        this.deletedAt = this.convertValues(source["deletedAt"], DeletedAt);
        this.token = source["token"];
        this.note = source["note"];
        this.userId = source["userId"];
        this.user = this.convertValues(source["user"], User);
        this.teamId = source["teamId"];
        this.team = this.convertValues(source["team"], Team);
    }

    convertValues(a: any, classs: any, asMap: boolean = false): any {
        if (!a) {
            return a;
        }
        if (a.slice) {
            return (a as any[]).map(elem => this.convertValues(elem, classs));
        } else if ("object" === typeof a) {
            if (asMap) {
                for (const key of Object.keys(a)) {
                    a[key] = new classs(a[key]);
                }
                return a;
            }
            return new classs(a);
        }
        return a;
    }
}
export class TeamInvitation {
    id: number;
    createdAt: Time;
    updatedAt: Time;
    deletedAt?: DeletedAt;
    teamId?: number;
    team?: Team;
    teamUserId?: number;
    teamUser?: TeamUser;
    email?: string;
    expire?: Time;
    accepted?: boolean;

    static createFrom(source: any = {}) {
        return new TeamInvitation(source);
    }

    constructor(source: any = {}) {
        if ('string' === typeof source) source = JSON.parse(source);
        this.id = source["id"];
        this.createdAt = this.convertValues(source["createdAt"], Time);
        this.updatedAt = this.convertValues(source["updatedAt"], Time);
        this.deletedAt = this.convertValues(source["deletedAt"], DeletedAt);
        this.teamId = source["teamId"];
        this.team = this.convertValues(source["team"], Team);
        this.teamUserId = source["teamUserId"];
        this.teamUser = this.convertValues(source["teamUser"], TeamUser);
        this.email = source["email"];
        this.expire = this.convertValues(source["expire"], Time);
        this.accepted = source["accepted"];
    }

    convertValues(a: any, classs: any, asMap: boolean = false): any {
        if (!a) {
            return a;
        }
        if (a.slice) {
            return (a as any[]).map(elem => this.convertValues(elem, classs));
        } else if ("object" === typeof a) {
            if (asMap) {
                for (const key of Object.keys(a)) {
                    a[key] = new classs(a[key]);
                }
                return a;
            }
            return new classs(a);
        }
        return a;
    }
}
export class Team {
    id: number;
    createdAt: Time;
    updatedAt: Time;
    deletedAt?: DeletedAt;
    name?: string;
    userId?: number;
    user?: User;
    teamUsers: TeamUser[];
    teamInvitations: TeamInvitation[];
    tokens: Token[];

    static createFrom(source: any = {}) {
        return new Team(source);
    }

    constructor(source: any = {}) {
        if ('string' === typeof source) source = JSON.parse(source);
        this.id = source["id"];
        this.createdAt = this.convertValues(source["createdAt"], Time);
        this.updatedAt = this.convertValues(source["updatedAt"], Time);
        this.deletedAt = this.convertValues(source["deletedAt"], DeletedAt);
        this.name = source["name"];
        this.userId = source["userId"];
        this.user = this.convertValues(source["user"], User);
        this.teamUsers = this.convertValues(source["teamUsers"], TeamUser);
        this.teamInvitations = this.convertValues(source["teamInvitations"], TeamInvitation);
        this.tokens = this.convertValues(source["tokens"], Token);
    }

    convertValues(a: any, classs: any, asMap: boolean = false): any {
        if (!a) {
            return a;
        }
        if (a.slice) {
            return (a as any[]).map(elem => this.convertValues(elem, classs));
        } else if ("object" === typeof a) {
            if (asMap) {
                for (const key of Object.keys(a)) {
                    a[key] = new classs(a[key]);
                }
                return a;
            }
            return new classs(a);
        }
        return a;
    }
}
export class TeamUser {
    id: number;
    createdAt: Time;
    updatedAt: Time;
    deletedAt?: DeletedAt;
    teamId?: number;
    team?: Team;
    userId?: number;
    user?: User;
    role?: string;
    teamInvitations: TeamInvitation[];

    static createFrom(source: any = {}) {
        return new TeamUser(source);
    }

    constructor(source: any = {}) {
        if ('string' === typeof source) source = JSON.parse(source);
        this.id = source["id"];
        this.createdAt = this.convertValues(source["createdAt"], Time);
        this.updatedAt = this.convertValues(source["updatedAt"], Time);
        this.deletedAt = this.convertValues(source["deletedAt"], DeletedAt);
        this.teamId = source["teamId"];
        this.team = this.convertValues(source["team"], Team);
        this.userId = source["userId"];
        this.user = this.convertValues(source["user"], User);
        this.role = source["role"];
        this.teamInvitations = this.convertValues(source["teamInvitations"], TeamInvitation);
    }

    convertValues(a: any, classs: any, asMap: boolean = false): any {
        if (!a) {
            return a;
        }
        if (a.slice) {
            return (a as any[]).map(elem => this.convertValues(elem, classs));
        } else if ("object" === typeof a) {
            if (asMap) {
                for (const key of Object.keys(a)) {
                    a[key] = new classs(a[key]);
                }
                return a;
            }
            return new classs(a);
        }
        return a;
    }
}
export class User {
    id: number;
    createdAt: Time;
    updatedAt: Time;
    deletedAt?: DeletedAt;
    name?: string;
    email?: string;
    emailVerification?: EmailVerification;
    teamUsers: TeamUser[];
    tokens: Token[];

    static createFrom(source: any = {}) {
        return new User(source);
    }

    constructor(source: any = {}) {
        if ('string' === typeof source) source = JSON.parse(source);
        this.id = source["id"];
        this.createdAt = this.convertValues(source["createdAt"], Time);
        this.updatedAt = this.convertValues(source["updatedAt"], Time);
        this.deletedAt = this.convertValues(source["deletedAt"], DeletedAt);
        this.name = source["name"];
        this.email = source["email"];
        this.emailVerification = this.convertValues(source["emailVerification"], EmailVerification);
        this.teamUsers = this.convertValues(source["teamUsers"], TeamUser);
        this.tokens = this.convertValues(source["tokens"], Token);
    }

    convertValues(a: any, classs: any, asMap: boolean = false): any {
        if (!a) {
            return a;
        }
        if (a.slice) {
            return (a as any[]).map(elem => this.convertValues(elem, classs));
        } else if ("object" === typeof a) {
            if (asMap) {
                for (const key of Object.keys(a)) {
                    a[key] = new classs(a[key]);
                }
                return a;
            }
            return new classs(a);
        }
        return a;
    }
}
export class EmailVerification {
    id: number;
    createdAt: Time;
    updatedAt: Time;
    deletedAt?: DeletedAt;
    verified?: boolean;
    userId?: number;
    user?: User;

    static createFrom(source: any = {}) {
        return new EmailVerification(source);
    }

    constructor(source: any = {}) {
        if ('string' === typeof source) source = JSON.parse(source);
        this.id = source["id"];
        this.createdAt = this.convertValues(source["createdAt"], Time);
        this.updatedAt = this.convertValues(source["updatedAt"], Time);
        this.deletedAt = this.convertValues(source["deletedAt"], DeletedAt);
        this.verified = source["verified"];
        this.userId = source["userId"];
        this.user = this.convertValues(source["user"], User);
    }

    convertValues(a: any, classs: any, asMap: boolean = false): any {
        if (!a) {
            return a;
        }
        if (a.slice) {
            return (a as any[]).map(elem => this.convertValues(elem, classs));
        } else if ("object" === typeof a) {
            if (asMap) {
                for (const key of Object.keys(a)) {
                    a[key] = new classs(a[key]);
                }
                return a;
            }
            return new classs(a);
        }
        return a;
    }
}
export class DeletedAt {

    static createFrom(source: any = {}) {
        return new DeletedAt(source);
    }

    constructor(source: any = {}) {
        if ('string' === typeof source) source = JSON.parse(source);

    }
}
export class Time {

    static createFrom(source: any = {}) {
        return new Time(source);
    }

    constructor(source: any = {}) {
        if ('string' === typeof source) source = JSON.parse(source);

    }
}
export class User {
    id: number;
    createdAt: Time;
    updatedAt: Time;
    deletedAt?: DeletedAt;
    name?: string;
    email?: string;
    emailVerification?: EmailVerification;
    teamUsers: TeamUser[];
    tokens: Token[];
    userRoleId?: number;
    userRole?: UserRole;

    static createFrom(source: any = {}) {
        return new User(source);
    }

    constructor(source: any = {}) {
        if ('string' === typeof source) source = JSON.parse(source);
        this.id = source["id"];
        this.createdAt = this.convertValues(source["createdAt"], Time);
        this.updatedAt = this.convertValues(source["updatedAt"], Time);
        this.deletedAt = this.convertValues(source["deletedAt"], DeletedAt);
        this.name = source["name"];
        this.email = source["email"];
        this.emailVerification = this.convertValues(source["emailVerification"], EmailVerification);
        this.teamUsers = this.convertValues(source["teamUsers"], TeamUser);
        this.tokens = this.convertValues(source["tokens"], Token);
        this.userRoleId = source["userRoleId"];
        this.userRole = this.convertValues(source["userRole"], UserRole);
    }

    convertValues(a: any, classs: any, asMap: boolean = false): any {
        if (!a) {
            return a;
        }
        if (a.slice) {
            return (a as any[]).map(elem => this.convertValues(elem, classs));
        } else if ("object" === typeof a) {
            if (asMap) {
                for (const key of Object.keys(a)) {
                    a[key] = new classs(a[key]);
                }
                return a;
            }
            return new classs(a);
        }
        return a;
    }
}
loeffel-io commented 2 years ago

Hey, any update on that one? ✌️

tkrajina commented 2 years ago

Yes, I see the problem.

The best solution would be to add a way to rename types. Probably something like:

converter.RenameType(iota.UserRole{}, "UserRole2")

But I don't know when I'll find the time to implement it. Pull requests are welcome ;)

loeffel-io commented 2 years ago

Would love to see this feature :-)