From fe1f23b0fd52997b16529a7abb2cfdeb52141601 Mon Sep 17 00:00:00 2001 From: Jeremy Jay Date: Tue, 13 Aug 2024 17:49:08 -0400 Subject: [PATCH] If a field is exported, generate json even in the absence of tags (#3678) * if no JSON tag, check if field is exported * add no-tags binding test case * update changelog for #3678 --- v2/internal/binding/binding.go | 5 +- .../binding_test/binding_notags_test.go | 59 +++++++++++++++++++ .../binding/binding_test/binding_test.go | 1 + v2/internal/typescriptify/typescriptify.go | 8 ++- website/src/pages/changelog.mdx | 1 + 5 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 v2/internal/binding/binding_test/binding_notags_test.go diff --git a/v2/internal/binding/binding.go b/v2/internal/binding/binding.go index 568e11b03..b42718bff 100644 --- a/v2/internal/binding/binding.go +++ b/v2/internal/binding/binding.go @@ -350,7 +350,10 @@ func (b *Bindings) hasExportedJSONFields(typeOf reflect.Type) bool { for i := 0; i < typeOf.NumField(); i++ { jsonFieldName := "" f := typeOf.Field(i) - jsonTag := f.Tag.Get("json") + jsonTag, hasTag := f.Tag.Lookup("json") + if !hasTag && f.IsExported() { + return true + } if len(jsonTag) == 0 { continue } diff --git a/v2/internal/binding/binding_test/binding_notags_test.go b/v2/internal/binding/binding_test/binding_notags_test.go new file mode 100644 index 000000000..c59a86e1b --- /dev/null +++ b/v2/internal/binding/binding_test/binding_notags_test.go @@ -0,0 +1,59 @@ +package binding_test + +type NoFieldTags struct { + Name string + Address string + Zip *string + Spouse *NoFieldTags +} + +func (n NoFieldTags) Get() NoFieldTags { + return n +} + +var NoFieldTagsTest = BindingTest{ + name: "NoFieldTags", + structs: []interface{}{ + &NoFieldTags{}, + }, + exemptions: nil, + shouldError: false, + want: ` +export namespace binding_test { + export class NoFieldTags { + Name: string; + Address: string; + Zip?: string; + Spouse?: NoFieldTags; + static createFrom(source: any = {}) { + return new NoFieldTags(source); + } + constructor(source: any = {}) { + if ('string' === typeof source) source = JSON.parse(source); + this.Name = source["Name"]; + this.Address = source["Address"]; + this.Zip = source["Zip"]; + this.Spouse = this.convertValues(source["Spouse"], NoFieldTags); + } + + convertValues(a: any, classs: any, asMap: boolean = false): any { + if (!a) { + return a; + } + if (a.slice && a.map) { + return (a as any[]).map(elem => this.convertValues(elem, classs)); + } else if ("object" === typeof a) { + if (asMap) { + for (const key of Object.keys(a)) { + a[key] = new classs(a[key]); + } + return a; + } + return new classs(a); + } + return a; + } + } +} +`, +} diff --git a/v2/internal/binding/binding_test/binding_test.go b/v2/internal/binding/binding_test/binding_test.go index a16dde5ea..32ebbe056 100644 --- a/v2/internal/binding/binding_test/binding_test.go +++ b/v2/internal/binding/binding_test/binding_test.go @@ -50,6 +50,7 @@ func TestBindings_GenerateModels(t *testing.T) { EntityWithDiffNamespacesTest, SpecialCharacterFieldTest, WithoutFieldsTest, + NoFieldTagsTest, } testLogger := &logger.Logger{} diff --git a/v2/internal/typescriptify/typescriptify.go b/v2/internal/typescriptify/typescriptify.go index 85fea9c42..1d22a1b65 100644 --- a/v2/internal/typescriptify/typescriptify.go +++ b/v2/internal/typescriptify/typescriptify.go @@ -553,7 +553,13 @@ func (t *TypeScriptify) getFieldOptions(structType reflect.Type, field reflect.S func (t *TypeScriptify) getJSONFieldName(field reflect.StructField, isPtr bool) string { jsonFieldName := "" - jsonTag := field.Tag.Get("json") + jsonTag, hasTag := field.Tag.Lookup("json") + if !hasTag && field.IsExported() { + jsonFieldName = field.Name + if isPtr { + jsonFieldName += "?" + } + } if len(jsonTag) > 0 { jsonTagParts := strings.Split(jsonTag, ",") if len(jsonTagParts) > 0 { diff --git a/website/src/pages/changelog.mdx b/website/src/pages/changelog.mdx index 479c9491d..740c6c877 100644 --- a/website/src/pages/changelog.mdx +++ b/website/src/pages/changelog.mdx @@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Fixed MacOS build to use `outputfilename` from wails.json. [#3200](https://github.com/wailsapp/wails/issues/3200) - Fixed file drop events on windows. Fixed in [PR](https://github.com/wailsapp/wails/pull/3595) by @FrancescoLuzzi - Fixed doctor command not finding pkg-config on Solus. [PR #3670](https://github.com/wailsapp/wails/pull/3670) by [@ianmjones](https://github.com/ianmjones) +- Fixed binding for struct fields that were exported but had no json tags. [PR #3678](https://github.com/wailsapp/wails/pull/3678) ## v2.9.1 - 2024-06-18