-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefinition.go
159 lines (133 loc) · 3.78 KB
/
definition.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package peas
import (
"github.com/procyon-projects/goo"
"sync"
)
type PeaDefinition interface {
GetTypeName() string
GetPeaType() goo.Type
GetScope() PeaScope
}
type SimplePeaDefinitionOption func(definition *SimplePeaDefinition)
type SimplePeaDefinition struct {
typ goo.Type
scope PeaScope
}
func NewSimplePeaDefinition(typ goo.Type, options ...SimplePeaDefinitionOption) *SimplePeaDefinition {
def := &SimplePeaDefinition{
typ: typ,
}
for _, option := range options {
option(def)
}
if def.scope == "" {
def.scope = SharedScope
}
return def
}
func (def *SimplePeaDefinition) GetTypeName() string {
if def.typ == nil {
return ""
}
if def.typ.IsFunction() {
fun := def.typ.ToFunctionType()
if fun.GetFunctionReturnTypeCount() == 1 {
return fun.GetFunctionReturnTypes()[0].GetFullName()
}
}
return def.typ.String()
}
func (def *SimplePeaDefinition) GetPeaType() goo.Type {
return def.typ
}
func (def *SimplePeaDefinition) GetScope() PeaScope {
return def.scope
}
func WithScope(scope PeaScope) SimplePeaDefinitionOption {
return func(definition *SimplePeaDefinition) {
definition.scope = scope
}
}
type PeaDefinitionRegistry interface {
RegisterPeaDefinition(peaName string, definition PeaDefinition)
RemovePeaDefinition(peaName string)
ContainsPeaDefinition(peaName string) bool
GetPeaDefinition(peaName string) PeaDefinition
GetPeaDefinitionNames() []string
GetPeaDefinitionCount() int
GetPeaNamesByType(typ goo.Type) []string
}
type DefaultPeaDefinitionRegistry struct {
definitions map[string]PeaDefinition
mu sync.RWMutex
}
func NewDefaultPeaDefinitionRegistry() *DefaultPeaDefinitionRegistry {
return &DefaultPeaDefinitionRegistry{
definitions: make(map[string]PeaDefinition, 0),
mu: sync.RWMutex{},
}
}
func (registry *DefaultPeaDefinitionRegistry) RegisterPeaDefinition(peaName string, definition PeaDefinition) {
registry.mu.Lock()
registry.definitions[peaName] = definition
registry.mu.Unlock()
}
func (registry *DefaultPeaDefinitionRegistry) RemovePeaDefinition(peaName string) {
registry.mu.Lock()
if _, ok := registry.definitions[peaName]; ok {
delete(registry.definitions, peaName)
}
registry.mu.Unlock()
}
func (registry *DefaultPeaDefinitionRegistry) ContainsPeaDefinition(peaName string) bool {
var result bool
registry.mu.Lock()
_, result = registry.definitions[peaName]
registry.mu.Unlock()
return result
}
func (registry *DefaultPeaDefinitionRegistry) GetPeaDefinition(peaName string) PeaDefinition {
var def PeaDefinition
registry.mu.Lock()
if val, ok := registry.definitions[peaName]; ok {
def = val
}
registry.mu.Unlock()
return def
}
func (registry *DefaultPeaDefinitionRegistry) GetPeaDefinitionNames() []string {
return getStringMapKeys(registry.definitions)
}
func (registry *DefaultPeaDefinitionRegistry) GetPeaDefinitionCount() int {
return len(registry.definitions)
}
func (registry *DefaultPeaDefinitionRegistry) GetPeaNamesByType(typ goo.Type) []string {
result := make([]string, 0)
for peaName, peaDefinition := range registry.definitions {
peaType := peaDefinition.GetPeaType()
if peaType.IsFunction() {
fun := peaType.ToFunctionType()
if fun.GetFunctionReturnTypeCount() == 1 {
peaType = fun.GetFunctionReturnTypes()[0]
} else {
continue
}
}
match := false
if typ.IsInterface() && peaType.IsStruct() && peaType.ToStructType().Implements(typ.ToInterfaceType()) {
match = true
} else if typ.IsStruct() && peaType.IsStruct() {
if typ.GetGoType() == peaType.GetGoType() && peaType.IsPointer() && !typ.IsPointer() {
match = true
} else if peaType.ToStructType().EmbeddedStruct(typ.ToStructType()) {
match = true
} else if typ.GetGoType() == peaType.GetGoType() {
match = true
}
}
if match {
result = append(result, peaName)
}
}
return result
}