Fix: TS namespace inference

This commit is contained in:
Lea Anthony
2022-04-05 08:26:54 +10:00
parent bb2da2f810
commit 2465538448
2 changed files with 22 additions and 13 deletions
+11 -12
View File
@@ -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)
}
+11 -1
View File
@@ -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
}