From 2465538448a6d91a8bbd8b16921217173bb86cb8 Mon Sep 17 00:00:00 2001 From: Lea Anthony Date: Tue, 5 Apr 2022 08:26:54 +1000 Subject: [PATCH] Fix: TS namespace inference --- v2/internal/binding/generate.go | 23 +++++++++++------------ v2/internal/binding/reflect.go | 12 +++++++++++- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/v2/internal/binding/generate.go b/v2/internal/binding/generate.go index 4d9518fa..a509588e 100644 --- a/v2/internal/binding/generate.go +++ b/v2/internal/binding/generate.go @@ -55,18 +55,15 @@ func (b *Bindings) GenerateGoBindings(baseDir string) error { args.Clear() for count, input := range methodDetails.Inputs { arg := fmt.Sprintf("arg%d", count+1) - args.Add(arg + ":" + goTypeToTypescriptType(input.TypeName)) - if strings.ContainsRune(input.TypeName, '.') { - importNamespaces.Add(strings.Split(input.TypeName, ".")[0]) - } + args.Add(arg + ":" + goTypeToTypescriptType(input.TypeName, &importNamespaces)) } tsBody.WriteString(args.Join(",") + "):") returnType := "Promise" if methodDetails.OutputCount() > 0 { - firstType := goTypeToTypescriptType(methodDetails.Outputs[0].TypeName) + firstType := goTypeToTypescriptType(methodDetails.Outputs[0].TypeName, &importNamespaces) returnType += "<" + firstType if methodDetails.OutputCount() == 2 { - secondType := goTypeToTypescriptType(methodDetails.Outputs[1].TypeName) + secondType := goTypeToTypescriptType(methodDetails.Outputs[1].TypeName, &importNamespaces) returnType += "|" + secondType } returnType += ">" @@ -101,7 +98,7 @@ func (b *Bindings) GenerateGoBindings(baseDir string) error { return nil } -func goTypeToJSDocType(input string) string { +func goTypeToJSDocType(input string, importNamespaces *slicer.StringSlicer) string { switch true { case input == "interface{}": return "any" @@ -119,20 +116,22 @@ func goTypeToJSDocType(input string) string { case input == "[]byte": return "string" case strings.HasPrefix(input, "[]"): - arrayType := goTypeToJSDocType(input[2:]) + arrayType := goTypeToJSDocType(input[2:], importNamespaces) return "Array<" + arrayType + ">" default: if strings.ContainsRune(input, '.') { - return input + namespace := getPackageName(input) + importNamespaces.Add(namespace) + return namespace + "." + strings.Split(input, ".")[1] } return "any" } } -func goTypeToTypescriptType(input string) string { +func goTypeToTypescriptType(input string, importNamespaces *slicer.StringSlicer) string { if strings.HasPrefix(input, "[]") { - arrayType := goTypeToJSDocType(input[2:]) + arrayType := goTypeToJSDocType(input[2:], importNamespaces) return "Array<" + arrayType + ">" } - return goTypeToJSDocType(input) + return goTypeToJSDocType(input, importNamespaces) } diff --git a/v2/internal/binding/reflect.go b/v2/internal/binding/reflect.go index a18e93b4..6572407f 100755 --- a/v2/internal/binding/reflect.go +++ b/v2/internal/binding/reflect.go @@ -50,7 +50,6 @@ func (b *Bindings) getMethods(value interface{}) ([]*BoundMethod, error) { structValue := reflect.ValueOf(value) structTypeString := structType.String() baseName := structTypeString[1:] - packageName := strings.Split(baseName, ".")[0] // Process Methods for i := 0; i < structType.NumMethod(); i++ { @@ -94,6 +93,7 @@ func (b *Bindings) getMethods(value interface{}) ([]*BoundMethod, error) { a := reflect.New(typ) s := reflect.Indirect(a).Interface() name := typ.Name() + packageName := getPackageName(thisInput.String()) b.AddStructToGenerateTS(packageName, name, s) } } @@ -103,6 +103,7 @@ func (b *Bindings) getMethods(value interface{}) ([]*BoundMethod, error) { a := reflect.New(thisInput) s := reflect.Indirect(a).Interface() name := thisInput.Name() + packageName := getPackageName(thisInput.String()) b.AddStructToGenerateTS(packageName, name, s) } @@ -133,6 +134,7 @@ func (b *Bindings) getMethods(value interface{}) ([]*BoundMethod, error) { a := reflect.New(typ) s := reflect.Indirect(a).Interface() name := typ.Name() + packageName := getPackageName(thisOutput.String()) b.AddStructToGenerateTS(packageName, name, s) } } @@ -142,6 +144,7 @@ func (b *Bindings) getMethods(value interface{}) ([]*BoundMethod, error) { a := reflect.New(thisOutput) s := reflect.Indirect(a).Interface() name := thisOutput.Name() + packageName := getPackageName(thisOutput.String()) b.AddStructToGenerateTS(packageName, name, s) } @@ -155,3 +158,10 @@ func (b *Bindings) getMethods(value interface{}) ([]*BoundMethod, error) { } return result, nil } + +func getPackageName(in string) string { + result := strings.Split(in, ".")[0] + result = strings.ReplaceAll(result, "[]", "") + result = strings.ReplaceAll(result, "*", "") + return result +}