This commit is contained in:
crusader 2017-12-03 22:54:09 +09:00
parent 17ddea24f4
commit dc841686a6
7 changed files with 112 additions and 1 deletions

6
annotation/component.go Normal file
View File

@ -0,0 +1,6 @@
package annotation
type Component struct {
Names *[]string
Scope *ScopeType
}

6
annotation/inject.go Normal file
View File

@ -0,0 +1,6 @@
package annotation
type Inject struct {
Name *string
Required *bool
}

8
annotation/scope.go Normal file
View File

@ -0,0 +1,8 @@
package annotation
type ScopeType int
const (
ScopeTypeSingleton ScopeType = iota
ScopeTypeTransiant
)

2
glide.yaml Normal file
View File

@ -0,0 +1,2 @@
package: git.loafle.net/commons_go/di
import: []

28
registry/definition.go Normal file
View File

@ -0,0 +1,28 @@
package registry
import "reflect"
type typeDefinition struct {
pkgName string
typeName string
targetType reflect.Type
realType reflect.Type
fields map[string]
}
type fieldDefinition struct {
pkgName string
typeName string
targetType reflect.Type
realType reflect.Type
}
type Service struct {
DAO *DAO `di-annotation:""`
}
type DAO struct {
}

View File

@ -1,6 +1,11 @@
package registry package registry
import "reflect" import (
"fmt"
"reflect"
cdur "git.loafle.net/commons_go/di/util/reflect"
)
type ComponentRegistry interface { type ComponentRegistry interface {
RegisterType(t reflect.Type, name string) error RegisterType(t reflect.Type, name string) error
@ -9,3 +14,36 @@ type ComponentRegistry interface {
GetInstance(t reflect.Type) (interface{}, error) GetInstance(t reflect.Type) (interface{}, error)
GetInstanceByName(name string) (interface{}, error) GetInstanceByName(name string) (interface{}, error)
} }
type defaultComponentRegistry struct {
definitions map[string]reflect.Type
}
func (cr *defaultComponentRegistry) RegisterType(t reflect.Type, name string) error {
if nil == t {
return fmt.Errorf("DI: t[reflect.Type] is nil")
}
if !cdur.IsTypeKind(t, reflect.Struct, true) {
return fmt.Errorf("DI: t[reflect.Type] must be specified but is %v", t)
}
rt, pkgName, tName := cdur.GetTypeName(t, true)
cr.definitions[name] = rt
return nil
}
func (cr *defaultComponentRegistry) RegisterFactory(i interface{}) error {
return nil
}
func (cr *defaultComponentRegistry) GetInstance(t reflect.Type) (interface{}, error) {
return nil, nil
}
func (cr *defaultComponentRegistry) GetInstanceByName(name string) (interface{}, error) {
return nil, nil
}

23
util/reflect/type.go Normal file
View File

@ -0,0 +1,23 @@
package reflect
import (
"reflect"
)
func IsTypeKind(t reflect.Type, kind reflect.Kind, removePtr bool) bool {
if reflect.Ptr == t.Kind() {
if removePtr {
return IsTypeKind(t.Elem(), kind, removePtr)
}
}
return kind == t.Kind()
}
func GetTypeName(t reflect.Type) (realType reflect.Type, pkgName string, name string) {
if reflect.Ptr == t.Kind() {
return GetTypeName(t.Elem())
}
return t, t.PkgPath(), t.Name()
}