Skip to content

Commit d740943

Browse files
committed
distribute constructor and updater
1 parent 11b8e98 commit d740943

File tree

2 files changed

+111
-28
lines changed

2 files changed

+111
-28
lines changed

generate_components_set.go

Lines changed: 62 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,29 @@ func generateComponentsSetCommand(ctx *cli.Context) error {
2828
return fmt.Errorf("failed to parse module: %w", err)
2929
}
3030

31+
keys := map[string]struct{}{}
32+
for key := range ps.Constructors {
33+
keys[key] = struct{}{}
34+
}
35+
for key := range ps.Updaters {
36+
keys[key] = struct{}{}
37+
}
38+
3139
writer := bytes.Buffer{}
32-
for name, list := range ps.Components {
40+
for name := range keys {
3341
log.Printf("Generating %s\n", name)
3442
writer.Reset()
35-
if err := buildComponentSet(&writer, name, list); err != nil {
43+
cons := ps.Constructors[name]
44+
if cons == nil {
45+
cons = []parser.Component{}
46+
}
47+
48+
upds := ps.Updaters[name]
49+
if upds == nil {
50+
upds = []parser.Component{}
51+
}
52+
53+
if err := buildComponentSet(&writer, name, cons, upds); err != nil {
3654
return fmt.Errorf("failed to build component set: %w", err)
3755
}
3856

@@ -52,11 +70,27 @@ func generateComponentsSetCommand(ctx *cli.Context) error {
5270
return nil
5371
}
5472

55-
func buildComponentSet(writer *bytes.Buffer, setName string, list []parser.Component) error {
73+
func buildComponentSet(writer *bytes.Buffer, setName string, cons []parser.Component, upds []parser.Component) error {
5674
pkgName := map[string]string{}
5775
pkgPathMap := map[string]string{}
5876

59-
for i, comp := range list {
77+
for i, comp := range cons {
78+
if _, ok := pkgName[comp.PackageName]; !ok {
79+
pkgName[comp.PackageName] = comp.PackagePath
80+
pkgPathMap[comp.PackagePath] = comp.PackageName
81+
continue
82+
}
83+
84+
if pkgName[comp.PackageName] == comp.PackagePath {
85+
continue
86+
}
87+
88+
comp.PackageName = fmt.Sprintf("%s%d", comp.PackageName, i)
89+
pkgName[comp.PackageName] = comp.PackagePath
90+
pkgPathMap[comp.PackagePath] = comp.PackageName
91+
}
92+
93+
for i, comp := range upds {
6094
if _, ok := pkgName[comp.PackageName]; !ok {
6195
pkgName[comp.PackageName] = comp.PackagePath
6296
pkgPathMap[comp.PackagePath] = comp.PackageName
@@ -90,7 +124,8 @@ func buildComponentSet(writer *bytes.Buffer, setName string, list []parser.Compo
90124
writer.WriteString("type ")
91125
writer.WriteString(structName)
92126
writer.WriteString(" struct {\n")
93-
writer.WriteString("\tlist []any")
127+
writer.WriteString("\tcons []any\n")
128+
writer.WriteString("\tupds []any\n")
94129
writer.WriteString("\n}\n\n")
95130

96131
writer.WriteString("func New")
@@ -101,8 +136,17 @@ func buildComponentSet(writer *bytes.Buffer, setName string, list []parser.Compo
101136
writer.WriteString("\treturn &")
102137
writer.WriteString(structName)
103138
writer.WriteString("{\n")
104-
writer.WriteString("\t\tlist: []any{\n")
105-
for _, comp := range list {
139+
writer.WriteString("\t\tcons: []any{\n")
140+
for _, comp := range cons {
141+
writer.WriteString("\t\t\t")
142+
writer.WriteString(comp.PackageName)
143+
writer.WriteString(".")
144+
writer.WriteString(comp.FunctionName)
145+
writer.WriteString(",\n")
146+
}
147+
writer.WriteString("\t\t},\n")
148+
writer.WriteString("\t\tupds: []any{\n")
149+
for _, comp := range upds {
106150
writer.WriteString("\t\t\t")
107151
writer.WriteString(comp.PackageName)
108152
writer.WriteString(".")
@@ -115,9 +159,17 @@ func buildComponentSet(writer *bytes.Buffer, setName string, list []parser.Compo
115159

116160
writer.WriteString("func (s *")
117161
writer.WriteString(structName)
118-
writer.WriteString(") List() []any {\n")
119-
writer.WriteString("\tclone := make([]any, len(s.list))\n")
120-
writer.WriteString("\tcopy(clone, s.list)\n")
162+
writer.WriteString(") Constructors() []any {\n")
163+
writer.WriteString("\tclone := make([]any, len(s.cons))\n")
164+
writer.WriteString("\tcopy(clone, s.cons)\n")
165+
writer.WriteString("\treturn clone\n")
166+
writer.WriteString("}\n")
167+
168+
writer.WriteString("func (s *")
169+
writer.WriteString(structName)
170+
writer.WriteString(") Updaters() []any {\n")
171+
writer.WriteString("\tclone := make([]any, len(s.upds))\n")
172+
writer.WriteString("\tcopy(clone, s.upds)\n")
121173
writer.WriteString("\treturn clone\n")
122174
writer.WriteString("}\n")
123175

parser/parser.go

Lines changed: 49 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,16 @@ type Component struct {
1919
}
2020

2121
type Parser struct {
22-
RootPath string
23-
ModuleName string
24-
Components map[string][]Component
22+
RootPath string
23+
ModuleName string
24+
Constructors map[string][]Component
25+
Updaters map[string][]Component
2526
}
2627

2728
func New() *Parser {
2829
return &Parser{
29-
Components: map[string][]Component{},
30+
Constructors: map[string][]Component{},
31+
Updaters: map[string][]Component{},
3032
}
3133
}
3234

@@ -73,7 +75,7 @@ func (p *Parser) ParseFromRoot() error {
7375
return fmt.Errorf("failed to parse from path: %w", err)
7476
}
7577

76-
for _, v := range p.Components {
78+
for _, v := range p.Constructors {
7779
for i := range v {
7880
v[i].PackagePath = filepath.Join(p.ModuleName, v[i].PackagePath)
7981
}
@@ -102,28 +104,33 @@ func (p *Parser) ParseFromPath(path string) error {
102104
packagePath = strings.TrimPrefix(packagePath, "/")
103105
packagePath = filepath.Dir(packagePath)
104106

105-
comps, err := p.ParseFile(path, packagePath)
107+
comps, upds, err := p.ParseFile(path, packagePath)
106108
if err != nil {
107109
return fmt.Errorf("failed to parse file: %w", err)
108110
}
109111

110112
for k, v := range comps {
111-
p.Components[k] = append(p.Components[k], v...)
113+
p.Constructors[k] = append(p.Constructors[k], v...)
114+
}
115+
116+
for k, v := range upds {
117+
p.Updaters[k] = append(p.Updaters[k], v...)
112118
}
113119

114120
return nil
115121
})
116122
}
117123

118-
func (p *Parser) ParseFile(path string, packagePath string) (map[string][]Component, error) {
124+
func (p *Parser) ParseFile(path string, packagePath string) (map[string][]Component, map[string][]Component, error) {
119125
fs := token.NewFileSet()
120126
f, err := parser.ParseFile(fs, path, nil, parser.ParseComments)
121127
if err != nil {
122-
return nil, fmt.Errorf("failed to parse file: %w", err)
128+
return nil, nil, fmt.Errorf("failed to parse file: %w", err)
123129
}
124130

125131
packageName := ""
126-
components := map[string][]Component{}
132+
constructors := map[string][]Component{}
133+
updaters := map[string][]Component{}
127134

128135
ast.Inspect(f, func(n ast.Node) bool {
129136
if n == nil {
@@ -136,20 +143,32 @@ func (p *Parser) ParseFile(path string, packagePath string) (map[string][]Compon
136143
case *ast.FuncDecl:
137144
if x.Recv == nil {
138145
isComp := false
139-
compSets := []string{}
146+
consSets := []string{}
147+
updSets := []string{}
140148
if x.Doc == nil {
141149
return true
142150
}
143151

144152
for _, c := range x.Doc.List {
145153
c.Text = strings.ToLower(strings.TrimSpace(c.Text))
146-
if strings.HasPrefix(c.Text, "// lux:comp") {
154+
155+
if strings.HasPrefix(c.Text, "// lux:cons") {
147156
isComp = true
148-
sp := strings.Split(strings.TrimPrefix(c.Text, "// lux:comp"), " ")
157+
sp := strings.Split(strings.TrimPrefix(c.Text, "// lux:cons"), " ")
158+
for i := range sp {
159+
sp[i] = strings.TrimSpace(sp[i])
160+
}
161+
consSets = append(consSets, sp...)
162+
break
163+
}
164+
165+
if strings.HasPrefix(c.Text, "// lux:upd") {
166+
isComp = false
167+
sp := strings.Split(strings.TrimPrefix(c.Text, "// lux:upd"), " ")
149168
for i := range sp {
150169
sp[i] = strings.TrimSpace(sp[i])
151170
}
152-
compSets = append(compSets, sp...)
171+
updSets = append(updSets, sp...)
153172
break
154173
}
155174
}
@@ -158,12 +177,24 @@ func (p *Parser) ParseFile(path string, packagePath string) (map[string][]Compon
158177
return true
159178
}
160179

161-
for _, compSet := range compSets {
162-
if compSet == "" {
180+
for _, consSet := range consSets {
181+
if consSet == "" {
182+
continue
183+
}
184+
185+
constructors[consSet] = append(constructors[consSet], Component{
186+
PackagePath: packagePath,
187+
PackageName: packageName,
188+
FunctionName: x.Name.Name,
189+
})
190+
}
191+
192+
for _, updSet := range updSets {
193+
if updSet == "" {
163194
continue
164195
}
165196

166-
components[compSet] = append(components[compSet], Component{
197+
updaters[updSet] = append(updaters[updSet], Component{
167198
PackagePath: packagePath,
168199
PackageName: packageName,
169200
FunctionName: x.Name.Name,
@@ -175,5 +206,5 @@ func (p *Parser) ParseFile(path string, packagePath string) (map[string][]Compon
175206
return true
176207
})
177208

178-
return components, nil
209+
return constructors, updaters, nil
179210
}

0 commit comments

Comments
 (0)