Skip to content

Commit

Permalink
Do not attempt to export fields that cannot be json-encoded (#3975)
Browse files Browse the repository at this point in the history
* Do not attempt to export fields that cannot be json-encoded

* update changelog w/ PR

* also skip UnsafePointers

---------

Co-authored-by: Lea Anthony <lea.anthony@gmail.com>
  • Loading branch information
pbnjay and leaanthony authored Jan 10, 2025
1 parent 90be707 commit 7566ed4
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 1 deletion.
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
}

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

0 comments on commit 7566ed4

Please sign in to comment.