Skip to content

Commit 1b77345

Browse files
committed
Missing prefix in map struct types
1 parent 2cf93a4 commit 1b77345

File tree

2 files changed

+18
-16
lines changed

2 files changed

+18
-16
lines changed

typescriptify/typescriptify.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,10 +260,11 @@ func (t *typeScriptClassBuilder) AddMapField(fieldName string, field reflect.Str
260260
// keyTypeStr = t.prefix + keyType.Name() + t.suffix
261261
// }
262262

263-
t.fields = append(t.fields, fmt.Sprintf("%s%s: {[key: %s]: %s};", t.indent, fieldName, keyTypeStr, valueTypeName))
264263
if valueType.Kind() == reflect.Struct {
264+
t.fields = append(t.fields, fmt.Sprintf("%s%s: {[key: %s]: %s};", t.indent, fieldName, keyTypeStr, t.prefix+valueTypeName))
265265
t.constructorBody = append(t.constructorBody, fmt.Sprintf("%s%sthis.%s = this.convertValues(source[\"%s\"], %s, true);", t.indent, t.indent, strippedFieldName, strippedFieldName, t.prefix+valueTypeName+t.suffix))
266266
} else {
267+
t.fields = append(t.fields, fmt.Sprintf("%s%s: {[key: %s]: %s};", t.indent, fieldName, keyTypeStr, valueTypeName))
267268
t.constructorBody = append(t.constructorBody, fmt.Sprintf("%s%sthis.%s = source[\"%s\"];", t.indent, t.indent, strippedFieldName, strippedFieldName))
268269
}
269270
}

typescriptify/typescriptify_test.go

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -781,10 +781,11 @@ func TestMaps(t *testing.T) {
781781
converter := New().
782782
AddType(reflect.TypeOf(WithMap{})).
783783
WithConstructor(true).
784+
WithPrefix("API_").
784785
WithBackupDir("")
785786

786787
desiredResult := `
787-
export class Address {
788+
export class API_Address {
788789
duration: number;
789790
text?: string;
790791
@@ -794,16 +795,16 @@ func TestMaps(t *testing.T) {
794795
this.text = source["text"];
795796
}
796797
}
797-
export class WithMap {
798+
export class API_WithMap {
798799
simpleMap: {[key: string]: number};
799-
mapObjects: {[key: string]: Address};
800-
ptrMapObjects?: {[key: string]: Address};
800+
mapObjects: {[key: string]: API_Address};
801+
ptrMapObjects?: {[key: string]: API_Address};
801802
802803
constructor(source: any = {}) {
803804
if ('string' === typeof source) source = JSON.parse(source);
804805
this.simpleMap = source["simpleMap"];
805-
this.mapObjects = this.convertValues(source["mapObjects"], Address, true);
806-
this.ptrMapObjects = this.convertValues(source["ptrMapObjects"], Address, true);
806+
this.mapObjects = this.convertValues(source["mapObjects"], API_Address, true);
807+
this.ptrMapObjects = this.convertValues(source["ptrMapObjects"], API_Address, true);
807808
}
808809
809810
` + tsConvertValuesFunc + `
@@ -817,15 +818,15 @@ func TestMaps(t *testing.T) {
817818
}
818819

819820
testConverter(t, converter, true, desiredResult, []string{
820-
`new WithMap(` + jsonizeOrPanic(json) + `).simpleMap.aaa == 1`,
821-
`(new WithMap(` + jsonizeOrPanic(json) + `).mapObjects.bbb) instanceof Address`,
822-
`!((new WithMap(` + jsonizeOrPanic(json) + `).mapObjects.bbb) instanceof WithMap)`,
823-
`new WithMap(` + jsonizeOrPanic(json) + `).mapObjects.bbb.duration == 1`,
824-
`new WithMap(` + jsonizeOrPanic(json) + `).mapObjects.bbb.text === "txt1"`,
825-
`(new WithMap(` + jsonizeOrPanic(json) + `)?.ptrMapObjects?.ccc) instanceof Address`,
826-
`!((new WithMap(` + jsonizeOrPanic(json) + `)?.ptrMapObjects?.ccc) instanceof WithMap)`,
827-
`new WithMap(` + jsonizeOrPanic(json) + `)?.ptrMapObjects?.ccc?.duration === 2`,
828-
`new WithMap(` + jsonizeOrPanic(json) + `)?.ptrMapObjects?.ccc?.text === "txt2"`,
821+
`new API_WithMap(` + jsonizeOrPanic(json) + `).simpleMap.aaa == 1`,
822+
`(new API_WithMap(` + jsonizeOrPanic(json) + `).mapObjects.bbb) instanceof API_Address`,
823+
`!((new API_WithMap(` + jsonizeOrPanic(json) + `).mapObjects.bbb) instanceof API_WithMap)`,
824+
`new API_WithMap(` + jsonizeOrPanic(json) + `).mapObjects.bbb.duration == 1`,
825+
`new API_WithMap(` + jsonizeOrPanic(json) + `).mapObjects.bbb.text === "txt1"`,
826+
`(new API_WithMap(` + jsonizeOrPanic(json) + `)?.ptrMapObjects?.ccc) instanceof API_Address`,
827+
`!((new API_WithMap(` + jsonizeOrPanic(json) + `)?.ptrMapObjects?.ccc) instanceof API_WithMap)`,
828+
`new API_WithMap(` + jsonizeOrPanic(json) + `)?.ptrMapObjects?.ccc?.duration === 2`,
829+
`new API_WithMap(` + jsonizeOrPanic(json) + `)?.ptrMapObjects?.ccc?.text === "txt2"`,
829830
})
830831
}
831832

0 commit comments

Comments
 (0)