5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-03 05:50:08 +08:00

Support Maps in TS conversion (#1435)

This commit is contained in:
Lea Anthony 2022-07-10 20:54:49 +10:00 committed by GitHub
parent 55ec688331
commit d4662bd797
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 3 deletions

View File

@ -110,7 +110,7 @@ func (b *Bindings) GenerateGoBindings(baseDir string) error {
func goTypeToJSDocType(input string, importNamespaces *slicer.StringSlicer) string { func goTypeToJSDocType(input string, importNamespaces *slicer.StringSlicer) string {
switch true { switch true {
case input == "interface {}": case input == "interface {}" || input == "interface{}":
return "any" return "any"
case input == "string": case input == "string":
return "string" return "string"
@ -125,6 +125,10 @@ func goTypeToJSDocType(input string, importNamespaces *slicer.StringSlicer) stri
return "boolean" return "boolean"
case input == "[]byte": case input == "[]byte":
return "string" return "string"
case strings.HasPrefix(input, "map"):
temp := strings.TrimPrefix(input, "map[")
keyType, valueType, _ := strings.Cut(temp, "]")
return fmt.Sprintf("{[key: %s]: %s}", goTypeToJSDocType(keyType, importNamespaces), goTypeToJSDocType(valueType, importNamespaces))
case strings.HasPrefix(input, "[]"): case strings.HasPrefix(input, "[]"):
arrayType := goTypeToJSDocType(input[2:], importNamespaces) arrayType := goTypeToJSDocType(input[2:], importNamespaces)
return "Array<" + arrayType + ">" return "Array<" + arrayType + ">"

View File

@ -1,6 +1,7 @@
package binding package binding
import ( import (
"github.com/leaanthony/slicer"
"testing" "testing"
) )
@ -81,15 +82,26 @@ func Test_goTypeToJSDocType(t *testing.T) {
input: "foo", input: "foo",
want: "any", want: "any",
}, },
{
name: "map",
input: "map[string]float64",
want: "{[key: string]: number}",
},
{
name: "map",
input: "map[string]map[string]float64",
want: "{[key: string]: {[key: string]: number}}",
},
{ {
name: "types", name: "types",
input: "main.SomeType", input: "main.SomeType",
want: "models.SomeType", want: "main.SomeType",
}, },
} }
var importNamespaces slicer.StringSlicer
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
if got := goTypeToJSDocType(tt.input); got != tt.want { if got := goTypeToJSDocType(tt.input, &importNamespaces); got != tt.want {
t.Errorf("goTypeToJSDocType() = %v, want %v", got, tt.want) t.Errorf("goTypeToJSDocType() = %v, want %v", got, tt.want)
} }
}) })