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

fix: Prevent type parsing to interfere with package name in typescript generation (#1942)

Before that fix:

The method...

```go
func (h *Handler) RespondToInteraction(interaction interactor.Interaction) {}
```

... would generate...

```ts
export function RespondToInteraction(arg1:number):Promise<Error>;
```

... because the `interaction` package starts with `int` and anything starting with `int` is interpreted as `number`.
This commit is contained in:
Valentin Trinqué 2022-10-10 14:50:55 +02:00 committed by GitHub
parent 504531f340
commit 2a20049ea7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 94 additions and 5 deletions

View File

@ -0,0 +1,64 @@
package binding_test
import (
"io/fs"
"os"
"testing"
"github.com/wailsapp/wails/v2/internal/binding"
"github.com/wailsapp/wails/v2/internal/binding/binding_test/binding_test_import/float_package"
"github.com/wailsapp/wails/v2/internal/binding/binding_test/binding_test_import/int_package"
"github.com/wailsapp/wails/v2/internal/binding/binding_test/binding_test_import/map_package"
"github.com/wailsapp/wails/v2/internal/binding/binding_test/binding_test_import/uint_package"
"github.com/wailsapp/wails/v2/internal/logger"
)
const expectedBindings = `// Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL
// This file is automatically generated. DO NOT EDIT
import {float_package} from '../models';
import {int_package} from '../models';
import {map_package} from '../models';
import {uint_package} from '../models';
export function StartingWithFloat(arg1:float_package.SomeStruct):Promise<void>;
export function StartingWithInt(arg1:int_package.SomeStruct):Promise<void>;
export function StartingWithMap(arg1:map_package.SomeStruct):Promise<void>;
export function StartingWithUint(arg1:uint_package.SomeStruct):Promise<void>;
`
type HandlerTest struct{}
func (h *HandlerTest) StartingWithInt(_ int_package.SomeStruct) {}
func (h *HandlerTest) StartingWithFloat(_ float_package.SomeStruct) {}
func (h *HandlerTest) StartingWithUint(_ uint_package.SomeStruct) {}
func (h *HandlerTest) StartingWithMap(_ map_package.SomeStruct) {}
func TestConflictingPackageName(t *testing.T) {
// given
generationDir := t.TempDir()
// setup
testLogger := &logger.Logger{}
b := binding.NewBindings(testLogger, []interface{}{&HandlerTest{}}, []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/HandlerTest.d.ts")
if err != nil {
t.Fatalf("could not read the generated bindings: %v", err)
}
// then
generatedBindings := string(rawGeneratedBindings)
if generatedBindings != expectedBindings {
t.Fatalf("the generated bindings does not match the expected ones.\nWanted:\n%s\n\nGot:\n%s", expectedBindings, generatedBindings)
}
}

View File

@ -0,0 +1,5 @@
package float_package
type SomeStruct struct {
Name string `json:"string"`
}

View File

@ -0,0 +1,5 @@
package int_package
type SomeStruct struct {
Name string `json:"string"`
}

View File

@ -0,0 +1,5 @@
package map_package
type SomeStruct struct {
Name string `json:"string"`
}

View File

@ -0,0 +1,5 @@
package uint_package
type SomeStruct struct {
Name string `json:"string"`
}

View File

@ -120,6 +120,16 @@ func (b *Bindings) GenerateGoBindings(baseDir string) error {
} }
func goTypeToJSDocType(input string, importNamespaces *slicer.StringSlicer) string { func goTypeToJSDocType(input string, importNamespaces *slicer.StringSlicer) string {
// Verifying this first to ensure we are not converting a type
// coming from a package that has a name matching a golang type, such as:
// - interactor -> int
// - mapper -> map
if strings.ContainsRune(input, '.') {
namespace := getPackageName(input)
importNamespaces.Add(namespace)
return namespace + "." + strings.Split(input, ".")[1]
}
switch true { switch true {
case input == "interface {}" || input == "interface{}": case input == "interface {}" || input == "interface{}":
return "any" return "any"
@ -150,11 +160,6 @@ func goTypeToJSDocType(input string, importNamespaces *slicer.StringSlicer) stri
arrayType := goTypeToJSDocType(input[2:], importNamespaces) arrayType := goTypeToJSDocType(input[2:], importNamespaces)
return "Array<" + arrayType + ">" return "Array<" + arrayType + ">"
default: default:
if strings.ContainsRune(input, '.') {
namespace := getPackageName(input)
importNamespaces.Add(namespace)
return namespace + "." + strings.Split(input, ".")[1]
}
return "any" return "any"
} }
} }