wails icon indicating copy to clipboard operation
wails copied to clipboard

Generate bindings for structs without fields

Open Gjergj opened this issue 9 months ago • 1 comments

Is your feature request related to a problem? Please describe.

I have a weird case where i have a struct that that doesn't have any fields and it's not generated in typescript.

That struct is used as a type in another struct.

type TypeA struct {
}

type TypeB struct {
    Field *TypeA `json"field"`
}

When the typescript types are generated it outputs something similar to this (this was redacted)

export class TypeB {
        // Go type: TypeA
        field?: any;
    
        static createFrom(source: any = {}) {
            return new TypeB(source);
        }
    
        constructor(source: any = {}) {
            if ('string' === typeof source) source = JSON.parse(source);
            this.field = this.convertValues(source["field"], null);
        }
    
        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;
        }
    }

On the Go side If have an object such as

obj := TypeB  {
    Field: nil,
}

then all is fine.

If the Go side I have an object such as

obj := TypeB  {
    Field: &TypeA{}
}

I get an error on the FE here this.field = this.convertValues(source["field"], null); and here return new classs(a);

I'm assuming it's because passing null as class name to convertValues.

Unfortunately I can not add extra fields TypeA (I can explain more on this if needed)

Describe the solution you'd like

Wails to create a typescript type for TypeA even though without properties

Describe alternatives you've considered

None

Additional context

if it makes or doesn't have any negative impact I have a possible fix #3489

Gjergj avatar May 17 '24 09:05 Gjergj

Discord discussion https://discord.com/channels/1042734330029547630/1240662111676268607/1240662111676268607

Gjergj avatar May 17 '24 10:05 Gjergj