Skip to content

Commit af65985

Browse files
authored
Merge pull request #4209 from joshuapare/invalid-mapkey-array-arg-generation
fixing typescript generation of map of struct arrays
2 parents f8998da + 9a10be6 commit af65985

File tree

3 files changed

+19
-18
lines changed

3 files changed

+19
-18
lines changed

v2/internal/binding/binding_test/binding_deepelements_test.go

+12-12
Original file line numberDiff line numberDiff line change
@@ -63,18 +63,18 @@ export namespace binding_test {
6363
DoubleFour: number[][];
6464
Triple: number[][][];
6565
SingleMap: Record<string, number>;
66-
SliceMap: Record<string, number[]>;
67-
DoubleSliceMap: Record<string, number[][]>;
68-
ArrayMap: Record<string, number[]>;
69-
DoubleArrayMap1: Record<string, number[][]>;
70-
DoubleArrayMap2: Record<string, number[][]>;
71-
DoubleArrayMap3: Record<string, number[][]>;
66+
SliceMap: Record<string, Array<number>>;
67+
DoubleSliceMap: Record<string, Array<Array<number>>>;
68+
ArrayMap: Record<string, Array<number>>;
69+
DoubleArrayMap1: Record<string, Array<Array<number>>>;
70+
DoubleArrayMap2: Record<string, Array<Array<number>>>;
71+
DoubleArrayMap3: Record<string, Array<Array<number>>>;
7272
OneStructs: DeepMessage[];
7373
TwoStructs: DeepMessage[][];
7474
ThreeStructs: DeepMessage[][][];
75-
MapStructs: Record<string, DeepMessage[]>;
76-
MapTwoStructs: Record<string, DeepMessage[][]>;
77-
MapThreeStructs: Record<string, DeepMessage[][][]>;
75+
MapStructs: Record<string, Array<DeepMessage>>;
76+
MapTwoStructs: Record<string, Array<Array<DeepMessage>>>;
77+
MapThreeStructs: Record<string, Array<Array<Array<DeepMessage>>>>;
7878
7979
static createFrom(source: any = {}) {
8080
return new DeepElements(source);
@@ -97,9 +97,9 @@ export namespace binding_test {
9797
this.OneStructs = this.convertValues(source["OneStructs"], DeepMessage);
9898
this.TwoStructs = this.convertValues(source["TwoStructs"], DeepMessage);
9999
this.ThreeStructs = this.convertValues(source["ThreeStructs"], DeepMessage);
100-
this.MapStructs = this.convertValues(source["MapStructs"], DeepMessage[], true);
101-
this.MapTwoStructs = this.convertValues(source["MapTwoStructs"], DeepMessage[][], true);
102-
this.MapThreeStructs = this.convertValues(source["MapThreeStructs"], DeepMessage[][][], true);
100+
this.MapStructs = this.convertValues(source["MapStructs"], Array<DeepMessage>, true);
101+
this.MapTwoStructs = this.convertValues(source["MapTwoStructs"], Array<Array<DeepMessage>>, true);
102+
this.MapThreeStructs = this.convertValues(source["MapThreeStructs"], Array<Array<Array<DeepMessage>>>, true);
103103
}
104104
105105
convertValues(a: any, classs: any, asMap: boolean = false): any {

v2/internal/typescriptify/typescriptify.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@ const (
4040
jsVariableNameRegex = `^([A-Z]|[a-z]|\$|_)([A-Z]|[a-z]|[0-9]|\$|_)*$`
4141
)
4242

43-
var (
44-
jsVariableUnsafeChars = regexp.MustCompile(`[^A-Za-z0-9_]`)
45-
)
43+
var jsVariableUnsafeChars = regexp.MustCompile(`[^A-Za-z0-9_]`)
4644

4745
func nameTypeOf(typeOf reflect.Type) string {
4846
tname := typeOf.Name()
@@ -277,6 +275,7 @@ func (t *typeScriptClassBuilder) AddMapField(fieldName string, field reflect.Str
277275
valueType := field.Type.Elem()
278276
valueTypeName := nameTypeOf(valueType)
279277
valueTypeSuffix := ""
278+
valueTypePrefix := ""
280279
if valueType.Kind() == reflect.Ptr {
281280
valueType = valueType.Elem()
282281
valueTypeName = nameTypeOf(valueType)
@@ -289,7 +288,8 @@ func (t *typeScriptClassBuilder) AddMapField(fieldName string, field reflect.Str
289288
}
290289
valueType = valueType.Elem()
291290
valueTypeName = nameTypeOf(valueType)
292-
valueTypeSuffix = strings.Repeat("[]", arrayDepth)
291+
valueTypeSuffix = strings.Repeat(">", arrayDepth)
292+
valueTypePrefix = strings.Repeat("Array<", arrayDepth)
293293
}
294294
if valueType.Kind() == reflect.Ptr {
295295
valueType = valueType.Elem()
@@ -325,10 +325,10 @@ func (t *typeScriptClassBuilder) AddMapField(fieldName string, field reflect.Str
325325
fieldName = fmt.Sprintf(`"%s"?`, strippedFieldName)
326326
}
327327
}
328-
t.fields = append(t.fields, fmt.Sprintf("%s%s: Record<%s, %s>;", t.indent, fieldName, keyTypeStr, valueTypeName+valueTypeSuffix))
328+
t.fields = append(t.fields, fmt.Sprintf("%s%s: Record<%s, %s>;", t.indent, fieldName, keyTypeStr, valueTypePrefix+valueTypeName+valueTypeSuffix))
329329
if valueType.Kind() == reflect.Struct {
330330
t.constructorBody = append(t.constructorBody, fmt.Sprintf("%s%sthis%s = this.convertValues(source[\"%s\"], %s, true);",
331-
t.indent, t.indent, dotField, strippedFieldName, t.prefix+valueTypeName+valueTypeSuffix+t.suffix))
331+
t.indent, t.indent, dotField, strippedFieldName, t.prefix+valueTypePrefix+valueTypeName+valueTypeSuffix+t.suffix))
332332
} else {
333333
t.constructorBody = append(t.constructorBody, fmt.Sprintf("%s%sthis%s = source[\"%s\"];",
334334
t.indent, t.indent, dotField, strippedFieldName))

website/src/pages/changelog.mdx

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2323
- Added `-skipembedcreate` flag to build and dev command to improve compile and recompile speed [#4143](https://github.com/wailsapp/wails/pull/4143) by @josStorer
2424

2525
### Fixed
26+
- Fixed typescript generation of maps with key of array of structs by @joshuapare in [#4209](https://github.com/wailsapp/wails/pull/4209)
2627
- Fixed -m build flag for dev command not working when recompiling in [#4141](https://github.com/wailsapp/wails/pull/4141) by @josStorer
2728
- Fixed window restoration behavior after minimization by @superDingda in [#4109](https://github.com/wailsapp/wails/issues/4109)
2829
- Fixed excessive console logging after updating to v2.10.1 by @superDingda in [#4111](https://github.com/wailsapp/wails/issues/4111)

0 commit comments

Comments
 (0)