-
Notifications
You must be signed in to change notification settings - Fork 0
/
summoner.go
230 lines (207 loc) · 4.39 KB
/
summoner.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package summoner
import (
"fmt"
"reflect"
// "github.com/traefik/yaegi/stdlib"
)
type Typeclass[A any] interface {
Given(A) error
GivenType(any, reflect.Type) error
Summon() (A, error)
SummonType(reflect.Type) (any, error)
}
type Transmute[A any] interface {
Transform() A
}
// func ValueFromTypeName(tname string) (any, error) {
// i := interp.New(interp.Options{GoPath: "/Users/comcx/go"})
// // i.CompilePath(".")
// _, err := i.Eval(`.`)
// if err != nil {
// return nil, err
// }
// x, err := i.Eval(fmt.Sprintf(`%s{}`, tname))
// if err != nil {
// return nil, err
// }
// return x.Interface(), nil
// }
type Summoner[A any] struct {
instances map[reflect.Type]any
rules map[reflect.Type]any
}
type summonError struct {
want reflect.Type
}
func (err *summonError) Error() string {
return fmt.Sprintf("Summon error: expected type %v", err.want)
}
func (self *Summoner[A]) tryBuild(t reflect.Type) (any, error) {
if t.Kind() == reflect.Interface {
return nil, &summonError{t}
}
r := reflect.New(t)
i := 0
var a A
for i < r.Elem().NumField() {
field := r.Elem().Field(i)
instance, err := self.SummonType(field.Type())
if err != nil {
return a, err
}
dependency := reflect.ValueOf(instance)
field.Set(dependency)
i += 1
}
ans := r.Elem().Interface()
// Cache
self.GivenType(ans, t)
return ans, nil
}
func (self *Summoner[A]) Summon() (A, error) {
t := TypeOf[A]()
ev, ok := self.instances[t]
if ok {
return ev.(A), nil
}
var a A
// return a, fmt.Errorf("Instance of %v not found", t)
x, err := self.tryBuild(t)
if err != nil {
return a, err
}
return x.(A), nil
}
func (self *Summoner[A]) SummonType(t reflect.Type) (any, error) {
ev, ok := self.instances[t]
if ok {
return ev, nil
}
// return nil, fmt.Errorf("Instance of %v not found", t)
var a A
x, err := self.tryBuild(t)
if err != nil {
return a, err
}
return x, nil
}
func (self *Summoner[A]) Given(ev A) error {
t := TypeOf[A]()
return self.GivenType(ev, t)
}
func (self *Summoner[A]) GivenType(ev any, t reflect.Type) error {
self.instances[t] = ev
return nil
}
func (self *Summoner[A]) Inspect() string {
devils := ""
for k, v := range self.instances {
devils += fmt.Sprintf("\t%v:\t%v\n", k, v)
}
return fmt.Sprintf("Devils[%d] {\n%v}",
len(self.instances),
devils,
)
}
func (self *Summoner[A]) Rules() string {
rules := ""
for k, v := range self.rules {
rules += fmt.Sprintf("\t%v: %v\n", k, v)
}
return fmt.Sprintf("Rules[%d] {\n%v}",
len(self.rules),
rules,
)
}
var global Summoner[any] = Summoner[any]{
instances: map[reflect.Type]any{},
rules: map[reflect.Type]any{},
}
type RType = reflect.Type
type Type struct {
RType
params []Type
}
func fromReflect(t reflect.Type) Type {
return Type{
RType: t,
params: []Type{},
}
}
func (self *Summoner[A]) Inject(structPtr any) error {
t := reflect.TypeOf(structPtr)
if t.Kind() != reflect.Pointer {
return fmt.Errorf("t.Kind(%v) is not reflect.Pointer", t.Kind())
}
v := reflect.ValueOf(structPtr).Elem()
for i := 0; i < v.NumField(); i += 1 {
field := v.Field(i)
ft := field.Type()
tag := t.Elem().Field(i).Tag.Get("summon")
if len(tag) == 0 {
switch ft.Kind() {
case reflect.Pointer:
self.Inject(field.Interface()) //recursively
case reflect.Struct:
self.Inject(field.Addr().Interface()) //recursively
default:
//pass
}
continue
}
switch tag {
case "none":
continue
case "name":
// err := self.InjectByName(field, tag)
// if err != nil {
// return err
// }
// continue
case "type":
default:
}
//It's leave node
err := self.InjectByType(field)
if err != nil {
return err
}
}
return nil
}
func (self *Summoner[A]) InjectByType(field reflect.Value) error {
ft := field.Type()
switch ft.Kind() {
case reflect.Struct:
x, err := self.tryBuild(field.Type())
if err != nil {
return err
}
field.Set(reflect.ValueOf(x))
case reflect.Interface:
x, err := self.SummonType(field.Type())
if err != nil {
return err
}
field.Set(reflect.ValueOf(x))
case reflect.Pointer:
x, err := self.tryBuild(field.Type().Elem())
if err != nil {
return err
}
p := reflect.New(ft.Elem())
val := reflect.ValueOf(x)
p.Elem().Set(val)
field.Set(p)
default:
x, err := self.SummonType(field.Type())
if err != nil {
return err
}
field.Set(reflect.ValueOf(x))
}
return nil
}
func main() {
fmt.Println("Hello, summoner")
}