mirror of
https://github.com/wailsapp/wails.git
synced 2025-05-02 18:10:48 +08:00
[binding] TS returns Promise<void> when go returns error (#2247)
This commit is contained in:
parent
0474a9e89a
commit
52f872b65c
@ -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)
|
||||
}
|
||||
}
|
@ -77,21 +77,27 @@ func (b *Bindings) GenerateGoBindings(baseDir string) error {
|
||||
args.Add(arg + ":" + goTypeToTypescriptType(input.TypeName, &importNamespaces))
|
||||
}
|
||||
tsBody.WriteString(args.Join(",") + "):")
|
||||
returnType := "Promise"
|
||||
if methodDetails.OutputCount() > 0 {
|
||||
// now build Typescript return types
|
||||
// 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)
|
||||
firstType := goTypeToTypescriptType(outputTypeName, &importNamespaces)
|
||||
returnType += "<" + firstType
|
||||
if methodDetails.OutputCount() == 2 {
|
||||
if methodDetails.Outputs[1].TypeName != "error" {
|
||||
outputTypeName = entityFullReturnType(methodDetails.Outputs[1].TypeName, b.tsPrefix, b.tsSuffix, &importNamespaces)
|
||||
secondType := goTypeToTypescriptType(outputTypeName, &importNamespaces)
|
||||
returnType += "|" + secondType
|
||||
}
|
||||
returnType = "Promise<" + firstType
|
||||
if methodDetails.OutputCount() == 2 && methodDetails.Outputs[1].TypeName != "error" {
|
||||
outputTypeName = entityFullReturnType(methodDetails.Outputs[1].TypeName, b.tsPrefix, b.tsSuffix, &importNamespaces)
|
||||
secondType := goTypeToTypescriptType(outputTypeName, &importNamespaces)
|
||||
returnType += "|" + secondType
|
||||
}
|
||||
returnType += ">"
|
||||
} else {
|
||||
returnType = "Promise<void>"
|
||||
}
|
||||
tsBody.WriteString(returnType + ";\n")
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user