mirror of
https://github.com/wailsapp/wails.git
synced 2025-05-02 18:10:48 +08:00
Support Maps in TS conversion (#1435)
This commit is contained in:
parent
55ec688331
commit
d4662bd797
@ -110,7 +110,7 @@ func (b *Bindings) GenerateGoBindings(baseDir string) error {
|
||||
|
||||
func goTypeToJSDocType(input string, importNamespaces *slicer.StringSlicer) string {
|
||||
switch true {
|
||||
case input == "interface {}":
|
||||
case input == "interface {}" || input == "interface{}":
|
||||
return "any"
|
||||
case input == "string":
|
||||
return "string"
|
||||
@ -125,6 +125,10 @@ func goTypeToJSDocType(input string, importNamespaces *slicer.StringSlicer) stri
|
||||
return "boolean"
|
||||
case input == "[]byte":
|
||||
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, "[]"):
|
||||
arrayType := goTypeToJSDocType(input[2:], importNamespaces)
|
||||
return "Array<" + arrayType + ">"
|
||||
|
@ -1,6 +1,7 @@
|
||||
package binding
|
||||
|
||||
import (
|
||||
"github.com/leaanthony/slicer"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@ -81,15 +82,26 @@ func Test_goTypeToJSDocType(t *testing.T) {
|
||||
input: "foo",
|
||||
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",
|
||||
input: "main.SomeType",
|
||||
want: "models.SomeType",
|
||||
want: "main.SomeType",
|
||||
},
|
||||
}
|
||||
var importNamespaces slicer.StringSlicer
|
||||
for _, tt := range tests {
|
||||
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)
|
||||
}
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user