From 27b4a984d513cfa8d8bab7e3277a4084020ac2a6 Mon Sep 17 00:00:00 2001 From: Travis McLane Date: Wed, 20 Sep 2023 15:08:11 -0500 Subject: [PATCH] [v3] process pointer and non-pointer receiver functions --- v3/internal/parser/parser.go | 85 +++++++++++++++++++----------------- 1 file changed, 46 insertions(+), 39 deletions(-) diff --git a/v3/internal/parser/parser.go b/v3/internal/parser/parser.go index c6cc546b..b1ec6d15 100644 --- a/v3/internal/parser/parser.go +++ b/v3/internal/parser/parser.go @@ -391,51 +391,58 @@ func (p *Project) parseBoundStructMethods(name string, pkg *ParsedPackage) error // Iterate over all declarations in the file for _, decl := range file.Decls { // Check if the declaration is a type declaration - if funcDecl, ok := decl.(*ast.FuncDecl); ok && funcDecl.Recv != nil { - // Check if the function has a receiver argument of the struct type - recvType, ok := funcDecl.Recv.List[0].Type.(*ast.StarExpr) - 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) + if funcDecl, ok := decl.(*ast.FuncDecl); ok && funcDecl.Recv != nil && funcDecl.Name.IsExported() { + var ident *ast.Ident + var ok bool - var alias *uint32 - var err error - // Check for the text `wails:methodID ` - if funcDecl.Doc != nil { - 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 + switch v := funcDecl.Recv.List[0].Type.(type) { + case *ast.StarExpr: + recv := funcDecl.Recv.List[0].Type.(*ast.StarExpr) + ident, ok = recv.X.(*ast.Ident) + case *ast.Ident: + ident, ok = funcDecl.Recv.List[0].Type.(*ast.Ident) + } + if 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 ` + if funcDecl.Doc != nil { + 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 - } - // Add the method to the list of methods - method := &BoundMethod{ - ID: id, - Name: funcDecl.Name.Name, - DocComment: funcDecl.Doc.Text(), - Alias: alias, - } - - if funcDecl.Type.Params != nil { - method.Inputs = p.parseParameters(funcDecl.Type.Params, pkg) - } - if funcDecl.Type.Results != nil { - method.Outputs = p.parseParameters(funcDecl.Type.Results, pkg) - } - - methods = append(methods, method) } + id, err := hash.Fnv(fqn) + if err != nil { + return err + } + // Add the method to the list of methods + method := &BoundMethod{ + ID: id, + Name: funcDecl.Name.Name, + DocComment: funcDecl.Doc.Text(), + Alias: alias, + } + + if funcDecl.Type.Params != nil { + method.Inputs = p.parseParameters(funcDecl.Type.Params, pkg) + } + if funcDecl.Type.Results != nil { + method.Outputs = p.parseParameters(funcDecl.Type.Results, pkg) + } + + methods = append(methods, method) } + } } }