Skip to content

Commit

Permalink
switch syntax for typescript record outputs
Browse files Browse the repository at this point in the history
prior syntax is primarily useful for naming keys
so not useful here, and this syntax avoids square
brackets in output which greatly simplifies
generation for Go generics
  • Loading branch information
pbnjay committed Jan 4, 2025
1 parent 2813db4 commit bf6867f
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export namespace binding_test {
export namespace binding_test_import {
export class AMapWrapper {
AMap: {[key: string]: binding_test_nestedimport.A};
AMap: Record<string, binding_test_nestedimport.A>;
static createFrom(source: any = {}) {
return new AMapWrapper(source);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var NonStringMapKeyTest = BindingTest{
want: `
export namespace binding_test {
export class NonStringMapKey {
numberMap: {[key: number]: any};
numberMap: Record<number, any>;
static createFrom(source: any = {}) {
return new NonStringMapKey(source);
}
Expand Down
4 changes: 2 additions & 2 deletions v2/internal/binding/binding_test/binding_type_alias_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ const expectedTypeAliasBindings = `// Cynhyrchwyd y ffeil hon yn awtomatig. PEID
import {binding_test} from '../models';
import {int_package} from '../models';
export function Map():Promise<{[key: string]: string}>;
export function Map():Promise<Record<string, string>>;
export function MapAlias():Promise<binding_test.MapAlias>;
export function MapWithImportedStructValue():Promise<{[key: string]: int_package.SomeStruct}>;
export function MapWithImportedStructValue():Promise<Record<string, int_package.SomeStruct>>;
export function Slice():Promise<Array<string>>;
Expand Down
26 changes: 13 additions & 13 deletions v2/internal/binding/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,18 @@ func fullyQualifiedName(packageName string, typeName string) string {
}
}

var (
jsVariableUnsafeChars = regexp.MustCompile(`[^A-Za-z0-9_]`)
)

func arrayifyValue(valueArray string, valueType string) string {
valueType = strings.ReplaceAll(valueType, "*", "")
gidx := strings.IndexRune(valueType, '[')
if gidx > 0 { // its a generic type
rem := strings.SplitN(valueType, "[", 2)
valueType = rem[0] + "_" + jsVariableUnsafeChars.ReplaceAllLiteralString(rem[1], "_")
}

if len(valueArray) == 0 {
return valueType
}
Expand Down Expand Up @@ -217,25 +228,14 @@ func goTypeToJSDocType(input string, importNamespaces *slicer.StringSlicer) stri
}

if len(key) > 0 {
return fmt.Sprintf("{[key: %s]: %s}", key, arrayifyValue(valueArray, value))
return fmt.Sprintf("Record<%s, %s>", key, arrayifyValue(valueArray, value))
}

return arrayifyValue(valueArray, value)
}

var (
jsVariableUnsafeChars = regexp.MustCompile(`[^A-Za-z0-9_]`)
)

func goTypeToTypescriptType(input string, importNamespaces *slicer.StringSlicer) string {
tname := goTypeToJSDocType(input, importNamespaces)
tname = strings.ReplaceAll(tname, "*", "")
gidx := strings.IndexRune(tname, '[')
if gidx > 0 { // its a generic type
rem := strings.SplitN(tname, "[", 2)
tname = rem[0] + "_" + jsVariableUnsafeChars.ReplaceAllLiteralString(rem[1], "_")
}
return tname
return goTypeToJSDocType(input, importNamespaces)
}

func entityFullReturnType(input, prefix, suffix string, importNamespaces *slicer.StringSlicer) string {
Expand Down
4 changes: 2 additions & 2 deletions v2/internal/binding/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,12 @@ func Test_goTypeToJSDocType(t *testing.T) {
{
name: "map",
input: "map[string]float64",
want: "{[key: string]: number}",
want: "Record<string, number>",
},
{
name: "map",
input: "map[string]map[string]float64",
want: "{[key: string]: {[key: string]: number}}",
want: "Record<string, Record<string, number>>",
},
{
name: "types",
Expand Down
2 changes: 1 addition & 1 deletion v2/internal/typescriptify/typescriptify.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ func (t *typeScriptClassBuilder) AddMapField(fieldName string, field reflect.Str
fieldName = fmt.Sprintf(`"%s"?`, strippedFieldName)
}
}
t.fields = append(t.fields, fmt.Sprintf("%s%s: {[key: %s]: %s};", t.indent, fieldName, keyTypeStr, valueTypeName))
t.fields = append(t.fields, fmt.Sprintf("%s%s: Record<%s, %s>;", t.indent, fieldName, keyTypeStr, valueTypeName))
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+t.suffix))
Expand Down

0 comments on commit bf6867f

Please sign in to comment.