mirror of
https://github.com/wailsapp/wails.git
synced 2025-05-02 19:50:15 +08:00
Support anonymous struct fields
This commit is contained in:
parent
cd11c0a83c
commit
cc1a6a3d50
@ -59,9 +59,10 @@ type ParsedPackage struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Project struct {
|
type Project struct {
|
||||||
Path string
|
Path string
|
||||||
BoundMethods map[packagePath]map[structName][]*BoundMethod
|
BoundMethods map[packagePath]map[structName][]*BoundMethod
|
||||||
Models map[packagePath]map[structName]*StructDef
|
Models map[packagePath]map[structName]*StructDef
|
||||||
|
anonymousStructIDCounter int
|
||||||
}
|
}
|
||||||
|
|
||||||
func ParseProject(projectPath string) (*Project, error) {
|
func ParseProject(projectPath string) (*Project, error) {
|
||||||
@ -353,6 +354,20 @@ func (p *Project) parseParameterType(field *ast.Field, pkg *ParsedPackage) *Para
|
|||||||
result.IsPointer = true
|
result.IsPointer = true
|
||||||
case *ast.StructType:
|
case *ast.StructType:
|
||||||
result.IsStruct = true
|
result.IsStruct = true
|
||||||
|
if result.Name == "" {
|
||||||
|
// Anonymous struct
|
||||||
|
result.Name = p.anonymousStructID()
|
||||||
|
// Create a new struct definition
|
||||||
|
result := &StructDef{
|
||||||
|
Name: result.Name,
|
||||||
|
}
|
||||||
|
pkg.StructCache[result.Name] = result
|
||||||
|
// Parse the fields
|
||||||
|
result.Fields = p.parseStructFields(&ast.StructType{
|
||||||
|
Fields: t.Fields,
|
||||||
|
}, pkg)
|
||||||
|
_ = result
|
||||||
|
}
|
||||||
case *ast.SelectorExpr:
|
case *ast.SelectorExpr:
|
||||||
extPackage, err := p.getParsedPackageFromName(t.X.(*ast.Ident).Name, pkg)
|
extPackage, err := p.getParsedPackageFromName(t.X.(*ast.Ident).Name, pkg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -503,6 +518,11 @@ func (p *Project) getPackageFromPath(packagedir string, packagepath string) (*as
|
|||||||
return nil, fmt.Errorf("package not found in imported package %s", packagepath)
|
return nil, fmt.Errorf("package not found in imported package %s", packagepath)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *Project) anonymousStructID() string {
|
||||||
|
p.anonymousStructIDCounter++
|
||||||
|
return fmt.Sprintf("anon%d", p.anonymousStructIDCounter)
|
||||||
|
}
|
||||||
|
|
||||||
func getTypeString(expr ast.Expr) string {
|
func getTypeString(expr ast.Expr) string {
|
||||||
switch t := expr.(type) {
|
switch t := expr.(type) {
|
||||||
case *ast.Ident:
|
case *ast.Ident:
|
||||||
@ -516,7 +536,7 @@ func getTypeString(expr ast.Expr) string {
|
|||||||
case *ast.SelectorExpr:
|
case *ast.SelectorExpr:
|
||||||
return getTypeString(t.Sel)
|
return getTypeString(t.Sel)
|
||||||
default:
|
default:
|
||||||
return "any"
|
return ""
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -865,6 +865,25 @@ func TestParseDirectory(t *testing.T) {
|
|||||||
Package: "main",
|
Package: "main",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
Name: "Details",
|
||||||
|
Type: &ParameterType{
|
||||||
|
Name: "anon1",
|
||||||
|
IsStruct: true,
|
||||||
|
Package: "main",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"anon1": {
|
||||||
|
Name: "anon1",
|
||||||
|
Fields: []*Field{
|
||||||
|
{
|
||||||
|
Name: "Age",
|
||||||
|
Type: &ParameterType{
|
||||||
|
Name: "int",
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -9,8 +9,11 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Person struct {
|
type Person struct {
|
||||||
Name string
|
Name string
|
||||||
Parent *Person
|
Parent *Person
|
||||||
|
Details struct {
|
||||||
|
Age int
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// GreetService is great
|
// GreetService is great
|
||||||
|
Loading…
Reference in New Issue
Block a user