diff --git a/annotation/component.go b/annotation/component.go new file mode 100644 index 0000000..f7fde6b --- /dev/null +++ b/annotation/component.go @@ -0,0 +1,6 @@ +package annotation + +type Component struct { + Names *[]string + Scope *ScopeType +} diff --git a/annotation/inject.go b/annotation/inject.go new file mode 100644 index 0000000..730b3d8 --- /dev/null +++ b/annotation/inject.go @@ -0,0 +1,6 @@ +package annotation + +type Inject struct { + Name *string + Required *bool +} diff --git a/annotation/scope.go b/annotation/scope.go new file mode 100644 index 0000000..dd6dee2 --- /dev/null +++ b/annotation/scope.go @@ -0,0 +1,8 @@ +package annotation + +type ScopeType int + +const ( + ScopeTypeSingleton ScopeType = iota + ScopeTypeTransiant +) diff --git a/glide.yaml b/glide.yaml new file mode 100644 index 0000000..ca1272a --- /dev/null +++ b/glide.yaml @@ -0,0 +1,2 @@ +package: git.loafle.net/commons_go/di +import: [] diff --git a/registry/definition.go b/registry/definition.go new file mode 100644 index 0000000..afe057a --- /dev/null +++ b/registry/definition.go @@ -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 { + +} \ No newline at end of file diff --git a/registry/registry.go b/registry/registry.go index f1f7321..179cc66 100644 --- a/registry/registry.go +++ b/registry/registry.go @@ -1,6 +1,11 @@ package registry -import "reflect" +import ( + "fmt" + "reflect" + + cdur "git.loafle.net/commons_go/di/util/reflect" +) type ComponentRegistry interface { RegisterType(t reflect.Type, name string) error @@ -9,3 +14,36 @@ type ComponentRegistry interface { GetInstance(t reflect.Type) (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 +} diff --git a/util/reflect/type.go b/util/reflect/type.go new file mode 100644 index 0000000..c9fa3f6 --- /dev/null +++ b/util/reflect/type.go @@ -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() +}