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

Do not attempt to export fields that cannot be json-encoded #3975

Merged
merged 4 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions v2/internal/binding/binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,14 @@ func (b *Bindings) hasExportedJSONFields(typeOf reflect.Type) bool {
for i := 0; i < typeOf.NumField(); i++ {
jsonFieldName := ""
f := typeOf.Field(i)
// function, complex, and channel types cannot be json-encoded
if f.Type.Kind() == reflect.Chan ||
f.Type.Kind() == reflect.Func ||
f.Type.Kind() == reflect.UnsafePointer ||
f.Type.Kind() == reflect.Complex128 ||
f.Type.Kind() == reflect.Complex64 {
continue
}
jsonTag, hasTag := f.Tag.Lookup("json")
if !hasTag && f.IsExported() {
return true
Expand Down
1 change: 1 addition & 0 deletions v2/internal/binding/binding_test/binding_notags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ type NoFieldTags struct {
Address string
Zip *string
Spouse *NoFieldTags
NoFunc func() string
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before this PR, this line would cause the same problem as the issue in 3809. So if it gets reverted then this will trigger the bug again.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So not only should it not show up in the test output ~ line 28 of this file, but it would also cause a binding failure.

}

func (n NoFieldTags) Get() NoFieldTags {
Expand Down
8 changes: 8 additions & 0 deletions v2/internal/typescriptify/typescriptify.go
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,14 @@ func (t *TypeScriptify) getFieldOptions(structType reflect.Type, field reflect.S

func (t *TypeScriptify) getJSONFieldName(field reflect.StructField, isPtr bool) string {
jsonFieldName := ""
// function, complex, and channel types cannot be json-encoded
if field.Type.Kind() == reflect.Chan ||
field.Type.Kind() == reflect.Func ||
field.Type.Kind() == reflect.UnsafePointer ||
field.Type.Kind() == reflect.Complex128 ||
field.Type.Kind() == reflect.Complex64 {
return ""
}
jsonTag, hasTag := field.Tag.Lookup("json")
if !hasTag && field.IsExported() {
jsonFieldName = field.Name
Expand Down
2 changes: 1 addition & 1 deletion website/src/pages/changelog.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fixed incorrect TS definition of `WindowSetSize` by @leaanthony
- chore: fix some comments in [PR](https://github.com/wailsapp/wails/pull/3932) by @lvyaoting
- [windows] Fixed frameless window flickering when minimizing/restoring by preventing unnecessary redraws [#3951](https://github.com/wailsapp/wails/issues/3951)

- Fixed failed models.ts build due to non-json-encodable Go types [PR](https://github.com/wailsapp/wails/pull/3975) by [@pbnjay](https://github.com/pbnjay)

### Changed
- Allow to specify macos-min-version externally. Implemented by @APshenkin in [PR](https://github.com/wailsapp/wails/pull/3756)
Expand Down