5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-02 19:50:15 +08:00

Support slices + out params in Models.ts generation. Update website with runtime info

This commit is contained in:
Lea Anthony 2021-11-18 17:54:09 +11:00
parent 9ad2665ad8
commit 8bfec24108
No known key found for this signature in database
GPG Key ID: 33DAF7BB90A58405
2 changed files with 47 additions and 7 deletions

View File

@ -78,10 +78,16 @@ func (b *Bindings) getMethods(value interface{}) ([]*BoundMethod, error) {
input := methodType.In(inputIndex) input := methodType.In(inputIndex)
thisParam := newParameter("", input) thisParam := newParameter("", input)
thisInput := input
if thisInput.Kind() == reflect.Slice {
thisInput = thisInput.Elem()
}
// Process struct pointer params // Process struct pointer params
if input.Kind() == reflect.Ptr { if thisInput.Kind() == reflect.Ptr {
if input.Elem().Kind() == reflect.Struct { if thisInput.Elem().Kind() == reflect.Struct {
typ := input.Elem() typ := thisInput.Elem()
a := reflect.New(typ) a := reflect.New(typ)
s := reflect.Indirect(a).Interface() s := reflect.Indirect(a).Interface()
b.converter.Add(s) b.converter.Add(s)
@ -89,8 +95,8 @@ func (b *Bindings) getMethods(value interface{}) ([]*BoundMethod, error) {
} }
// Process struct params // Process struct params
if input.Kind() == reflect.Struct { if thisInput.Kind() == reflect.Struct {
a := reflect.New(input) a := reflect.New(thisInput)
s := reflect.Indirect(a).Interface() s := reflect.Indirect(a).Interface()
b.converter.Add(s) b.converter.Add(s)
} }
@ -108,6 +114,30 @@ func (b *Bindings) getMethods(value interface{}) ([]*BoundMethod, error) {
for outputIndex := 0; outputIndex < outputParamCount; outputIndex++ { for outputIndex := 0; outputIndex < outputParamCount; outputIndex++ {
output := methodType.Out(outputIndex) output := methodType.Out(outputIndex)
thisParam := newParameter("", output) thisParam := newParameter("", output)
thisOutput := output
if thisOutput.Kind() == reflect.Slice {
thisOutput = thisOutput.Elem()
}
// Process struct pointer params
if thisOutput.Kind() == reflect.Ptr {
if thisOutput.Elem().Kind() == reflect.Struct {
typ := thisOutput.Elem()
a := reflect.New(typ)
s := reflect.Indirect(a).Interface()
b.converter.Add(s)
}
}
// Process struct params
if thisOutput.Kind() == reflect.Struct {
a := reflect.New(thisOutput)
s := reflect.Indirect(a).Interface()
b.converter.Add(s)
}
outputs = append(outputs, thisParam) outputs = append(outputs, thisParam)
} }
boundMethod.Outputs = outputs boundMethod.Outputs = outputs

View File

@ -8,9 +8,19 @@ The runtime is a library that provides utility methods for your application. The
and the aim is to try and keep them at parity where possible. and the aim is to try and keep them at parity where possible.
The Go Runtime is available through importing `github.com/wailsapp/wails/v2/pkg/runtime`. All methods in this package The Go Runtime is available through importing `github.com/wailsapp/wails/v2/pkg/runtime`. All methods in this package
take a context as the first parameter. This context can be obtained from the [OnStartup](/docs/reference/options#OnStartup) take a context as the first parameter. This context can be obtained from the [OnStartup](/docs/reference/options#onstartup)
or [OnDomReady](/docs/reference/options#OnDomReady) hooks. or [OnDomReady](/docs/reference/options#ondomready) hooks.
:::info Note
Whilst the context will be provided to the
[OnStartup](/docs/reference/options#onstartup) method, there's no guarantee the runtime will work in this method as
the window is initialising in a different thread. If
you wish to call runtime methods at startup, use [OnDomReady](/docs/reference/options#ondomready).
:::
The Javascript library is available to the frontend via the `window.runtime` map. There is a runtime package generated when using `dev` The Javascript library is available to the frontend via the `window.runtime` map. There is a runtime package generated when using `dev`
mode that provides Typescript declarations for the runtime. This should be located in the `wailsjs` directory in your mode that provides Typescript declarations for the runtime. This should be located in the `wailsjs` directory in your
frontend directory. frontend directory.