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

Shouldn't we register exported field during binding ? #1721

Closed
ValentinTrinque opened this issue Aug 9, 2022 · 4 comments · Fixed by #1727
Closed

Shouldn't we register exported field during binding ? #1721

ValentinTrinque opened this issue Aug 9, 2022 · 4 comments · Fixed by #1727
Labels
Bug Something isn't working

Comments

@ValentinTrinque
Copy link
Contributor

Description

I encounter a problem while generating the binding for response object with nested structure.

type As struct {
  B Bs `json:"b"`
}

type Bs struct {
  Name string `json:"name"`
}

It appears the struct Bs is not added to the struct to generate for typescript, as we end up with an any type in the TS models.

I think the problem comes from these lines:

In this case, validating the condition field.PkgPath == "" means we found an exported field. So, we are skipping the exported fields, which, I think, is not what we want.

We could use !field.IsExported() instead. That would be less cryptic. It has been added recently, AFAIR.

To Reproduce

Not sure if this is accurate, but I think the example above could reproduce the bug.

type As struct {
  B Bs `json:"b"`
}

type Bs struct {
  Name string `json:"name"`
}

Expected behaviour

The binding process should register nested struct in the TS models, not an any.

Screenshots

Screenshot 2022-08-09 at 22 51 03

Attempted Fixes

The following change worked for me. But I don't have the full picture so, it might not be the complete solution.

- if field.PkgPath == "" {
+ if field.PkgPath != "" {
    continue
}

System Details

Wails CLI v2.0.0-beta.42

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

System
------
OS:		MacOS
Version: 	12.5
ID:		199506
Go Version:	go1.18.4
Platform:	darwin
Architecture:	arm64

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

Dependency			Package Name	Status		Version
----------			------------	------		-------
xcode command line tools 	N/A		Installed	2395
npm 				N/A		Installed	8.1.2
*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 : Available at https://nsis.sourceforge.io/Download


### Additional context

_No response_
@leaanthony
Copy link
Member

Thanks for raising this detailed insight into the issue. We should build some tests around this for sure as it's really hard to get right all the time. I'm going to get some basic tests working around this, especially the WriteModels method. Leave it with me a couple of days to work out how best to do that 👍

@leaanthony
Copy link
Member

leaanthony commented Aug 10, 2022

Ok, I've had a quick look and created a simple example for testing TS generation. This is on the feature/ts-generation-tests branch: https://github.com/wailsapp/wails/tree/feature/ts-generation-tests

The example test is here: https://github.com/wailsapp/wails/blob/feature/ts-generation-tests/v2/internal/binding/binding_singlefield_test.go

The main test runner uses it here: https://github.com/wailsapp/wails/blob/feature/ts-generation-tests/v2/internal/binding/binding_test.go

This should, at the very least, allow us to reproduce bugs with the smallest examples possible. It will also allow us to debug easily and find out what the issue is. Please let me know how you get on 👍

I'll also start adding more tests here over time.

EDIT: I added your example. I don't have time to debug it right now, but please take a look and confirm that is what you are seeing. Also feel free to debug it of course 😄

@leaanthony leaanthony added the Bug Something isn't working label Aug 10, 2022
@ValentinTrinque
Copy link
Contributor Author

I am about to push a PR with a test that highlight the problem, would you mind to look at it ?

@ValentinTrinque
Copy link
Contributor Author

Also my PR contains the fix.

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

Successfully merging a pull request may close this issue.

2 participants