-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
167 lines (154 loc) · 3.58 KB
/
main.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
package main
import (
"fmt"
"reflect"
)
//type BaseI interface {
//}
//type Base struct {
//}
//
//type A struct {
// Age int
//}
//
//type B struct {
// Name string
//}
//
//type Container struct {
// Deps interface{}
//}
//
//func (i *Container) Inject(deps ...interface{}) {
// i.Deps = deps
//}
//
//func (i *Container) Invoke(deb interface{}) {
// a := i.Deps
// getType := reflect.TypeOf(deb)
// if getType.Kind() == reflect.Ptr {
// getType = getType.Elem()
// }
// getVal := reflect.ValueOf(deb)
// switch getType.Kind() {
// case reflect.Func:
//
// }
// realA := a.([]interface{})
// //realParams := make([]reflect.Value, len(realA)) //参数
// realParams := make([]reflect.Value, 1) //参数
// for _, item := range realA {
// t1 := reflect.TypeOf(item).Elem().Name()
// // todo 需要获取 deb函数的参数,当前传入的 deb的名称
// if t1 == "B" {
// realParams[0] = reflect.ValueOf(item)
// }
// }
//
// getVal.Call(realParams)
//}
//
//func ref(res interface{}) {
// getType := reflect.TypeOf(res)
// getValue := reflect.ValueOf(res)
//
// // getElem := res.Type().Elem()
// v := reflect.New(getType.Elem())
// fmt.Println(getType, getType.Name(), getType.Kind())
// fmt.Println(v)
// fmt.Println("getValue", getValue)
// a := getType.Kind()
//
// fmt.Println(a)
// switch getType.Kind() {
// case reflect.Slice:
// fmt.Println("Kind是:", getType.Kind())
//
// sturct_type := getType.Elem()
// fmt.Println("获取到slice内sturct类型:", sturct_type)
// fmt.Println("判断是什么类型的指针:", getType.Elem().Kind())
// var v reflect.Value
// if getType.Elem().Kind() == reflect.Ptr {
// subgetType := getType.Elem()
// sturct_type = subgetType.Elem()
// v = reflect.New(sturct_type)
// fmt.Println("生在对象信息1:", v, v.Elem(), sturct_type)
// } else {
// //根据类型生成&{},通过.Elem()再转为{}
// v = reflect.New(sturct_type).Elem()
// fmt.Println("生在对象信息2:", v, sturct_type.NumField())
// }
// //根据类型生成slice
// sl := reflect.MakeSlice(getType, 0, 0)
// sl = reflect.Append(sl, v)
// fmt.Println("反射获取slice :", sl.Interface())
// // reflect.NewAt(ptr,sturct_type)
// //根据获取到的v可以进行动态赋值
// // reflect.NewAt(ptr,sturct_type)
// default:
// fmt.Println("err ", getType)
// }
//}
//
//func App(b *B) {
// fmt.Println(b.Name)
//}
type Config struct {
DbName string
Pass string
Addr string
Port int
}
func NewConfig() (*Config, error) {
return &Config{
DbName: "testmysql",
Pass: "123456",
Addr: "localhost",
Port: 3306,
}, nil
}
type MysqlIns struct {
Conn string //test
Conf *Config
}
func NewMysql(c *Config) *MysqlIns {
return &MysqlIns{Conf: c}
}
type container struct {
}
func new() *container {
return &container{}
}
func (c *container) provide(dest any) error {
kind := reflect.TypeOf(dest).Kind()
if kind != reflect.Func {
return fmt.Errorf("provide kind:%v need:%v", kind, reflect.Func)
}
destValue := reflect.ValueOf(dest)
destType := reflect.TypeOf(dest)
numIn := destType.NumIn()
numOut := destType.NumOut()
in := fmt.Sprintf("%s", destType.In(0))
out := fmt.Sprintf("%s", destType.Out(0))
fmt.Println(destType, destValue, numIn, numOut, in, out)
return nil
}
func (c *container) invoke(dest any) error {
kind := reflect.TypeOf(dest).Kind()
if kind != reflect.Func {
return fmt.Errorf("invoke kind:%v need:%v", kind, reflect.Func)
}
return nil
}
func main() {
c := new()
if err := c.provide(NewMysql); err != nil {
fmt.Println(err)
}
if err := c.provide(NewConfig); err != nil {
fmt.Println(err)
}
c.invoke(func(i *MysqlIns) {
})
}