Skip to content

Commit 7566ed4

Browse files
pbnjayleaanthony
andauthored
Do not attempt to export fields that cannot be json-encoded (#3975)
* Do not attempt to export fields that cannot be json-encoded * update changelog w/ PR * also skip UnsafePointers --------- Co-authored-by: Lea Anthony <[email protected]>
1 parent 90be707 commit 7566ed4

File tree

4 files changed

+18
-1
lines changed

4 files changed

+18
-1
lines changed

v2/internal/binding/binding.go

+8
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,14 @@ func (b *Bindings) hasExportedJSONFields(typeOf reflect.Type) bool {
350350
for i := 0; i < typeOf.NumField(); i++ {
351351
jsonFieldName := ""
352352
f := typeOf.Field(i)
353+
// function, complex, and channel types cannot be json-encoded
354+
if f.Type.Kind() == reflect.Chan ||
355+
f.Type.Kind() == reflect.Func ||
356+
f.Type.Kind() == reflect.UnsafePointer ||
357+
f.Type.Kind() == reflect.Complex128 ||
358+
f.Type.Kind() == reflect.Complex64 {
359+
continue
360+
}
353361
jsonTag, hasTag := f.Tag.Lookup("json")
354362
if !hasTag && f.IsExported() {
355363
return true

v2/internal/binding/binding_test/binding_notags_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ type NoFieldTags struct {
55
Address string
66
Zip *string
77
Spouse *NoFieldTags
8+
NoFunc func() string
89
}
910

1011
func (n NoFieldTags) Get() NoFieldTags {

v2/internal/typescriptify/typescriptify.go

+8
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,14 @@ func (t *TypeScriptify) getFieldOptions(structType reflect.Type, field reflect.S
553553

554554
func (t *TypeScriptify) getJSONFieldName(field reflect.StructField, isPtr bool) string {
555555
jsonFieldName := ""
556+
// function, complex, and channel types cannot be json-encoded
557+
if field.Type.Kind() == reflect.Chan ||
558+
field.Type.Kind() == reflect.Func ||
559+
field.Type.Kind() == reflect.UnsafePointer ||
560+
field.Type.Kind() == reflect.Complex128 ||
561+
field.Type.Kind() == reflect.Complex64 {
562+
return ""
563+
}
556564
jsonTag, hasTag := field.Tag.Lookup("json")
557565
if !hasTag && field.IsExported() {
558566
jsonFieldName = field.Name

website/src/pages/changelog.mdx

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2828
- Fixed incorrect TS definition of `WindowSetSize` by @leaanthony
2929
- chore: fix some comments in [PR](https://github.com/wailsapp/wails/pull/3932) by @lvyaoting
3030
- [windows] Fixed frameless window flickering when minimizing/restoring by preventing unnecessary redraws [#3951](https://github.com/wailsapp/wails/issues/3951)
31-
31+
- 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)
3232

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

0 commit comments

Comments
 (0)