Skip to content

Nested Struct Won't Convert #56

@wolveix

Description

@wolveix

Hey! Firstly, thank you so much for this project, it is incredibly helpful! :)

When a struct has an implicitly declared struct within it, Typescriptify doesn't know how to handle it and ends up returning nothing.

The struct in question:

type DashboardMetrics struct {
	BusinessDailyTotal struct {
		Monday    float64 `json:"monday" yaml:"monday"`
		Tuesday   float64 `json:"tuesday" yaml:"tuesday"`
		Wednesday float64 `json:"wednesday" yaml:"wednesday"`
		Thursday  float64 `json:"thursday" yaml:"thursday"`
		Friday    float64 `json:"friday" yaml:"friday"`
	} `json:"business_daily_total" yaml:"business_daily_total"`
	BusinessWeeklyTotal float64 `json:"business_weekly_total" yaml:"business_weekly_total"`
}

The generated Typescript:

export class DashboardMetrics {
    business_daily_total: ;
    business_weekly_total: number;

    constructor(source: any = {}) {
        if ('string' === typeof source) source = JSON.parse(source);
        this.business_daily_total = this.convertValues(source["business_daily_total"], );
        this.business_weekly_total = source["business_weekly_total"];
    }

	convertValues(a: any, classs: any, asMap: boolean = false): any {
	    if (!a) {
	        return a;
	    }
	    if (a instanceof Array) {
	        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;
	}
}

Declaring the struct as its own struct first would of course circumvent this, but it'd be great to see some sort of fix for this :) Apologies if this has already been posted as an issue, I couldn't see anything obvious when I did a quick skim!

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