[v3] Support //wails:methodID <uint32> directive in bound method comments.

This commit is contained in:
Lea Anthony 2023-08-27 21:16:30 +10:00
commit 5e1f29eda3
No known key found for this signature in database
GPG key ID: 33DAF7BB90A58405
4 changed files with 28 additions and 8 deletions

View file

@ -8,10 +8,16 @@ type Person struct {
type GreetService struct {
}
// Greet greets a person
//
//wails:methodID 1
func (*GreetService) Greet(name string) string {
return "Hello " + name
}
// GreetPerson greets a person
//
//wails:methodID 2
func (*GreetService) GreetPerson(person Person) string {
return "Hello " + person.name
}

View file

@ -10,19 +10,19 @@ window.go.main = {
/**
* GreetService.Greet
*
* Greet greets a person
* @param name {string}
* @returns {Promise<string>}
**/
Greet: function(name) { wails.CallByID(1411160069, ...Array.prototype.slice.call(arguments, 0)); },
Greet: function(name) { wails.CallByID(1, ...Array.prototype.slice.call(arguments, 0)); },
/**
* GreetService.GreetPerson
*
* GreetPerson greets a person
* @param person {main.Person}
* @returns {Promise<string>}
**/
GreetPerson: function(person) { wails.CallByID(4021313248, ...Array.prototype.slice.call(arguments, 0)); },
GreetPerson: function(person) { wails.CallByID(2, ...Array.prototype.slice.call(arguments, 0)); },
},
};

View file

@ -16,10 +16,6 @@ func main() {
Bind: []any{
&GreetService{},
},
BindAliases: map[uint32]uint32{
1: 1411160069,
2: 4021313248,
},
Assets: application.AssetOptions{
FS: assets,
},

View file

@ -2,6 +2,7 @@ package parser
import (
"fmt"
"github.com/samber/lo"
"github.com/wailsapp/wails/v3/internal/hash"
"go/ast"
"go/build"
@ -81,6 +82,7 @@ type BoundMethod struct {
Inputs []*Parameter
Outputs []*Parameter
ID uint32
Alias *uint32
}
func (m BoundMethod) IDAsString() string {
@ -394,6 +396,21 @@ func (p *Project) parseBoundStructMethods(name string, pkg *ParsedPackage) error
if ok {
if ident, ok := recvType.X.(*ast.Ident); ok && ident.Name == name {
fqn := fmt.Sprintf("%s.%s.%s", pkg.Path, name, funcDecl.Name.Name)
var alias *uint32
var err error
// Check for the text `wails:methodID <integer>`
for _, docstring := range funcDecl.Doc.List {
if strings.Contains(docstring.Text, "//wails:methodID") {
idString := strings.TrimSpace(strings.TrimPrefix(docstring.Text, "//wails:methodID"))
parsedID, err := strconv.ParseUint(idString, 10, 32)
if err != nil {
return fmt.Errorf("invalid value in `wails:methodID` directive: '%s'. Expected a valid uint32 value", idString)
}
alias = lo.ToPtr(uint32(parsedID))
break
}
}
id, err := hash.Fnv(fqn)
if err != nil {
return err
@ -403,6 +420,7 @@ func (p *Project) parseBoundStructMethods(name string, pkg *ParsedPackage) error
ID: id,
Name: funcDecl.Name.Name,
DocComment: funcDecl.Doc.Text(),
Alias: alias,
}
if funcDecl.Type.Params != nil {