mirror of
https://github.com/wailsapp/wails.git
synced 2025-05-03 01:30:32 +08:00
Move struct cache into parsed package
Fix bug in package path calculation
This commit is contained in:
parent
d0769ecd1c
commit
b606561f8d
@ -10,7 +10,6 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var packageCache = make(map[string]*ParsedPackage)
|
var packageCache = make(map[string]*ParsedPackage)
|
||||||
var structCache = make(map[string]map[structName]*StructDef)
|
|
||||||
|
|
||||||
type packageName = string
|
type packageName = string
|
||||||
type structName = string
|
type structName = string
|
||||||
@ -51,6 +50,7 @@ type ParsedPackage struct {
|
|||||||
Pkg *ast.Package
|
Pkg *ast.Package
|
||||||
Name string
|
Name string
|
||||||
Dir string
|
Dir string
|
||||||
|
structCache map[structName]*StructDef
|
||||||
}
|
}
|
||||||
|
|
||||||
type Project struct {
|
type Project struct {
|
||||||
@ -97,11 +97,12 @@ func (p *Project) parseDirectory(dir string) (map[string]*ParsedPackage, error)
|
|||||||
for packageName, pkg := range pkgs {
|
for packageName, pkg := range pkgs {
|
||||||
parsedPackage := &ParsedPackage{
|
parsedPackage := &ParsedPackage{
|
||||||
Pkg: pkg,
|
Pkg: pkg,
|
||||||
|
Name: packageName,
|
||||||
Dir: getDirectoryForPackage(pkg),
|
Dir: getDirectoryForPackage(pkg),
|
||||||
|
structCache: make(map[structName]*StructDef),
|
||||||
}
|
}
|
||||||
packageCache[dir] = parsedPackage
|
packageCache[dir] = parsedPackage
|
||||||
result[packageName] = parsedPackage
|
result[packageName] = parsedPackage
|
||||||
structCache[packageName] = make(map[structName]*StructDef)
|
|
||||||
}
|
}
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
@ -200,7 +201,7 @@ func (p *Project) findApplicationNewCalls(pkgs map[string]*ParsedPackage) (err e
|
|||||||
// Check if the lit is an ident
|
// Check if the lit is an ident
|
||||||
ident, ok := boundStructLit.Type.(*ast.Ident)
|
ident, ok := boundStructLit.Type.(*ast.Ident)
|
||||||
if ok {
|
if ok {
|
||||||
err = p.parseBoundStructMethods(ident.Name, thisPackage)
|
err = p.parseBoundStructMethods(ident.Name, pkg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
@ -278,10 +279,10 @@ func (p *Project) addBoundMethods(packageName string, name string, boundMethods
|
|||||||
p.BoundMethods[packageName][name] = boundMethods
|
p.BoundMethods[packageName][name] = boundMethods
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Project) parseBoundStructMethods(name string, pkg *ast.Package) error {
|
func (p *Project) parseBoundStructMethods(name string, pkg *ParsedPackage) error {
|
||||||
var methods []*BoundMethod
|
var methods []*BoundMethod
|
||||||
// Iterate over all files in the package
|
// Iterate over all files in the package
|
||||||
for _, file := range pkg.Files {
|
for _, file := range pkg.Pkg.Files {
|
||||||
// Iterate over all declarations in the file
|
// Iterate over all declarations in the file
|
||||||
for _, decl := range file.Decls {
|
for _, decl := range file.Decls {
|
||||||
// Check if the declaration is a type declaration
|
// Check if the declaration is a type declaration
|
||||||
@ -315,7 +316,7 @@ func (p *Project) parseBoundStructMethods(name string, pkg *ast.Package) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Project) parseParameters(params *ast.FieldList, pkg *ast.Package) []*Parameter {
|
func (p *Project) parseParameters(params *ast.FieldList, pkg *ParsedPackage) []*Parameter {
|
||||||
var result []*Parameter
|
var result []*Parameter
|
||||||
for _, field := range params.List {
|
for _, field := range params.List {
|
||||||
var theseFields []*Parameter
|
var theseFields []*Parameter
|
||||||
@ -339,7 +340,7 @@ func (p *Project) parseParameters(params *ast.FieldList, pkg *ast.Package) []*Pa
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Project) parseParameterType(field *ast.Field, pkg *ast.Package) *ParameterType {
|
func (p *Project) parseParameterType(field *ast.Field, pkg *ParsedPackage) *ParameterType {
|
||||||
var result ParameterType
|
var result ParameterType
|
||||||
result.Name = getTypeString(field.Type)
|
result.Name = getTypeString(field.Type)
|
||||||
switch t := field.Type.(type) {
|
switch t := field.Type.(type) {
|
||||||
@ -359,7 +360,7 @@ func (p *Project) parseParameterType(field *ast.Field, pkg *ast.Package) *Parame
|
|||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
if result.IsStruct {
|
if result.IsStruct {
|
||||||
_, ok := structCache[pkg.Name][result.Name]
|
_, ok := pkg.structCache[result.Name]
|
||||||
if !ok {
|
if !ok {
|
||||||
p.getStructDef(result.Name, pkg)
|
p.getStructDef(result.Name, pkg)
|
||||||
}
|
}
|
||||||
@ -367,13 +368,13 @@ func (p *Project) parseParameterType(field *ast.Field, pkg *ast.Package) *Parame
|
|||||||
return &result
|
return &result
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Project) getStructDef(name string, pkg *ast.Package) {
|
func (p *Project) getStructDef(name string, pkg *ParsedPackage) {
|
||||||
_, ok := structCache[pkg.Name][name]
|
_, ok := pkg.structCache[name]
|
||||||
if ok {
|
if ok {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// Iterate over all files in the package
|
// Iterate over all files in the package
|
||||||
for _, file := range pkg.Files {
|
for _, file := range pkg.Pkg.Files {
|
||||||
// Iterate over all declarations in the file
|
// Iterate over all declarations in the file
|
||||||
for _, decl := range file.Decls {
|
for _, decl := range file.Decls {
|
||||||
// Check if the declaration is a type declaration
|
// Check if the declaration is a type declaration
|
||||||
@ -388,7 +389,7 @@ func (p *Project) getStructDef(name string, pkg *ast.Package) {
|
|||||||
Name: name,
|
Name: name,
|
||||||
DocComment: typeDecl.Doc.Text(),
|
DocComment: typeDecl.Doc.Text(),
|
||||||
}
|
}
|
||||||
structCache[pkg.Name][name] = result
|
pkg.structCache[name] = result
|
||||||
result.Fields = p.parseStructFields(structType, pkg)
|
result.Fields = p.parseStructFields(structType, pkg)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -400,7 +401,7 @@ func (p *Project) getStructDef(name string, pkg *ast.Package) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Project) parseStructFields(structType *ast.StructType, pkg *ast.Package) []*Field {
|
func (p *Project) parseStructFields(structType *ast.StructType, pkg *ParsedPackage) []*Field {
|
||||||
var result []*Field
|
var result []*Field
|
||||||
for _, field := range structType.Fields.List {
|
for _, field := range structType.Fields.List {
|
||||||
var theseFields []*Field
|
var theseFields []*Field
|
||||||
@ -419,7 +420,7 @@ func (p *Project) parseStructFields(structType *ast.StructType, pkg *ast.Package
|
|||||||
for _, thisField := range theseFields {
|
for _, thisField := range theseFields {
|
||||||
paramType := p.parseParameterType(field, pkg)
|
paramType := p.parseParameterType(field, pkg)
|
||||||
if paramType.IsStruct {
|
if paramType.IsStruct {
|
||||||
_, ok := structCache[pkg.Name][paramType.Name]
|
_, ok := pkg.structCache[paramType.Name]
|
||||||
if !ok {
|
if !ok {
|
||||||
p.getStructDef(paramType.Name, pkg)
|
p.getStructDef(paramType.Name, pkg)
|
||||||
}
|
}
|
||||||
@ -466,8 +467,13 @@ func isStructType(expr ast.Expr) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func getDirectoryForPackage(pkg *ast.Package) string {
|
func getDirectoryForPackage(pkg *ast.Package) string {
|
||||||
for _, file := range pkg.Files {
|
for filename, _ := range pkg.Files {
|
||||||
return filepath.Dir(file.Name.Name)
|
path := filepath.Dir(filename)
|
||||||
|
abs, err := filepath.Abs(path)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return abs
|
||||||
}
|
}
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user