needs.go 1.6 KB
Newer Older
Y
Your Name 已提交
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
package pdao

import (
	"fmt"
	"reflect"
	"sync"
)

//NeedsManager needsManager
type NeedsManager struct {
	daoInterfaces map[string][]*reflect.Value
	lock          sync.Mutex
}

//NewNeedsManager 创建新的needsManager
func NewNeedsManager() *NeedsManager {
	return &NeedsManager{
		daoInterfaces: make(map[string][]*reflect.Value),
		lock:          sync.Mutex{},
	}
}

func (m *NeedsManager) add(key string, v *reflect.Value) {
	m.lock.Lock()
	m.daoInterfaces[key] = append(m.daoInterfaces[key], v)
	m.lock.Unlock()
}

func (m *NeedsManager) set(key string, v reflect.Value) {
	m.lock.Lock()
	for _, e := range m.daoInterfaces[key] {
		e.Set(v)
	}
	delete(m.daoInterfaces, key)
	m.lock.Unlock()
}
func (m *NeedsManager) check() []string {
	m.lock.Lock()
	r := make([]string, 0, len(m.daoInterfaces))
	for pkg := range m.daoInterfaces {

		r = append(r, pkg)
	}
	m.lock.Unlock()
	return r
}

//Need 声明
func (m *NeedsManager) Need(p interface{}) {

	v := reflect.ValueOf(p)

	if v.Kind() != reflect.Ptr {
		panic("must ptr")
	}
	e := v.Elem()
	pkg := key(e.Type())
	if pkg == "" {
		panic("invalid interface")
	}

	if !e.CanSet() {
		panic("invalid interface")
	}
	m.add(pkg, &e)
}

//Set 注入
func (m *NeedsManager) Set(i interface{}) {
	v := reflect.ValueOf(i)
	if v.Kind() != reflect.Ptr {
		panic("must ptr")
	}
	e := v.Elem()
	pkg := key(e.Type())

	if pkg == "" {
		panic("invalid interface")
	}
	m.set(pkg, e)
}
func key(t reflect.Type) string {
	return fmt.Sprintf("%s.%s", t.PkgPath(), t.String())
}

//Check 检查是否实现相关dao类
func (m *NeedsManager) Check() error {
	rs := m.check()
	if len(rs) > 0 {
		return fmt.Errorf("need:%v", rs)
	}
	return nil
}