container

package module
v0.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 28, 2023 License: Apache-2.0 Imports: 5 Imported by: 1

README

container

介绍

golang 运行时 依赖注入

快速开始

package main
import "gitee.com/aurora-engine/container"
type Aaa struct {
	Bbb
	*Ccc
	Name string
}

type Bbb struct {
	Name string
	*Ccc
}

type Ccc struct {
	Name string
	*Ddd
}

type Ddd struct {
	Name string
	*Fff
}

type Eee struct {
	Name string
}

type Fff struct {
	Name string
	*Aaa
	*Eee
}

func main(){
	aaa := &Aaa{Name: "aaa"}
	bbb := &Bbb{Name: "bbb"}
	ccc := &Ccc{Name: "ccc"}
	ddd := &Ddd{Name: "ddd"}
	eee := &Eee{Name: "eee"}
	//fff := &Fff{Name: "fff"}
	space := container.NewSpace()
	space.Put("", aaa)
	space.Put("", bbb)
	space.Put("", ccc)
	space.Put("", ddd)
	space.Put("", eee)
	//space.Put("", fff)
	err := space.Start()
	if err != nil {
		fmt.Println(err.Error())
		return
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DepKey

func DepKey(filed reflect.StructField) (string, bool)

DepKey 通过字段结构获取依赖 key 优先获取 tag属性的ref值 没有ref属性值 则获取包路径加类型全面

func Injection

func Injection(field, value reflect.Value) error

Injection 反射初始化 把容器中对应的 value 赋值给 目标结构体的 field 字段

func TypeKey

func TypeKey(t any) string

TypeKey 获取任意类型的 key

Types

type Constructor

type Constructor reflect.Value

Constructor 适配任何构造函数

type Space

type Space struct {
	*sync.Mutex
	// contains filtered or unexported fields
}

func NewSpace

func NewSpace() *Space

func (*Space) Cache

func (space *Space) Cache() map[string]*reflect.Value

Cache 获取主缓存 需要等待容器启动完成获取

func (*Space) CacheCheck

func (space *Space) CacheCheck(key string, value any) (string, error)

CacheCheck 向容器添加缓存检查,返回检查错误或者生成key 容器只接受纸箱结构体的指针变量

func (*Space) Constructor

func (space *Space) Constructor(New ...any)

func (*Space) Get

func (space *Space) Get(ref string) *reflect.Value

Get 从容器中获取引用,存指针的目的在于方便校验 nil

func (*Space) Put

func (space *Space) Put(key string, value any) error

Put 向容器中存放依赖项 key :作为容器中的唯一标识,如果key为空字符串则会生成 value的默认key,默认key是包名全路径和字符串类型来表示

func (*Space) Puts added in v0.0.2

func (space *Space) Puts() error

func (*Space) Start

func (space *Space) Start() error

Start 启动容器 启动容器将 kv装在的属性进行初始化

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL