From bc7b816c37bfb59cd541db57dde34c93e22dbbff Mon Sep 17 00:00:00 2001 From: joshuapare Date: Thu, 10 Apr 2025 08:35:49 -0500 Subject: [PATCH] postfix [] in instance is element accessor, not array type declaration --- .../binding_test/binding_deepelements_test.go | 24 +++++++++---------- v2/internal/typescriptify/typescriptify.go | 12 +++++----- website/src/pages/changelog.mdx | 1 + 3 files changed, 19 insertions(+), 18 deletions(-) diff --git a/v2/internal/binding/binding_test/binding_deepelements_test.go b/v2/internal/binding/binding_test/binding_deepelements_test.go index 488c58f6d..034687474 100644 --- a/v2/internal/binding/binding_test/binding_deepelements_test.go +++ b/v2/internal/binding/binding_test/binding_deepelements_test.go @@ -63,18 +63,18 @@ export namespace binding_test { DoubleFour: number[][]; Triple: number[][][]; SingleMap: Record; - SliceMap: Record; - DoubleSliceMap: Record; - ArrayMap: Record; - DoubleArrayMap1: Record; - DoubleArrayMap2: Record; - DoubleArrayMap3: Record; + SliceMap: Record>; + DoubleSliceMap: Record>>; + ArrayMap: Record>; + DoubleArrayMap1: Record>>; + DoubleArrayMap2: Record>>; + DoubleArrayMap3: Record>>; OneStructs: DeepMessage[]; TwoStructs: DeepMessage[][]; ThreeStructs: DeepMessage[][][]; - MapStructs: Record; - MapTwoStructs: Record; - MapThreeStructs: Record; + MapStructs: Record>; + MapTwoStructs: Record>>; + MapThreeStructs: Record>>>; static createFrom(source: any = {}) { return new DeepElements(source); @@ -97,9 +97,9 @@ export namespace binding_test { this.OneStructs = this.convertValues(source["OneStructs"], DeepMessage); this.TwoStructs = this.convertValues(source["TwoStructs"], DeepMessage); this.ThreeStructs = this.convertValues(source["ThreeStructs"], DeepMessage); - this.MapStructs = this.convertValues(source["MapStructs"], DeepMessage[], true); - this.MapTwoStructs = this.convertValues(source["MapTwoStructs"], DeepMessage[][], true); - this.MapThreeStructs = this.convertValues(source["MapThreeStructs"], DeepMessage[][][], true); + this.MapStructs = this.convertValues(source["MapStructs"], Array, true); + this.MapTwoStructs = this.convertValues(source["MapTwoStructs"], Array>, true); + this.MapThreeStructs = this.convertValues(source["MapThreeStructs"], Array>>, true); } convertValues(a: any, classs: any, asMap: boolean = false): any { diff --git a/v2/internal/typescriptify/typescriptify.go b/v2/internal/typescriptify/typescriptify.go index 95376b2f4..216fba820 100644 --- a/v2/internal/typescriptify/typescriptify.go +++ b/v2/internal/typescriptify/typescriptify.go @@ -40,9 +40,7 @@ const ( jsVariableNameRegex = `^([A-Z]|[a-z]|\$|_)([A-Z]|[a-z]|[0-9]|\$|_)*$` ) -var ( - jsVariableUnsafeChars = regexp.MustCompile(`[^A-Za-z0-9_]`) -) +var jsVariableUnsafeChars = regexp.MustCompile(`[^A-Za-z0-9_]`) func nameTypeOf(typeOf reflect.Type) string { tname := typeOf.Name() @@ -277,6 +275,7 @@ func (t *typeScriptClassBuilder) AddMapField(fieldName string, field reflect.Str valueType := field.Type.Elem() valueTypeName := nameTypeOf(valueType) valueTypeSuffix := "" + valueTypePrefix := "" if valueType.Kind() == reflect.Ptr { valueType = valueType.Elem() valueTypeName = nameTypeOf(valueType) @@ -289,7 +288,8 @@ func (t *typeScriptClassBuilder) AddMapField(fieldName string, field reflect.Str } valueType = valueType.Elem() valueTypeName = nameTypeOf(valueType) - valueTypeSuffix = strings.Repeat("[]", arrayDepth) + valueTypeSuffix = strings.Repeat(">", arrayDepth) + valueTypePrefix = strings.Repeat("Array<", arrayDepth) } if valueType.Kind() == reflect.Ptr { valueType = valueType.Elem() @@ -325,10 +325,10 @@ func (t *typeScriptClassBuilder) AddMapField(fieldName string, field reflect.Str fieldName = fmt.Sprintf(`"%s"?`, strippedFieldName) } } - t.fields = append(t.fields, fmt.Sprintf("%s%s: Record<%s, %s>;", t.indent, fieldName, keyTypeStr, valueTypeName+valueTypeSuffix)) + t.fields = append(t.fields, fmt.Sprintf("%s%s: Record<%s, %s>;", t.indent, fieldName, keyTypeStr, valueTypePrefix+valueTypeName+valueTypeSuffix)) if valueType.Kind() == reflect.Struct { t.constructorBody = append(t.constructorBody, fmt.Sprintf("%s%sthis%s = this.convertValues(source[\"%s\"], %s, true);", - t.indent, t.indent, dotField, strippedFieldName, t.prefix+valueTypeName+valueTypeSuffix+t.suffix)) + t.indent, t.indent, dotField, strippedFieldName, t.prefix+valueTypePrefix+valueTypeName+valueTypeSuffix+t.suffix)) } else { t.constructorBody = append(t.constructorBody, fmt.Sprintf("%s%sthis%s = source[\"%s\"];", t.indent, t.indent, dotField, strippedFieldName)) diff --git a/website/src/pages/changelog.mdx b/website/src/pages/changelog.mdx index 68288cfff..fc1a5ff6b 100644 --- a/website/src/pages/changelog.mdx +++ b/website/src/pages/changelog.mdx @@ -23,6 +23,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Added `-skipembedcreate` flag to build and dev command to improve compile and recompile speed [#4143](https://github.com/wailsapp/wails/pull/4143) by @josStorer ### Fixed +- Fixed typescript generation of maps with key of array of structs by @joshuapare in [#4209](https://github.com/wailsapp/wails/pull/4209) - Fixed -m build flag for dev command not working when recompiling in [#4141](https://github.com/wailsapp/wails/pull/4141) by @josStorer - Fixed window restoration behavior after minimization by @superDingda in [#4109](https://github.com/wailsapp/wails/issues/4109) - Fixed excessive console logging after updating to v2.10.1 by @superDingda in [#4111](https://github.com/wailsapp/wails/issues/4111)