-
Notifications
You must be signed in to change notification settings - Fork 0
/
ada.go
203 lines (174 loc) · 4.51 KB
/
ada.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package lama
import (
"fmt"
"reflect"
"sync"
)
// NewAda returns a new instance of Ada.
func NewAda() *Ada {
return &Ada{
services: make([]reflect.Value, 0),
values: make(map[reflect.Type]reflect.Value),
}
}
type Ada struct {
services []reflect.Value
values map[reflect.Type]reflect.Value
}
// Register registers one or more services to Ada.
func (s *Ada) Register(services ...interface{}) error {
for _, srv := range services {
err := s.Services(srv)
if err != nil {
return err
}
}
return nil
}
// Services registers a service to Ada.
func (s *Ada) Services(service interface{}) error {
value := reflect.ValueOf(service)
if value.Kind() != reflect.Ptr || value.IsNil() {
return fmt.Errorf("service is not a valid pointer: %v", reflect.TypeOf(service))
}
if value.Elem().Type().Kind() != reflect.Struct {
return fmt.Errorf("service is not a valid struct: %v", reflect.TypeOf(service))
}
if !value.IsValid() || !value.CanInterface() {
return fmt.Errorf("service is not a valid value: %v", reflect.TypeOf(service))
}
provideMethod := value.MethodByName("Provide")
if provideMethod.IsValid() {
provideTyp := provideMethod.Type()
numIn := provideTyp.NumIn()
if numIn > 0 {
return fmt.Errorf("provide method cannot accept parameters: %v", reflect.TypeOf(service))
}
numOut := provideTyp.NumOut()
if numOut > 0 {
values := provideMethod.Call([]reflect.Value{})
for idx, val := range values {
kind := val.Kind()
out := provideTyp.Out(idx)
name := out.String()
if !val.IsValid() || val.IsNil() {
return fmt.Errorf("provide value [%s] is not a valid in %v", name, reflect.TypeOf(service))
}
if kind == reflect.Ptr {
if val.Elem().Type().Kind() != reflect.Struct {
return fmt.Errorf("provide value [%s] is not a valid struct in %v", name, reflect.TypeOf(service))
}
} else {
if kind != reflect.Func {
return fmt.Errorf("provide value [%s] is not a valid pointer in %v", out.String(), reflect.TypeOf(service))
}
}
s.values[val.Type()] = val
}
}
}
s.services = append(s.services, value)
return nil
}
// Init initializes all registered services.
func (s *Ada) Init() error {
eType := reflect.TypeOf((*error)(nil)).Elem()
for _, srv := range s.services {
method := srv.MethodByName("Init")
if !method.IsValid() {
continue
}
typ := method.Type()
numIn := typ.NumIn()
numOut := typ.NumOut()
args := make([]reflect.Value, numIn)
for i := 0; i < numIn; i++ {
argType := method.Type().In(i)
argValue, exists := s.values[argType]
if !exists {
return fmt.Errorf("missing dependency [%v] for service %s", argType, srv)
}
args[i] = argValue
}
returnValue := method.Call(args)
if numOut > 0 {
returnTyp := typ.Out(0)
if returnTyp.AssignableTo(eType) {
itf := returnValue[0].Interface()
if itf != nil {
return itf.(error)
}
}
}
}
return nil
}
// Serve calls the Serve method of all registered services.
func (s *Ada) Serve() <-chan error {
wg := sync.WaitGroup{}
out := make(chan error)
eType := reflect.TypeOf((*error)(nil)).Elem()
ecType := reflect.TypeOf((chan error)(nil))
for _, srv := range s.services {
method := srv.MethodByName("Serve")
if !method.IsValid() {
continue
}
typ := method.Type()
numIn := typ.NumIn()
numOut := typ.NumOut()
if numIn == 0 && numOut == 1 {
returnTyp := typ.Out(0)
if returnTyp.AssignableTo(eType) {
wg.Add(1)
itf := method.Call([]reflect.Value{})[0].Interface()
if itf != nil {
out <- itf.(error)
}
wg.Done()
} else if returnTyp.AssignableTo(ecType) {
wg.Add(1)
itf := method.Call([]reflect.Value{})[0].Interface()
if itf != nil {
go func() {
for err := range itf.(chan error) {
out <- err
}
wg.Done()
}()
} else {
wg.Done()
}
}
}
}
go func() {
wg.Wait()
close(out)
}()
return out
}
// Stop calls the Stop method of all registered services.
func (s *Ada) Stop() error {
var errList []error
ecType := reflect.TypeOf((*error)(nil)).Elem()
for _, srv := range s.services {
method := srv.MethodByName("Stop")
if !method.IsValid() {
continue
}
typ := method.Type()
numIn := typ.NumIn()
numOut := typ.NumOut()
if numIn == 0 && numOut == 1 && typ.Out(0).AssignableTo(ecType) {
itf := method.Call([]reflect.Value{})[0].Interface()
if itf != nil {
errList = append(errList, itf.(error))
}
}
}
if len(errList) > 0 {
return fmt.Errorf("failed to stop some services: %v", errList)
}
return nil
}