From 8fd0e06c24df77190c91a46334ed6c3ddb5c0f82 Mon Sep 17 00:00:00 2001 From: Lea Anthony Date: Thu, 23 Feb 2023 20:26:15 +1100 Subject: [PATCH] Use package path instead of name --- v3/internal/parser/parser.go | 33 ++++++++++++++----------- v3/internal/parser/parser_types_test.go | 2 +- 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/v3/internal/parser/parser.go b/v3/internal/parser/parser.go index 15ce1333d..821908fdc 100644 --- a/v3/internal/parser/parser.go +++ b/v3/internal/parser/parser.go @@ -13,7 +13,7 @@ import ( var packageCache = make(map[string]*ParsedPackage) -type packageName = string +type packagePath = string type structName = string type StructDef struct { @@ -51,18 +51,19 @@ type Field struct { type ParsedPackage struct { Pkg *ast.Package Name string + Path string Dir string - structCache map[structName]*StructDef + StructCache map[structName]*StructDef } type Project struct { Path string - BoundMethods map[packageName]map[structName][]*BoundMethod + BoundMethods map[packagePath]map[structName][]*BoundMethod } func ParseProject(projectPath string) (*Project, error) { result := &Project{ - BoundMethods: make(map[packageName]map[structName][]*BoundMethod), + BoundMethods: make(map[packagePath]map[structName][]*BoundMethod), } pkgs, err := result.parseDirectory(projectPath) if err != nil { @@ -100,8 +101,9 @@ func (p *Project) parseDirectory(dir string) (map[string]*ParsedPackage, error) parsedPackage := &ParsedPackage{ Pkg: pkg, Name: packageName, + Path: packageName, Dir: getDirectoryForPackage(pkg), - structCache: make(map[structName]*StructDef), + StructCache: make(map[structName]*StructDef), } packageCache[dir] = parsedPackage result[packageName] = parsedPackage @@ -265,12 +267,12 @@ func (p *Project) findApplicationNewCalls(pkgs map[string]*ParsedPackage) (err e return nil } -func (p *Project) addBoundMethods(packageName string, name string, boundMethods []*BoundMethod) { - _, ok := p.BoundMethods[packageName] +func (p *Project) addBoundMethods(packagePath string, name string, boundMethods []*BoundMethod) { + _, ok := p.BoundMethods[packagePath] if !ok { - p.BoundMethods[packageName] = make(map[structName][]*BoundMethod) + p.BoundMethods[packagePath] = make(map[structName][]*BoundMethod) } - p.BoundMethods[packageName][name] = boundMethods + p.BoundMethods[packagePath][name] = boundMethods } func (p *Project) parseBoundStructMethods(name string, pkg *ParsedPackage) error { @@ -304,7 +306,7 @@ func (p *Project) parseBoundStructMethods(name string, pkg *ParsedPackage) error } } } - p.addBoundMethods(pkg.Name, name, methods) + p.addBoundMethods(pkg.Path, name, methods) return nil } @@ -353,7 +355,7 @@ func (p *Project) parseParameterType(field *ast.Field, pkg *ParsedPackage) *Para default: } if result.IsStruct { - _, ok := pkg.structCache[result.Name] + _, ok := pkg.StructCache[result.Name] if !ok { p.getStructDef(result.Name, pkg) } @@ -362,7 +364,7 @@ func (p *Project) parseParameterType(field *ast.Field, pkg *ParsedPackage) *Para } func (p *Project) getStructDef(name string, pkg *ParsedPackage) { - _, ok := pkg.structCache[name] + _, ok := pkg.StructCache[name] if ok { return } @@ -382,7 +384,7 @@ func (p *Project) getStructDef(name string, pkg *ParsedPackage) { Name: name, DocComment: typeDecl.Doc.Text(), } - pkg.structCache[name] = result + pkg.StructCache[name] = result result.Fields = p.parseStructFields(structType, pkg) } } @@ -413,7 +415,7 @@ func (p *Project) parseStructFields(structType *ast.StructType, pkg *ParsedPacka for _, thisField := range theseFields { paramType := p.parseParameterType(field, pkg) if paramType.IsStruct { - _, ok := pkg.structCache[paramType.Name] + _, ok := pkg.StructCache[paramType.Name] if !ok { p.getStructDef(paramType.Name, pkg) } @@ -446,8 +448,9 @@ func (p *Project) getParsedPackageFromName(packageName string, currentPackage *P return &ParsedPackage{ Pkg: pkg, Name: packageName, + Path: path, Dir: dir, - structCache: make(map[string]*StructDef), + StructCache: make(map[string]*StructDef), }, nil } } diff --git a/v3/internal/parser/parser_types_test.go b/v3/internal/parser/parser_types_test.go index 524a2ac21..e5563be76 100644 --- a/v3/internal/parser/parser_types_test.go +++ b/v3/internal/parser/parser_types_test.go @@ -966,7 +966,7 @@ func TestParseDirectory(t *testing.T) { }, }, }, - "services": { + "github.com/wailsapp/wails/v3/internal/parser/testdata/struct_literal_multiple_other/services": { "OtherService": { { Name: "Yay",