summer

package module
v0.0.0-...-b05c880 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2016 License: Apache-2.0 Imports: 8 Imported by: 0

README

summer

summer like spring ^_&

What is summer ?

image

Summer is a easy tool if you just want a simple IOC/DI framework. Summer work based on tag "sm".

If you want to use summer,you must put stone to the summer basket first,

and you must call "Start",then summer can work for you.

What is Stone ?

Stone is go stone,like bean is javabean.

What is basket ?

A container to store stone,and resolve stones dependents.

simple use:

package main

import (
	"github.com/cocotyty/summer"
	"fmt"
)

func init() {
	summer.Toml(`
	[printer]
	prefix="[PRINT]"`)
	summer.Put(&A{})
	summer.Add("lay", &B{})
	summer.Put(&Cat{})
	summer.Put(&Printer{})
	summer.Start()
}

func main() {
	a := summer.GetStoneWithName("a").(*A)
	a.Call()
}

type A struct {
	// $ means you want to get a stone's field , it happened usually after stones inited
	BoyName string `sm:"$.lay.Name"`
	B       *B `sm:"lay"`
	// yes,we support interface ,tag is stone's name
	C       C `sm:"cat"`
}

func (a *A)Call() {
	a.C.Print()
	fmt.Println("hi ,I am A", "bodys name:", a.BoyName)
	fmt.Println(a.B)
}

type B struct {
	Name string
}

func (this *B)Init() {
	this.Name = "Boy!"
}

type C interface {
	Print()
}
type Printer struct {
	// if you already set the toml plugin config, you can use the #  ,to get value from toml,
	// # is toml plugin's name
	// toml plugin will work after directly dependency resolved,before init
	Prefix string `sm:"#.printer.prefix"`
}

func (printer *Printer)Print(str string) {
	fmt.Println(printer.Prefix + str)
}

type Cat struct {
	// * is mostly used tag,summer will find by the field's name  or the field's type or both
	Printer *Printer `sm:"*"`
}

func (c *Cat)Ready() {
	fmt.Println("my name is cat,i am ready.")
}
func (c *Cat)Print() {
	c.Printer.Print("Little Cat")
}

output:

my name is cat,i am ready.
[PRINT]Little Cat
hi ,I am A bodys name: Boy!
&{Boy!}

really * easy * as you see.

doc

if you have questions ,you can send me a email , my email address is cocotyty@sina.com

Documentation

Overview

#> What is summer ?

Summer is a easy tool if you just want a simple IOC/DI framework. Summer work based on tag "sm".

If you want to use summer,you must put stone to the summer basket first,

and you must call "Start",then summer can work for you.

#> What is Stone ?

Stone is go stone,like bean is javabean.

#> What is basket ?

A container to store stone,and resolve stones dependents.

Index

Constants

This section is empty.

Variables

View Source
var CannotResolveDependencyErr = errors.New("sorry,stone's dependency missed")
View Source
var NotSupportContainsDot = errors.New("sorry we not support name contains a dot")
View Source
var NotSupportStructErr = errors.New("sorry we not support struct now")

Functions

func Add

func Add(name string, stone Stone)

add a stone to the default basket with a name

func PluginRegister

func PluginRegister(p Plugin, pt PluginWorkTime)

register a plugin to basket

func Put

func Put(stone Stone)

put a stone into the default basket

func SetLogLevel

func SetLogLevel(logLevel LogLevel)

func ShutDown

func ShutDown()

func Start

func Start()

func Toml

func Toml(src string) error

func TomlFile

func TomlFile(path string) error

Types

type Basket

type Basket struct {
	// contains filtered or unexported fields
}

func NewBasket

func NewBasket() *Basket

func (*Basket) Add

func (this *Basket) Add(name string, stone Stone)

add a stone to basket,the stone must be struct's pointer

func (*Basket) Each

func (this *Basket) Each(fn func(holder *Holder))

func (*Basket) EachHolder

func (this *Basket) EachHolder(fn func(name string, holder *Holder) bool)

func (*Basket) GetStone

func (this *Basket) GetStone(name string, t reflect.Type) (stone Stone)

get a stone from basket

func (*Basket) GetStoneHolder

func (this *Basket) GetStoneHolder(name string, t reflect.Type) (h *Holder)

get a stone holder from basket

func (*Basket) GetStoneHolderWithName

func (this *Basket) GetStoneHolderWithName(name string) *Holder

get a stone holder from basket

func (*Basket) GetStoneWithName

func (this *Basket) GetStoneWithName(name string) (stone Stone)

get a stone from basket

func (*Basket) PluginRegister

func (this *Basket) PluginRegister(plugin Plugin, t PluginWorkTime)

register a plugin to basket

func (*Basket) Put

func (this *Basket) Put(stone Stone)

put a stone into basket ,the stone must be struct's pointer,the stone name will be that's type's name with first character lowercase for example,if stone's type is Foo then the stone will get a name that is "foo"

func (*Basket) PutDelayField

