di/registry/registry_test.go

69 lines
1.5 KiB
Go
Raw Normal View History

2017-12-05 13:28:42 +00:00
package registry
import (
2017-12-08 10:32:59 +00:00
"log"
2017-12-05 13:28:42 +00:00
"reflect"
"testing"
2018-03-15 13:19:44 +00:00
"git.loafle.net/commons_go/di/annotation"
2018-03-15 13:49:25 +00:00
cdia "git.loafle.net/commons_go/di/injection/annotation"
2017-12-05 13:28:42 +00:00
)
func TestRegisterType(t *testing.T) {
2018-03-21 09:28:21 +00:00
var (
err error
css []interface{}
cs interface{}
)
2018-03-15 13:19:44 +00:00
RegisterType(reflect.TypeOf((*AService)(nil)))
RegisterType(reflect.TypeOf((*BService)(nil)))
2017-12-08 10:32:59 +00:00
// RegisterType(reflect.TypeOf((*TestStruct2)(nil)), &cda.ComponentAnnotation{
// Name: "test1",
// })
// RegisterType(reflect.TypeOf((*TestStruct3)(nil)), &cda.ComponentAnnotation{
// Name: "test2",
// })
// fs := getFields(reflect.TypeOf((*TestStruct3)(nil)))
// log.Printf("%v", fs)
RegisterResource("List", []string{"dfdkf", "skgkfg"})
2018-03-21 09:28:21 +00:00
if css, err = GetInstancesByAnnotationName(cdia.ComponentTag); nil != err {
log.Printf("%v \n", err)
}
2018-03-15 13:49:25 +00:00
log.Printf("%v", css)
2018-03-21 09:28:21 +00:00
if css, err = GetInstances([]reflect.Type{
2018-03-15 14:08:51 +00:00
reflect.TypeOf((*AService)(nil)),
reflect.TypeOf((*BService)(nil)),
2018-03-21 09:28:21 +00:00
}); nil != err {
log.Printf("%v \n", err)
}
log.Printf("%v", css)
2018-03-15 13:19:44 +00:00
2018-03-21 09:28:21 +00:00
if cs, err = GetInstance(reflect.TypeOf((*CService)(nil))); nil != err {
log.Printf("%v \n", err)
}
log.Printf("%v", cs.(*CService))
2017-12-08 10:32:59 +00:00
2017-12-05 13:28:42 +00:00
}
2017-12-08 10:32:59 +00:00
type AService struct {
2018-03-15 13:19:44 +00:00
annotation.TypeAnnotation `annotation:"@Component(name=dkdkdf)"`
2017-12-08 10:32:59 +00:00
NameA string
2017-12-06 00:48:03 +00:00
}
2017-12-08 10:32:59 +00:00
type BService struct {
2018-03-15 13:19:44 +00:00
annotation.TypeAnnotation `annotation:"@Component()"`
2017-12-08 10:32:59 +00:00
NameB string
2017-12-06 00:48:03 +00:00
}
2017-12-08 10:32:59 +00:00
type CService struct {
AService *AService `annotation:"@Inject()"`
BService *BService `annotation:"@Inject()"`
List []string `annotation:"@Resource()"`
2017-12-05 13:28:42 +00:00
}