wails: A lot of different bugs while generating bindings

Description

The binding generator does not properly handle maps, type aliases and other packages types:

In my codebase I have a file where I save all the types used in the cross comunications so my binded methods often uses external packages types. This are some of the types inside intypes.go

type Library struct {
	Comics  ComicsMap `json:"Comics"`
	Version string `json:"Version"`
}
type ComicsMap map[string]Comic
type Comic struct{
	Title string `json:"Title"`
	Slug string `json:"Slug"`
}

And this is the method I want to bind inside app.go

func (a *App) GetLibrary() map[string]intypes.Comics {
	\\ my code
}

If I try to generate the file I get a non existent and wrong type:

export function GetLibrary():Promise<map[string]intypes.Comic>;

To Reproduce

  1. Define a struct in a separated package
  2. Bind a method that returns a map which the value is a type of another package
  3. Generate bindings with either wails dev or wails generate module

Expected behaviour

Generate the bound methods with the proper return type

export function GetLibrary():Promise<{[key: string]: intypes.Comic}>;

Screenshots

No response

Attempted Fixes

While trying to solve this problem I discovered even more problems.

If I try to return the type alias ComicsMap Like this:

func (a *App) GetLibrary() intypes.ComicsMap {
	\\ my code
}

It generate it likes this:

export function GetLibrary():Promise<intypes.ComicsMap>;

Which is correct but it break the import like this:

import {map[string]intypes} from '../models';

and the generated models.ts doesn’t have the ComicsMap type inside the intypes namespace.

After this I then tried to create a similar type inside the app.go file (so it simply was map[string]Comic) and did everything as expected confirming that the problem was the different package. This of course did not solve the problem so I tried returning the Library struct above directly and it worked as expected.

So basically no type aliases, no external packages maps. Bound methods can’t return an external package struct if not inside a given struct

System Details

# System

OS           | Windows 10 Pro
Version      | 2009 (Build: 19044)
ID           | 21H2
Go Version   | go1.19.4
Platform     | windows
Architecture | amd64

# Wails

Version | v2.3.1

# Dependencies

Dependency | Package Name | Status    | Version
WebView2   | N/A          | Installed | 108.0.1462.76
npm        | N/A          | Installed | 8.15.0
*upx       | N/A          | Available |
*nsis      | N/A          | Available |
* - Optional Dependency

# Diagnosis

Your system is ready for Wails development!
Optional package(s) installation details:
  - upx : Available at https://upx.github.io/
  - nsis : More info at https://wails.io/docs/guides/windows-installer/

Additional context

Aside from the bug I would also advise the use of js Maps:

export function GetLibrary():Promise<Map<string, intypes.Comic>>;

and the creation of types aliases inside the models.ts (if not generating it was intended).

I tried giving a fast look at “internal/binding/generate.go” but I would need more time to figure it out

About this issue

  • Original URL
  • State: open
  • Created a year ago
  • Comments: 19 (10 by maintainers)

Most upvoted comments

Also, I saw tygo mentioned in #2323. Are there plans to switch to that and start using ast (or related) instead of reflect? If so, has that work been started somewhere?

Yeah, the work has started! The parser is in v3/internal/parser, there is an example I’m using for testing in v3/examples/binding. If you install the v3 cli, then go to v3/examples/binding then run wails generate bindings, you can see the current progress. It’s very basic and very incomplete (static analysis is hard!) but yes, I’m looking to somehow integrate tygo if possible, but I’ve been too busy getting a multi-window runtime done to look any further in it 😅 Could totally do with some help on that! You can ping me on discord for more info/help 👍

https://discord.com/channels/1042734330029547630/1071249333929922640