Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issues with generating App.d.ts and model.ts files for structs that use generic types. #3371

Open
StevenAlexanderJohnson opened this issue Apr 4, 2024 · 0 comments
Labels
Bug Something isn't working

Comments

@StevenAlexanderJohnson
Copy link

Description

When attempting to return a struct that has a generic type, the App.d.ts file is incorrectly generated and contains typos.
It is very close, but it doesn't finish out the line.

It also generates an invalid models.ts file.

I've seen similar issues with #2323 and #2303, but neither seemed to have identical problems here.

To Reproduce

I created a "dbConnection.go" file in the main package and created the following structs.

type Database interface {
	Initialize() error
	Disconnect() error
}

type UserPermissionResult struct {
	Name           string  `json:"Name"`
	PermissionName string  `json:"PermissionName"`
	ObjectName     *string `json:"ObjectName"`
}

type DataResult interface {
	UserPermissionResult
}

type QueryResult[T DataResult] struct {
	Duration time.Duration `json:"Duration"`
	Data     []T           `json:"Data"`
}

I then attempt to return QueryResult from a function.

func (a *App) GetUserPermissions(databaseKey string, user string, target string) (QueryResult[UserPermissionResult], error) {}

The resulting "App.d.ts" file looks like this.

import {main} from '../models';

export function GetUserPermissions(arg1:string,arg2:string,arg3:string):Promise<main.QueryResult[main>;

It is very close but as you can see there is a typo in the Promise return type. To manually fix it, you just have to change it to the following manually

import {main} from '../models';

export function GetUserPermissions(arg1:string,arg2:string,arg3:string):Promise<main.QueryResult[main.UserPermissionResult]>;

As for "models.ts" the resulting file looks like this.

export namespace main {
	export class UserPermissionResult {
	    Name: string;
	    PermissionName: string;
	    ObjectName?: string;
	
	    static createFrom(source: any = {}) {
	        return new UserPermissionResult(source);
	    }
	
	    constructor(source: any = {}) {
	        if ('string' === typeof source) source = JSON.parse(source);
	        this.Name = source["Name"];
	        this.PermissionName = source["PermissionName"];
	        this.ObjectName = source["ObjectName"];
	    }
	}
	export class QueryResult[main.UserPermissionResult] {
	    Duration: number;
	    Data: UserPermissionResult[];
	
	    static createFrom(source: any = {}) {
	        return new QueryResult[main.UserPermissionResult](source);
	    }
	
	    constructor(source: any = {}) {
	        if ('string' === typeof source) source = JSON.parse(source);
	        this.Duration = source["Duration"];
	        this.Data = this.convertValues(source["Data"], UserPermissionResult);
	    }
	
		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;
		}
	}
}

This is a bit of a mixed bag, and I don't know how to fix it.
You'll see that first it's defining the class incorrectly.
It needs <> instead of [] for the generics. It's also using a concrete type for the class.
If we change export class QueryResult[main.UserPermissionResult] to export class QueryResult<T> then there are issues down the line with convertValues. You can't pass T as a type for classs.

I'm not sure how this would be fixed as I'm no TS expert.

Expected behaviour

The models.ts file should generate a proper class that uses generic types.
App.d.ts should finish the type declaration in the promise.

Screenshots

image
image

Attempted Fixes

For the time being, I'm going to change the return type to string and serialize the result then deserialize it on the JS side.
I've tried manually changing the generated file but was unable to find a fix due to the convertValues function unable to take a generic type.

System Details

Wails Doctor          
                                

                                                                                                                                                                                                                                                                     
# Wails
Version         | v2.8.0
Package Manager | apt   

# System
┌──────────────────────────────────────────────────────────┐
| OS           | Ubuntu                                    |
| Version      | 22.04                                     |
| ID           | ubuntu                                    |
| Go Version   | go1.22.0                                  |
| Platform     | linux                                     |
| Architecture | amd64                                     |
| CPU 1        | Intel(R) Xeon(R) Gold 6226R CPU @ 2.90GHz |
| CPU 2        | Intel(R) Xeon(R) Gold 6226R CPU @ 2.90GHz |
| GPU          | SVGA II Adapter (VMware) - Driver: vmwgfx |
| Memory       | 8GB                                       |
└──────────────────────────────────────────────────────────┘

# Dependencies
┌──────────────────────────────────────────────────────────────────────────┐
| Dependency | Package Name          | Status    | Version                 |
| *docker    | docker.io             | Installed | 26.0.0                  |
| gcc        | build-essential       | Installed | 12.9ubuntu3             |
| libgtk-3   | libgtk-3-dev          | Installed | 3.24.33-1ubuntu2        |
| libwebkit  | libwebkit2gtk-4.0-dev | Installed | 2.42.5-0ubuntu0.22.04.2 |
| npm        | npm                   | Installed | 10.2.4                  |
| *nsis      | nsis                  | Available | 3.08-2                  |
| pkg-config | pkg-config            | Installed | 0.29.2-1ubuntu3         |
└──────────────────────── * - Optional Dependency ─────────────────────────┘

# Diagnosis
Optional package(s) installation details: 
  - nsis: sudo apt install nsis

Additional context

No response

@StevenAlexanderJohnson StevenAlexanderJohnson added the Bug Something isn't working label Apr 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant