-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomponents.go
73 lines (62 loc) · 2.1 KB
/
components.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package context
import (
"github.com/procyon-projects/goo"
core "github.com/procyon-projects/procyon-core"
peas "github.com/procyon-projects/procyon-peas"
"strings"
)
type ScannedPeaDefinition struct {
*peas.SimplePeaDefinition
componentName string
}
func NewScannedPeaDefinition(componentName string, peaType goo.Type) ScannedPeaDefinition {
return ScannedPeaDefinition{
peas.NewSimplePeaDefinition(peaType),
componentName,
}
}
func (definition ScannedPeaDefinition) GetComponentName() string {
return definition.componentName
}
type ScannedPeaNameGenerator struct {
}
func NewScannedPeaNameGenerator() ScannedPeaNameGenerator {
return ScannedPeaNameGenerator{}
}
func (generator ScannedPeaNameGenerator) GenerateName(peaDefinition peas.PeaDefinition) string {
peaTypeName := peaDefinition.GetTypeName()
lastDotIndex := strings.LastIndex(peaTypeName, ".")
shortName := ""
if lastDotIndex != -1 {
shortName = peaTypeName[lastDotIndex+1:]
} else {
shortName = peaTypeName
}
shortName = strings.ToLower(shortName[:1]) + shortName[1:]
return shortName
}
type ComponentPeaDefinitionScanner struct {
peaNameGenerator peas.PeaNameGenerator
peaRegistry peas.PeaDefinitionRegistry
}
func NewComponentPeaDefinitionScanner(registry peas.PeaDefinitionRegistry) ComponentPeaDefinitionScanner {
return ComponentPeaDefinitionScanner{
NewScannedPeaNameGenerator(),
registry,
}
}
func (scanner ComponentPeaDefinitionScanner) DoScan() {
scannedPeaDefinitions := make([]ScannedPeaDefinition, 0)
core.ForEachComponentType(func(componentName string, componentType goo.Type) error {
scannedPeaDefinition := NewScannedPeaDefinition(componentName, componentType)
scannedPeaDefinitions = append(scannedPeaDefinitions, scannedPeaDefinition)
return nil
})
for _, peaDefinition := range scannedPeaDefinitions {
peaName := scanner.peaNameGenerator.GenerateName(peaDefinition)
if !scanner.peaRegistry.ContainsPeaDefinition(peaName) {
peaDefinitionHolder := peas.NewPeaDefinitionHolder(peaName, peaDefinition)
scanner.peaRegistry.RegisterPeaDefinition(peaName, peaDefinitionHolder.GetPeaDefinition())
}
}
}