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

[binding] TS returns Promise<void> when go returns error (#2247)

This commit is contained in:
Adam Tenderholt 2022-12-28 03:32:29 -08:00 committed by GitHub
parent 0474a9e89a
commit 52f872b65c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 76 additions and 11 deletions

View File

@ -0,0 +1,59 @@
package binding_test
import (
"io/fs"
"os"
"testing"
"github.com/wailsapp/wails/v2/internal/binding"
"github.com/wailsapp/wails/v2/internal/logger"
)
const expectedPromiseBindings = `// Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL
// This file is automatically generated. DO NOT EDIT
export function ErrorReturn(arg1:number):Promise<void>;
export function NoReturn(arg1:string):Promise<void>;
export function SingleReturn(arg1:any):Promise<number>;
export function SingleReturnWithError(arg1:number):Promise<string>;
export function TwoReturn(arg1:any):Promise<string|number>;
`
type PromisesTest struct{}
func (h *PromisesTest) NoReturn(_ string) {}
func (h *PromisesTest) ErrorReturn(_ int) error { return nil }
func (h *PromisesTest) SingleReturn(_ interface{}) int { return 0 }
func (h *PromisesTest) SingleReturnWithError(_ int) (string, error) { return "", nil }
func (h *PromisesTest) TwoReturn(_ interface{}) (string, int) { return "", 0 }
func TestPromises(t *testing.T) {
// given
generationDir := t.TempDir()
// setup
testLogger := &logger.Logger{}
b := binding.NewBindings(testLogger, []interface{}{&PromisesTest{}}, []interface{}{}, false)
// then
err := b.GenerateGoBindings(generationDir)
if err != nil {
t.Fatalf("could not generate the Go bindings: %v", err)
}
// then
rawGeneratedBindings, err := fs.ReadFile(os.DirFS(generationDir), "binding_test/PromisesTest.d.ts")
if err != nil {
t.Fatalf("could not read the generated bindings: %v", err)
}
// then
generatedBindings := string(rawGeneratedBindings)
if generatedBindings != expectedPromiseBindings {
t.Fatalf("the generated bindings does not match the expected ones.\nWanted:\n%s\n\nGot:\n%s", expectedPromiseBindings, generatedBindings)
}
}

View File

@ -77,21 +77,27 @@ func (b *Bindings) GenerateGoBindings(baseDir string) error {
args.Add(arg + ":" + goTypeToTypescriptType(input.TypeName, &importNamespaces)) args.Add(arg + ":" + goTypeToTypescriptType(input.TypeName, &importNamespaces))
} }
tsBody.WriteString(args.Join(",") + "):") tsBody.WriteString(args.Join(",") + "):")
returnType := "Promise" // now build Typescript return types
if methodDetails.OutputCount() > 0 { // If there is no return value or only returning error, TS returns Promise<void>
// If returning single value, TS returns Promise<type>
// If returning single value or error, TS returns Promise<type>
// If returning two values, TS returns Promise<type1|type2>
// Otherwise, TS returns Promise<type1> (instead of throwing Go error?)
var returnType string
if methodDetails.OutputCount() == 0 {
returnType = "Promise<void>"
} else if methodDetails.OutputCount() == 1 && methodDetails.Outputs[0].TypeName == "error" {
returnType = "Promise<void>"
} else {
outputTypeName := entityFullReturnType(methodDetails.Outputs[0].TypeName, b.tsPrefix, b.tsSuffix, &importNamespaces) outputTypeName := entityFullReturnType(methodDetails.Outputs[0].TypeName, b.tsPrefix, b.tsSuffix, &importNamespaces)
firstType := goTypeToTypescriptType(outputTypeName, &importNamespaces) firstType := goTypeToTypescriptType(outputTypeName, &importNamespaces)
returnType += "<" + firstType returnType = "Promise<" + firstType
if methodDetails.OutputCount() == 2 { if methodDetails.OutputCount() == 2 && methodDetails.Outputs[1].TypeName != "error" {
if methodDetails.Outputs[1].TypeName != "error" { outputTypeName = entityFullReturnType(methodDetails.Outputs[1].TypeName, b.tsPrefix, b.tsSuffix, &importNamespaces)
outputTypeName = entityFullReturnType(methodDetails.Outputs[1].TypeName, b.tsPrefix, b.tsSuffix, &importNamespaces) secondType := goTypeToTypescriptType(outputTypeName, &importNamespaces)
secondType := goTypeToTypescriptType(outputTypeName, &importNamespaces) returnType += "|" + secondType
returnType += "|" + secondType
}
} }
returnType += ">" returnType += ">"
} else {
returnType = "Promise<void>"
} }
tsBody.WriteString(returnType + ";\n") tsBody.WriteString(returnType + ";\n")
} }