func (this *Basket) PutDelayField(field *DelayField)

func (*Basket) ShutDown

func (this *Basket) ShutDown()

shutdown will call all stone's Destroy method

func (*Basket) Start

func (this *Basket) Start()

start work

1 : summer will resolve the direct dependency

# 2 : summer will call all stones's Init method<br/> if a directly depend on b, b directly depend on c and d ,then a will init after b init,and b will after c and d

# 3 : summer will call all stones's Ready method<br/> if a depend on b, b depend on c and d ,then a will init after b init,and b will after c and d

type DelayField

type DelayField struct {
	Holder *Holder
	// contains filtered or unexported fields
}

type Destroy

type Destroy interface {
	Destroy()
}

a stone can Destroy

type Holder

type Holder struct {
	Stone        Stone
	Class        reflect.Type
	PointerClass reflect.Type
	Value        reflect.Value
	Basket       *Basket
	Dependents   []*Holder
}

a holder that can hold stone

func (*Holder) ResolveDirectlyDependents

func (this *Holder) ResolveDirectlyDependents()

func (*Holder) SetDirectDependValue

func (this *Holder) SetDirectDependValue(fieldValue reflect.Value, fieldInfo reflect.StructField)

in this step we try to find the stone which the field need

type Init

type Init interface {
	Init()
}

a stone can init

type LogLevel

type LogLevel int
const (
	DebugLevel LogLevel = iota
	InfoLevel
	WarnLevel
	ErrorLevel
	PanicLevel
	FatalLevel
)

type Plugin

type Plugin interface {
	// look up the value which field wanted
	Look(Holder *Holder, path string, sf *reflect.StructField) reflect.Value
	// tell  summer the plugin prefix
	Prefix() string
	// zIndex represent the sequence of plugins
	ZIndex() int
}

type PluginWorkTime

type PluginWorkTime int
const (
	BeforeInit PluginWorkTime = iota
	AfterInit
)

type Provider

type Provider interface {
	Provide() interface{}
}

type ProviderPlugin

type ProviderPlugin struct {
}

func (*ProviderPlugin) Look

func (this *ProviderPlugin) Look(holder *Holder, path string, sf *reflect.StructField) (need reflect.Value)

func (*ProviderPlugin) Prefix

func (this *ProviderPlugin) Prefix() string

func (*ProviderPlugin) ZIndex

func (this *ProviderPlugin) ZIndex() int

zIndex represent the sequence of plugins

type Ready

type Ready interface {
	Ready()
}

a stone can ready

type RefPlugin

type RefPlugin struct {
	// contains filtered or unexported fields
}

func (*RefPlugin) Look

func (this *RefPlugin) Look(Holder *Holder, path string, sf *reflect.StructField) reflect.Value

func (*RefPlugin) Prefix

func (this *RefPlugin) Prefix() string

func (*RefPlugin) ZIndex

func (this *RefPlugin) ZIndex() int

type SimpleLog

type SimpleLog struct {
	// contains filtered or unexported fields
}

func NewSimpleLog

func NewSimpleLog(module string, logLevel LogLevel) *SimpleLog

func (*SimpleLog) Debug

func (log *SimpleLog) Debug(args ...interface{})

func (*SimpleLog) Error

func (log *SimpleLog) Error(args ...interface{})

func (*SimpleLog) Fatal

func (log *SimpleLog) Fatal(args ...interface{})

func (*SimpleLog) Info

func (log *SimpleLog) Info(args ...interface{})

func (*SimpleLog) Panic

func (log *SimpleLog) Panic(args ...interface{})

func (*SimpleLog) Println

func (log *SimpleLog) Println(args ...interface{})

func (*SimpleLog) SetLevel

func (sl *SimpleLog) SetLevel(logLevel LogLevel) *SimpleLog

func (*SimpleLog) Warn

func (log *SimpleLog) Warn(args ...interface{})

type SimpleLogger

type SimpleLogger struct {
	// contains filtered or unexported fields
}

func NewSimpleLogger

func NewSimpleLogger(logLevel LogLevel) *SimpleLogger

func (*SimpleLogger) Module

func (sl *SimpleLogger) Module(module string) *SimpleLog

type Stone

type Stone interface{}

a stone is a go stone as you know

func GetStone

func GetStone(name string, t reflect.Type) (stone Stone)

get a stone with the name and the type

func GetStoneWithName

func GetStoneWithName(name string) (stone Stone)

get a tone with the name

type TomlPlugin

type TomlPlugin struct {
	// contains filtered or unexported fields
}

func (*TomlPlugin) Look

func (this *TomlPlugin) Look(h *Holder, path string, sf *reflect.StructField) reflect.Value

func (*TomlPlugin) Prefix

func (this *TomlPlugin) Prefix() string

func (*TomlPlugin) ZIndex

func (this *TomlPlugin) ZIndex() int

Directories

Path Synopsis
examples
exp
gen

Jump to

Keyboard shortcuts

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