wails icon indicating copy to clipboard operation
wails copied to clipboard

`models.ts` is not generated.

Open mondy opened this issue 2 years ago • 8 comments

Description

When I run wails dev, I expect frontend/wailsjs/go/models.ts to be generated, but it is not. I use wails version v2.0.0-beta.35.

To Reproduce

  1. Execute wails init -t svelte-ts -d foo -n bar.
  2. Execute cd foo.
  3. Add the following to app.go.
type Foo struct{}

func (a *App) Foo() *Foo {
	return &Foo{}
}
  1. Execute wails dev.
  2. I look for frontend/wailsjs/go/models.ts but can't find it.

Expected behaviour

frontend/wailsjs/go/models.ts is generated.

Screenshots

No response

Attempted Fixes

In wails version v2.0.0-beta.34 frontend/wailsjs/go/models.ts is generated.

System Details

Wails CLI v2.0.0-beta.35


Scanning system - Please wait (this may take a long time)...Done.

System
------
OS:             Windows 10 Home
Version:        2009 (Build: 19044)
ID:             21H2
Go Version:     go1.18.1
Platform:       windows
Architecture:   amd64

Wails
------
Version:        v2.0.0-beta.35

Dependency      Package Name    Status          Version
----------      ------------    ------          -------
WebView2        N/A             Installed       100.0.1185.50
npm             N/A             Installed       8.5.5
*upx            N/A             Installed       upx 3.96
*nsis           N/A             Available

* - Optional Dependency

Diagnosis
---------
Your system is ready for Wails development!
Optional package(s) installation details:
  - nsis : Available at https://nsis.sourceforge.io/Download

Additional context

No response

mondy avatar Apr 30 '22 11:04 mondy

Hi, does your struct have json tags? Wails will only create the models.ts if your go struct has associated json:"" tags and if it exported like:

type MyStruct struct {
    ID string `json:"id"`
}

AlexBitsTech avatar Apr 30 '22 12:04 AlexBitsTech

I overlooked that a json tag is required. My apologies.

mondy avatar Apr 30 '22 12:04 mondy

@mondy is there something we could do in the docs to make this clearer?

leaanthony avatar Apr 30 '22 21:04 leaanthony

@leaanthony I think changes in the beta version are common. So I don't think it is necessary to mention them in the documentation. Instead, how about a template where models.ts is generated by default? You can use a simplified version of the main.go code listed here.

mondy avatar May 01 '22 00:05 mondy

I forgot to mention an issue that should have been mentioned. The following code App.d.ts is generated by following the steps shown in the introduction.

// Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL
// This file is automatically generated. DO NOT EDIT
import {main} from '../models';

export function Foo():Promise<main.Foo>;

export function Greet(arg1:string):Promise<string>;

../models is imported, but it does not exist. It is incomplete as source code. There is a issue with this condition. I would like you to generate an empty Foo class as follows.

export namespace main {
	
	export class Foo {
	    id: string;
	
	    static createFrom(source: any = {}) {
	        return new Foo(source);
	    }
	
	    constructor(source: any = {}) {
	        if ('string' === typeof source) source = JSON.parse(source);
	    }
	}

}

Or , if it generates incomplete code, it should output a warning to the CLI when it runs wails dev at minimum.

mondy avatar May 01 '22 00:05 mondy

I'm wondering if it's more desirable to leave it so it doesn't compile, as that will tell you that you've got something wrong in your code. Or...we could generate a blank model with a comment that warns about this and has a link to the docs.... that actually sounds reasonable to me.

leaanthony avatar May 01 '22 01:05 leaanthony

@leaanthony On version v2.0.0-beta.44.2 we cant generate models ((((( You know how fix?

Osbornnnnn avatar Aug 29 '22 19:08 Osbornnnnn

@Osbornnnnn please open a bug with details

leaanthony avatar Aug 29 '22 20:08 leaanthony

I forgot to mention an issue that should have been mentioned. The following code App.d.ts is generated by following the steps shown in the introduction.

// Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL
// This file is automatically generated. DO NOT EDIT
import {main} from '../models';

export function Foo():Promise<main.Foo>;

export function Greet(arg1:string):Promise<string>;

../models is imported, but it does not exist. It is incomplete as source code. There is a issue with this condition. I would like you to generate an empty Foo class as follows.

export namespace main {
	
	export class Foo {
	    id: string;
	
	    static createFrom(source: any = {}) {
	        return new Foo(source);
	    }
	
	    constructor(source: any = {}) {
	        if ('string' === typeof source) source = JSON.parse(source);
	    }
	}

}

Or , if it generates incomplete code, it should output a warning to the CLI when it runs wails dev at minimum.

~So @leaanthony and @mondy - is this expected behaviour? I was following the getting started guide and ran into the same issue where App.d.ts was showing an import for models but models.ts was nowhere to be found. This was on v2.0.0-rc.1.1.~

Sorry for the noise, it was a snafu on my end. model.ts is indeed generated.

simar7 avatar Sep 16 '22 05:09 simar7

@mondy is there something we could do in the docs to make this clearer?

@leaanthony Go has a default transformation of field names between JSON and Go, would it make sense to not require the json:"..." tag at all?

endophage avatar Oct 07 '22 02:10 endophage

I'd prefer this approach for sure but remember it did not working correctly for some reason back in the day. If you are happy to look at it, please open a ticket and we can stick it on the board 👍

leaanthony avatar Oct 07 '22 19:10 leaanthony

I only become aware of Wails recently thanks to the Golang Weekly newsletter. Got a little personal project it's perfect for and I've started on. Once I have a bit more familiarity, and if somebody hasn't started on it already, I'll happily take that on.

endophage avatar Oct 08 '22 15:10 endophage

Any news on this @endophage ? Might have a go this weekend

leaanthony avatar Oct 15 '22 03:10 leaanthony

Bumping this to v2.2

leaanthony avatar Oct 16 '22 09:10 leaanthony

The static analysis approach of v3 will enable us to generate models for unbound structs. Bumping this to v3 as there's been no appetite for this.

leaanthony avatar Aug 03 '23 22:08 leaanthony

I cannot understand what is the reason to have to type json:"name" against every field in my structs. This is useless. They represent exactly what supposted to be sent/received, however some of them might require extra customization like custom type declaration. They are simply api specification. My modules (*models) are pure DTOs and they have no any extra properties which must be ignored.

@leaanthony I'd prefer this approach for sure but remember it did not working correctly for some reason back in the day.

Would be nice to get rid of required tags.

fairking avatar Sep 16 '23 09:09 fairking

Sure thing. Feel free to open a PR to do this 👍

leaanthony avatar Sep 16 '23 09:09 leaanthony