di-go/registry/registry_test.go

69 lines
1.5 KiB
Go
Raw Permalink Normal View History

2018-04-03 09:02:31 +00:00
package registry
import (
"log"
"reflect"
"testing"
"git.loafle.net/commons/di-go/annotation"
cdia "git.loafle.net/commons/di-go/injection/annotation"
)
func TestRegisterType(t *testing.T) {
var (
err error
css []interface{}
cs interface{}
)
RegisterType(reflect.TypeOf((*AService)(nil)))
RegisterType(reflect.TypeOf((*BService)(nil)))
// 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-04-06 16:57:25 +00:00
if css, err = GetInstancesByAnnotationType(cdia.ComponentAnnotationType); nil != err {
2018-04-03 09:02:31 +00:00
log.Printf("%v \n", err)
}
log.Printf("%v", css)
if css, err = GetInstances([]reflect.Type{
reflect.TypeOf((*AService)(nil)),
reflect.TypeOf((*BService)(nil)),
}); nil != err {
log.Printf("%v \n", err)
}
log.Printf("%v", css)
if cs, err = GetInstance(reflect.TypeOf((*CService)(nil))); nil != err {
log.Printf("%v \n", err)
}
log.Printf("%v", cs.(*CService))
}
type AService struct {
2018-04-10 12:30:57 +00:00
annotation.TypeAnnotation `annotation:"@Component(name='dkdkdf')"`
2018-04-03 09:02:31 +00:00
NameA string
}
type BService struct {
annotation.TypeAnnotation `annotation:"@Component()"`
NameB string
}
type CService struct {
AService *AService `annotation:"@Inject()"`
BService *BService `annotation:"@Inject()"`
List []string `annotation:"@Resource()"`
}