From b5c6ba701988cafa282b009b5ffb9198ee0b8ffc Mon Sep 17 00:00:00 2001 From: harveyshao Date: Wed, 13 Oct 2021 01:47:51 +0800 Subject: [PATCH] opt some --- cmd.go | 117 ++++++++++++++++++++++++++++++++++++++++++ go.sum | 2 + misc.go | 4 ++ misc/wework/bot.go | 38 ++++++++++++++ misc/wework/wework.go | 1 + 5 files changed, 162 insertions(+) create mode 100644 cmd.go create mode 100644 misc/wework/bot.go create mode 100644 misc/wework/wework.go diff --git a/cmd.go b/cmd.go new file mode 100644 index 00000000..dadbf2a0 --- /dev/null +++ b/cmd.go @@ -0,0 +1,117 @@ +package ice + +import ( + "reflect" + "strings" + + kit "shylinux.com/x/toolkits" +) + +func ref(obj interface{}) (reflect.Type, reflect.Value) { + t := reflect.TypeOf(obj) + v := reflect.ValueOf(obj) + if t.Kind() == reflect.Ptr { + t, v = t.Elem(), v.Elem() + } + return t, v +} +func val(m *Message, arg ...string) []reflect.Value { + args := []reflect.Value{reflect.ValueOf(m)} + for _, v := range arg { + args = append(args, reflect.ValueOf(v)) + } + return args +} +func transMethod(config *Config, command *Command, obj interface{}) { + t, v := ref(obj) + for i := 0; i < v.NumMethod(); i++ { + method := v.Method(i) + var h func(*Message, ...string) + switch method.Interface().(type) { + case func(*Message, ...string): + h = func(m *Message, arg ...string) { method.Call(val(m, arg...)) } + case func(*Message): + h = func(m *Message, arg ...string) { method.Call(val(m)) } + default: + continue + } + + if key := strings.ToLower(t.Method(i).Name); key == "list" { + command.Hand = func(m *Message, c *Context, cmd string, arg ...string) { h(m, arg...) } + } else { + if action, ok := command.Action[key]; !ok { + command.Action[key] = &Action{Hand: h} + } else { + action.Hand = h + } + } + } + +} +func transField(config *Config, command *Command, obj interface{}) { + t, v := ref(obj) + for i := 0; i < v.NumField(); i++ { + if t.Field(i).Type.Kind() == reflect.Struct { + if v.Field(i).CanInterface() { + transField(config, command, v.Field(i).Interface()) + } + } + } + + meta := kit.Value(config.Value, kit.MDB_META) + for i := 0; i < v.NumField(); i++ { + key, tag := t.Field(i).Name, t.Field(i).Tag + if data := tag.Get("data"); data != "" { + kit.Value(meta, key, data) + } + + if name := tag.Get("name"); name != "" { + if help := tag.Get("help"); key == "list" { + command.Name, command.Help = name, help + config.Name, config.Help = name, help + } else if action, ok := command.Action[key]; ok { + action.Name, action.Help = name, help + } + } + } +} +func Cmd(key string, obj interface{}) { + if obj == nil { + return + } + command := &Command{Action: map[string]*Action{}} + config := &Config{Value: kit.Data()} + transMethod(config, command, obj) + transField(config, command, obj) + + last := Index + list := strings.Split(key, ".") + for i := 1; i < len(list); i++ { + has := false + Pulse.Search(strings.Join(list[:i], ".")+".", func(p *Context, s *Context) { + has, last = true, s + }) + if !has { + context := &Context{Name: list[i-1]} + last.Register(context, nil) + last = context + } + if i < len(list)-1 { + continue + } + + last.Merge(&Context{Commands: map[string]*Command{ + CTX_INIT: {Hand: func(m *Message, c *Context, cmd string, arg ...string) { + if action, ok := command.Action["init"]; ok { + action.Hand(m, arg...) + } else { + m.Load() + } + }}, + CTX_EXIT: {Hand: func(m *Message, c *Context, cmd string, arg ...string) { + m.Save() + }}, + list[i]: command, + }, Configs: map[string]*Config{list[i]: config}}) + } +} diff --git a/go.sum b/go.sum index 3ff192c8..5a228589 100644 --- a/go.sum +++ b/go.sum @@ -17,3 +17,5 @@ golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= shylinux.com/x/toolkits v0.3.0 h1:rF9SIE02VCfgpuI2P/H7EoJaWpf+zomxAaRHiYD7yjE= shylinux.com/x/toolkits v0.3.0/go.mod h1:8LbYHe7oxBIqb6s4MSOD+4d28QvPdvkyCVtwB/JW7AA= +shylinux.com/x/toolkits v0.3.3 h1:yNH54CANAv7cPT264tpnDt9G2BVfGwLC9y3rxtU6pkg= +shylinux.com/x/toolkits v0.3.3/go.mod h1:8LbYHe7oxBIqb6s4MSOD+4d28QvPdvkyCVtwB/JW7AA= diff --git a/misc.go b/misc.go index 4aad21f2..d8f66ee1 100644 --- a/misc.go +++ b/misc.go @@ -497,3 +497,7 @@ func SelectAction(list map[string]*Action, fields ...string) map[string]*Action } return res } + +func (m *Message) SetAppend(key ...string) { + m.Set(MSG_APPEND, key...) +} diff --git a/misc/wework/bot.go b/misc/wework/bot.go new file mode 100644 index 00000000..cea56da2 --- /dev/null +++ b/misc/wework/bot.go @@ -0,0 +1,38 @@ +package wework + +import ( + ice "shylinux.com/x/icebergs" + "shylinux.com/x/icebergs/base/mdb" + "shylinux.com/x/icebergs/base/web" + kit "shylinux.com/x/toolkits" +) + +const BOT = "bot" + +type Bot struct { + short string `data:"name"` + field string `data:"time,name,token,ekey,hook"` + + create string `name:"list name token ekey hook" help:"创建"` + list string `name:"list name chat text:textarea auto create" help:"机器人"` +} + +func (b Bot) Create(m *ice.Message, arg ...string) { + m.Cmdy(mdb.INSERT, m.PrefixKey(), "", mdb.HASH, arg) +} +func (b Bot) List(m *ice.Message, arg ...string) { + m.Fields(len(arg), m.Config(kit.MDB_FIELD)) + m.Cmdy(mdb.SELECT, m.PrefixKey(), "", mdb.HASH, m.Config(kit.MDB_SHORT), arg) + if len(arg) > 2 { + m.Cmd(web.SPIDE, mdb.CREATE, arg[0], m.Append("hook")) + m.SetAppend() + m.Cmdy(web.SPIDE, arg[0], "", kit.Format(kit.Dict( + "chatid", arg[1], + "msgtype", "text", "text", kit.Dict( + "content", arg[2], + ), + ))) + } +} + +func init() { ice.Cmd("web.chat.wework.bot", Bot{}) } diff --git a/misc/wework/wework.go b/misc/wework/wework.go new file mode 100644 index 00000000..8cc73370 --- /dev/null +++ b/misc/wework/wework.go @@ -0,0 +1 @@ +package wework