diff --git a/type.go b/type.go index 4d5b1b49..d850d9da 100644 --- a/type.go +++ b/type.go @@ -1,27 +1,29 @@ package ice import ( + "github.com/shylinux/toolkits" + + "fmt" + "os" + "strings" "time" ) type Cache struct { - Value string Name string Help string - Hand func(m *Message, x *Cache, arg ...string) string + Value string } type Config struct { - Value interface{} Name string Help string - Hand func(m *Message, x *Config, arg ...string) string + Value interface{} } type Command struct { - Form map[string]int Name string Help interface{} - Auto func(m *Message, c *Context, key string, arg ...string) (ok bool) - Hand func(m *Message, c *Context, key string, arg ...string) (e error) + Form map[string]int + Hand func(m *Message, c *Context, key string, arg ...string) } type Context struct { Name string @@ -35,8 +37,8 @@ type Context struct { context *Context root *Context - exit chan bool - Server + exit chan bool + server Server } type Server interface { Spawn(m *Message, c *Context, arg ...string) Server @@ -49,8 +51,8 @@ type Message struct { time time.Time code int - Meta map[string][]string - Data map[string]interface{} + meta map[string][]string + data map[string]interface{} messages []*Message message *Message @@ -60,3 +62,105 @@ type Message struct { target *Context Hand bool } + +func (m *Message) Spawn(arg ...interface{}) *Message { + msg := &Message{ + time: time.Now(), + code: -1, + + meta: map[string][]string{}, + data: map[string]interface{}{}, + + message: m, + root: m.root, + + source: m.target, + target: m.target, + } + + if len(arg) > 0 { + switch val := arg[0].(type) { + case *Context: + msg.target = val + } + } + return msg +} +func (m *Message) Log(level string, str string, arg ...interface{}) { + fmt.Fprintf(os.Stderr, "%s %s %s\n", time.Now().Format("2006-01-02 15:04:05"), level, fmt.Sprintf(str, arg...)) +} +func (m *Message) Confv(arg ...interface{}) interface{} { + key := "" + switch val := arg[0].(type) { + case string: + key, arg = val, arg[1:] + } + + for _, s := range []*Context{m.target} { + for c := s; c != nil; c = c.context { + if conf, ok := c.Configs[key]; ok { + m.Log("conf", "%s.%s", c.Name, key) + return kit.Value(conf.Value, key) + } + } + } + return nil +} +func (m *Message) Conf(arg ...interface{}) string { + return kit.Format(m.Confv(arg...)) +} +func (m *Message) Cmd(arg ...interface{}) *Message { + list := kit.Trans(arg...) + if len(list) == 0 { + return m + } + for _, s := range []*Context{m.target} { + for c := s; c != nil; c = c.context { + if cmd, ok := c.Commands[list[0]]; ok { + m.Log("cmd", "%s.%s", c.Name, list[0]) + cmd.Hand(m, c, list[0], list[1:]...) + } + } + } + return m +} +func (m *Message) Echo(str string, arg ...interface{}) *Message { + m.meta["result"] = append(m.meta["result"], fmt.Sprintf(str, arg...)) + return m +} +func (m *Message) Result(arg ...interface{}) string { + return strings.Join(m.meta["result"], "") +} + +var Pulse = &Message{ + time: time.Now(), code: 0, + meta: map[string][]string{}, + data: map[string]interface{}{}, + + messages: []*Message{}, message: nil, root: nil, + source: Index, target: Index, Hand: true, +} +var Index = &Context{Name: "root", Help: "元始模块", + Caches: map[string]*Cache{}, + Configs: map[string]*Config{}, + Commands: map[string]*Command{ + "hi": {Name: "hi", Help: "hello", Hand: func(m *Message, c *Context, cmd string, arg ...string) { + m.Echo("hello world") + }}, + }, +} + +func (c *Context) Register(s *Context, x Server) *Context { + if c.contexts == nil { + c.contexts = map[string]*Context{} + } + c.contexts[s.Name] = s + s.root = c.root + s.context = c + s.server = x + return s +} +func init() { + Index.root = Index + Pulse.root = Pulse +}