From 70ee348ae918748f3c7da2d2848fcaefbcbaa9e1 Mon Sep 17 00:00:00 2001 From: harveyshao Date: Tue, 14 Jun 2022 17:16:33 +0800 Subject: [PATCH] opt some --- data.go | 20 ++++++++--------- exec.go | 20 ++++++++--------- info.go | 8 +++---- init.go | 2 +- logs.go | 38 ++++++++++++++++----------------- meta.go | 30 +++++++++++++------------- misc.go | 26 +++++++++++----------- option.go | 64 +++++++++++++++++++++++++++---------------------------- render.go | 42 ++++++++++++++++++------------------ type.go | 48 +++++++++++++++++++++-------------------- 10 files changed, 150 insertions(+), 148 deletions(-) diff --git a/data.go b/data.go index e28d90ec..8870f662 100644 --- a/data.go +++ b/data.go @@ -25,20 +25,20 @@ func (m *Message) ConfigSet(keys string, arg ...string) { m.Config(k, kit.Select("", arg, i)) } } -func (m *Message) Config(key string, arg ...interface{}) string { +func (m *Message) Config(key string, arg ...Any) string { if len(arg) > 0 { m.Conf(m.PrefixKey(), kit.Keym(key), arg[0]) } return m.Conf(m.PrefixKey(), kit.Keym(key)) } -func (m *Message) Configv(key string, arg ...interface{}) interface{} { +func (m *Message) Configv(key string, arg ...Any) Any { if len(arg) > 0 { m.Confv(m.PrefixKey(), kit.Keym(key), arg[0]) } return m.Confv(m.PrefixKey(), kit.Keym(key)) } -func (m *Message) Configm(key string, arg ...interface{}) map[string]interface{} { - v, _ := m.Configv(key, arg...).(map[string]interface{}) +func (m *Message) Configm(key string, arg ...Any) Map { + v, _ := m.Configv(key, arg...).(Map) return v } func (m *Message) ConfigSimple(key ...string) (list []string) { @@ -75,17 +75,17 @@ func (m *Message) Load(arg ...string) *Message { return m.Cmd(CONFIG, LOAD, m.Prefix(JSON), arg) } -func (m *Message) Richs(prefix string, chain interface{}, raw interface{}, cb interface{}) (res map[string]interface{}) { +func (m *Message) Richs(prefix string, chain Any, raw Any, cb Any) (res Map) { cache := m.Confm(prefix, chain) if cache == nil { return nil } switch cb := cb.(type) { - case func(*sync.Mutex, string, map[string]interface{}): + case func(*sync.Mutex, string, Map): wg, mu := &sync.WaitGroup{}, &sync.Mutex{} defer wg.Wait() - res = miss.Richs(kit.Keys(prefix, chain), cache, raw, func(key string, value map[string]interface{}) { + res = miss.Richs(kit.Keys(prefix, chain), cache, raw, func(key string, value Map) { wg.Add(1) m.Go(func() { defer wg.Done() @@ -98,7 +98,7 @@ func (m *Message) Richs(prefix string, chain interface{}, raw interface{}, cb in return res } -func (m *Message) Rich(prefix string, chain interface{}, data interface{}) string { +func (m *Message) Rich(prefix string, chain Any, data Any) string { cache := m.Confm(prefix, chain) if cache == nil { cache = kit.Data() @@ -106,7 +106,7 @@ func (m *Message) Rich(prefix string, chain interface{}, data interface{}) strin } return miss.Rich(kit.Keys(prefix, chain), cache, data) } -func (m *Message) Grow(prefix string, chain interface{}, data interface{}) int { +func (m *Message) Grow(prefix string, chain Any, data Any) int { cache := m.Confm(prefix, chain) if cache == nil { cache = kit.Data() @@ -114,7 +114,7 @@ func (m *Message) Grow(prefix string, chain interface{}, data interface{}) int { } return miss.Grow(kit.Keys(prefix, chain), cache, data) } -func (m *Message) Grows(prefix string, chain interface{}, match string, value string, cb interface{}) map[string]interface{} { +func (m *Message) Grows(prefix string, chain Any, match string, value string, cb Any) Map { cache := m.Confm(prefix, chain) if cache == nil { return nil diff --git a/exec.go b/exec.go index 8f45cfd2..d2efc27e 100644 --- a/exec.go +++ b/exec.go @@ -36,7 +36,7 @@ func (m *Message) TryCatch(msg *Message, silent bool, hand ...func(msg *Message) } return m } -func (m *Message) Assert(expr interface{}) bool { +func (m *Message) Assert(expr Any) bool { switch expr := expr.(type) { case nil: return true @@ -51,17 +51,17 @@ func (m *Message) Assert(expr interface{}) bool { m.Result(ErrPanic, expr) panic(expr) } -func (m *Message) Sleep(d string, arg ...interface{}) *Message { +func (m *Message) Sleep(d string, arg ...Any) *Message { // m.Debug("sleep %s %s", d, kit.FileLine(2, 3)) if time.Sleep(kit.Duration(d)); len(arg) > 0 { m.Cmdy(arg...) } return m } -func (m *Message) Sleep300ms(arg ...interface{}) *Message { return m.Sleep("300ms", arg...) } -func (m *Message) Sleep30ms(arg ...interface{}) *Message { return m.Sleep("30ms", arg...) } -func (m *Message) Sleep3s(arg ...interface{}) *Message { return m.Sleep("3s", arg...) } -func (m *Message) Sleep30s(arg ...interface{}) *Message { return m.Sleep("30s", arg...) } +func (m *Message) Sleep300ms(arg ...Any) *Message { return m.Sleep("300ms", arg...) } +func (m *Message) Sleep30ms(arg ...Any) *Message { return m.Sleep("30ms", arg...) } +func (m *Message) Sleep3s(arg ...Any) *Message { return m.Sleep("3s", arg...) } +func (m *Message) Sleep30s(arg ...Any) *Message { return m.Sleep("30s", arg...) } func (m *Message) Hold(n int) *Message { for ctx := m.target; ctx != nil; ctx = ctx.context { if ctx.wg != nil { @@ -118,7 +118,7 @@ func (m *Message) Back(res *Message) *Message { } return m } -func (m *Message) Go(cb interface{}) *Message { +func (m *Message) Go(cb Any) *Message { task.Put(kit.FileLine(cb, 3), func(task *task.Task) error { m.TryCatch(m, true, func(m *Message) { switch cb := cb.(type) { @@ -144,18 +144,18 @@ func (m *Message) Event(key string, arg ...string) *Message { m.Cmd("event", ACTION, "action", "event", key, arg) return m } -func (m *Message) Right(arg ...interface{}) bool { +func (m *Message) Right(arg ...Any) bool { key := strings.ReplaceAll(kit.Keys(arg...), PS, PT) return m.Option(MSG_USERROLE) == "root" || !m.Warn(m.Cmdx("role", "right", m.Option(MSG_USERROLE), key) != OK, ErrNotRight, kit.Join(kit.Simple(arg), PT), "userrole", m.Option(MSG_USERROLE), "fileline", kit.FileLine(2, 3)) } -func (m *Message) Space(arg interface{}) []string { +func (m *Message) Space(arg Any) []string { if arg == nil || arg == "" || kit.Format(arg) == m.Conf("runtime", "node.name") { return nil } return []string{SPACE, kit.Format(arg)} } -func (m *Message) PodCmd(arg ...interface{}) bool { +func (m *Message) PodCmd(arg ...Any) bool { if pod := m.Option(POD); pod != "" { if m.Option(POD, ""); m.Option(MSG_UPLOAD) != "" { msg := m.Cmd(CACHE, "upload") diff --git a/info.go b/info.go index 5fc24e2f..1438dfde 100644 --- a/info.go +++ b/info.go @@ -43,8 +43,8 @@ var Info = struct { Dump func(w io.Writer, name string, cb func(string)) bool Log func(m *Message, p, l, s string) - render map[string]func(*Message, string, ...interface{}) string - names map[string]interface{} + render map[string]func(*Message, string, ...Any) string + names Map }{ Help: ` ^_^ 欢迎使用冰山框架 ^_^ @@ -60,8 +60,8 @@ source: https://shylinux.com/x/icebergs Dump: func(w io.Writer, name string, cb func(string)) bool { return false }, Log: func(m *Message, p, l, s string) {}, - render: map[string]func(*Message, string, ...interface{}) string{}, - names: map[string]interface{}{}, + render: map[string]func(*Message, string, ...Any) string{}, + names: Map{}, } func FileURI(dir string) string { diff --git a/init.go b/init.go index fa2afcf0..24285b59 100644 --- a/init.go +++ b/init.go @@ -94,7 +94,7 @@ var Index = &Context{Name: "ice", Help: "冰山模块", Configs: map[string]*Con var Pulse = &Message{ time: time.Now(), code: 0, meta: map[string][]string{}, - data: map[string]interface{}{}, + data: Map{}, source: Index, target: Index, Hand: true, } diff --git a/logs.go b/logs.go index c3655d14..ea05be71 100644 --- a/logs.go +++ b/logs.go @@ -10,7 +10,7 @@ import ( log "shylinux.com/x/toolkits/logs" ) -func (m *Message) log(level string, str string, arg ...interface{}) *Message { +func (m *Message) log(level string, str string, arg ...Any) *Message { if log.LogDisable { return m // 禁用日志 } @@ -54,7 +54,7 @@ func (m *Message) log(level string, str string, arg ...interface{}) *Message { kit.Format("%4s->%-4s", m.source.Name, m.target.Name), prefix, level, str, suffix)) return m } -func (m *Message) join(arg ...interface{}) string { +func (m *Message) join(arg ...Any) string { args := kit.Simple(arg...) list := []string{} for i := 0; i < len(args); i += 2 { @@ -67,17 +67,17 @@ func (m *Message) join(arg ...interface{}) string { return kit.Join(list, SP) } -func (m *Message) Log(level string, str string, arg ...interface{}) *Message { +func (m *Message) Log(level string, str string, arg ...Any) *Message { return m.log(level, str, arg...) } -func (m *Message) Info(str string, arg ...interface{}) *Message { +func (m *Message) Info(str string, arg ...Any) *Message { return m.log(LOG_INFO, str, arg...) } -func (m *Message) Cost(arg ...interface{}) *Message { +func (m *Message) Cost(arg ...Any) *Message { list := []string{m.FormatCost(), m.join(arg...)} return m.log(LOG_COST, kit.Join(list, SP)) } -func (m *Message) Warn(err interface{}, arg ...interface{}) bool { +func (m *Message) Warn(err Any, arg ...Any) bool { switch err := err.(type) { case error: if err == io.EOF { @@ -101,7 +101,7 @@ func (m *Message) Warn(err interface{}, arg ...interface{}) bool { m.meta[MSG_RESULT] = kit.Simple(ErrWarn, arg[0], arg[1], SP, m.join(kit.Simple(arg[2:]...))) return true } -func (m *Message) Error(err bool, str string, arg ...interface{}) bool { +func (m *Message) Error(err bool, str string, arg ...Any) bool { if err { m.Echo(ErrWarn).Echo(str, arg...) m.log(LOG_ERROR, m.FormatStack(1, 100)) @@ -111,44 +111,44 @@ func (m *Message) Error(err bool, str string, arg ...interface{}) bool { } return false } -func (m *Message) Debug(str string, arg ...interface{}) { +func (m *Message) Debug(str string, arg ...Any) { if str == "" { str = m.FormatMeta() } m.log(LOG_DEBUG, str, arg...) } -func (m *Message) Logs(level string, arg ...interface{}) *Message { +func (m *Message) Logs(level string, arg ...Any) *Message { return m.log(level, m.join(arg...)) } -func (m *Message) Log_AUTH(arg ...interface{}) *Message { +func (m *Message) Log_AUTH(arg ...Any) *Message { return m.log(LOG_AUTH, m.join(arg...)) } -func (m *Message) Log_SEND(arg ...interface{}) *Message { +func (m *Message) Log_SEND(arg ...Any) *Message { return m.log(LOG_AUTH, m.join(arg...)) } -func (m *Message) Log_CREATE(arg ...interface{}) *Message { +func (m *Message) Log_CREATE(arg ...Any) *Message { return m.log(LOG_CREATE, m.join(arg...)) } -func (m *Message) Log_REMOVE(arg ...interface{}) *Message { +func (m *Message) Log_REMOVE(arg ...Any) *Message { return m.log(LOG_REMOVE, m.join(arg...)) } -func (m *Message) Log_INSERT(arg ...interface{}) *Message { +func (m *Message) Log_INSERT(arg ...Any) *Message { return m.log(LOG_INSERT, m.join(arg...)) } -func (m *Message) Log_DELETE(arg ...interface{}) *Message { +func (m *Message) Log_DELETE(arg ...Any) *Message { return m.log(LOG_DELETE, m.join(arg...)) } -func (m *Message) Log_MODIFY(arg ...interface{}) *Message { +func (m *Message) Log_MODIFY(arg ...Any) *Message { return m.log(LOG_MODIFY, m.join(arg...)) } -func (m *Message) Log_SELECT(arg ...interface{}) *Message { +func (m *Message) Log_SELECT(arg ...Any) *Message { return m.log(LOG_SELECT, m.join(arg...)) } -func (m *Message) Log_EXPORT(arg ...interface{}) *Message { +func (m *Message) Log_EXPORT(arg ...Any) *Message { return m.log(LOG_EXPORT, m.join(arg...)) } -func (m *Message) Log_IMPORT(arg ...interface{}) *Message { +func (m *Message) Log_IMPORT(arg ...Any) *Message { return m.log(LOG_IMPORT, m.join(arg...)) } diff --git a/meta.go b/meta.go index f8da0094..fd44b8be 100644 --- a/meta.go +++ b/meta.go @@ -102,25 +102,25 @@ func (m *Message) Cut(fields ...string) *Message { m.meta[MSG_APPEND] = kit.Split(kit.Join(fields)) return m } -func (m *Message) Push(key string, value interface{}, arg ...interface{}) *Message { +func (m *Message) Push(key string, value Any, arg ...Any) *Message { switch value := value.(type) { - case map[string]interface{}: + case Map: head := kit.Simple() if len(arg) > 0 { head = kit.Simple(arg[0]) } if len(head) == 0 { // 键值排序 - head = kit.SortedKey(kit.KeyValue(map[string]interface{}{}, "", value)) + head = kit.SortedKey(kit.KeyValue(Map{}, "", value)) } - var val map[string]interface{} + var val Map if len(arg) > 1 { - val, _ = arg[1].(map[string]interface{}) + val, _ = arg[1].(Map) } for _, k := range head { // 查找数据 - var v interface{} + var v Any switch k { case KEY, HASH: if key != "" && key != CACHE_DETAIL { @@ -174,7 +174,7 @@ func (m *Message) Push(key string, value interface{}, arg ...interface{}) *Messa } return m } -func (m *Message) Echo(str string, arg ...interface{}) *Message { +func (m *Message) Echo(str string, arg ...Any) *Message { if str == "" { return m } @@ -406,13 +406,13 @@ func (m *Message) SortStrR(key string) { m.Sort(key, "str_r") } func (m *Message) SortTime(key string) { m.Sort(key, "time") } func (m *Message) SortTimeR(key string) { m.Sort(key, "time_r") } -func (m *Message) Detail(arg ...interface{}) string { +func (m *Message) Detail(arg ...Any) string { return kit.Select("", m.meta[MSG_DETAIL], 0) } -func (m *Message) Detailv(arg ...interface{}) []string { +func (m *Message) Detailv(arg ...Any) []string { return m.meta[MSG_DETAIL] } -func (m *Message) Optionv(key string, arg ...interface{}) interface{} { +func (m *Message) Optionv(key string, arg ...Any) Any { if len(arg) > 0 { if kit.IndexOf(m.meta[MSG_OPTION], key) == -1 { // 写数据 m.meta[MSG_OPTION] = append(m.meta[MSG_OPTION], key) @@ -440,16 +440,16 @@ func (m *Message) Optionv(key string, arg ...interface{}) interface{} { } return nil } -func (m *Message) Option(key string, arg ...interface{}) string { +func (m *Message) Option(key string, arg ...Any) string { return kit.Select("", kit.Simple(m.Optionv(key, arg...)), 0) } -func (m *Message) Append(key string, arg ...interface{}) string { +func (m *Message) Append(key string, arg ...Any) string { if key == "" { return m.Append(m.Appendv(MSG_APPEND)[0]) } return kit.Select("", m.Appendv(key, arg...), 0) } -func (m *Message) Appendv(key string, arg ...interface{}) []string { +func (m *Message) Appendv(key string, arg ...Any) []string { if key == MSG_APPEND { if len(arg) > 0 { m.meta[MSG_APPEND] = kit.Simple(arg) @@ -479,13 +479,13 @@ func (m *Message) Appendv(key string, arg ...interface{}) []string { } return m.meta[key] } -func (m *Message) Resultv(arg ...interface{}) []string { +func (m *Message) Resultv(arg ...Any) []string { if len(arg) > 0 { m.meta[MSG_RESULT] = kit.Simple(arg...) } return m.meta[MSG_RESULT] } -func (m *Message) Result(arg ...interface{}) string { +func (m *Message) Result(arg ...Any) string { if len(arg) > 0 { switch v := arg[0].(type) { case int: diff --git a/misc.go b/misc.go index 056f9c0d..fed9713b 100644 --- a/misc.go +++ b/misc.go @@ -74,14 +74,14 @@ func (m *Message) Split(str string, arg ...string) *Message { // field sp nl func (m *Message) SplitIndex(str string, arg ...string) *Message { return m.Split(str, kit.Simple("index", arg)...) } -func (m *Message) PushDetail(value interface{}, arg ...string) *Message { +func (m *Message) PushDetail(value Any, arg ...string) *Message { return m.Push(CACHE_DETAIL, value, kit.Split(kit.Join(arg))) } -func (m *Message) PushRecord(value interface{}, arg ...string) *Message { +func (m *Message) PushRecord(value Any, arg ...string) *Message { return m.Push("", value, kit.Split(kit.Join(arg))) } -func (m *Message) OptionCB(key string, cb ...interface{}) interface{} { +func (m *Message) OptionCB(key string, cb ...Any) Any { if len(cb) > 0 { return m.Optionv(kit.Keycb(key), cb...) } @@ -148,10 +148,10 @@ func (m *Message) IsErr(arg ...string) bool { } func (m *Message) IsErrNotFound() bool { return m.Result(1) == ErrNotFound } -func (m *Message) cmd(arg ...interface{}) *Message { - opts := map[string]interface{}{} - args := []interface{}{} - var cbs interface{} +func (m *Message) cmd(arg ...Any) *Message { + opts := Map{} + args := []Any{} + var cbs Any // 解析参数 for _, v := range arg { @@ -160,7 +160,7 @@ func (m *Message) cmd(arg ...interface{}) *Message { opts[val.Name] = val.Value case *Option: opts[val.Name] = val.Value - case map[string]interface{}: + case Map: for k, v := range val { opts[k] = v } @@ -280,7 +280,7 @@ func (c *Context) _cmd(m *Message, cmd *Command, key string, sub string, h *Acti h.Hand(m, arg...) return m } -func SplitCmd(name string) (list []interface{}) { +func SplitCmd(name string) (list []Any) { const ( TEXT = "text" ARGS = "args" @@ -348,18 +348,18 @@ func SplitCmd(name string) (list []interface{}) { } return list } -func (m *Message) Design(action interface{}, help string, input ...interface{}) { +func (m *Message) Design(action Any, help string, input ...Any) { list := kit.List() for _, input := range input { switch input := input.(type) { case string: list = append(list, SplitCmd("action "+input)...) - case map[string]interface{}: + case Map: if kit.Format(input[TYPE]) != "" && kit.Format(input[NAME]) != "" { list = append(list, input) continue } - kit.Fetch(kit.KeyValue(nil, "", input), func(k string, v interface{}) { + kit.Fetch(kit.KeyValue(nil, "", input), func(k string, v Any) { list = append(list, kit.Dict(NAME, k, TYPE, "text", VALUE, v)) }) } @@ -372,7 +372,7 @@ func (m *Message) Design(action interface{}, help string, input ...interface{}) } } -func MergeAction(list ...interface{}) map[string]*Action { +func MergeAction(list ...Any) map[string]*Action { if len(list) == 0 { return nil } diff --git a/option.go b/option.go index 6d1dc3ee..ecb35c63 100644 --- a/option.go +++ b/option.go @@ -13,7 +13,7 @@ import ( type Option struct { Name string - Value interface{} + Value Any } func OptionHash(arg string) Option { return Option{HASH, arg} } @@ -43,10 +43,10 @@ func (m *Message) OptionLoad(file string) *Message { if f, e := os.Open(file); e == nil { defer f.Close() - var data interface{} + var data Any m.Assert(json.NewDecoder(f).Decode(&data)) - kit.Fetch(data, func(key string, value interface{}) { + kit.Fetch(data, func(key string, value Any) { m.Option(key, kit.Simple(value)) }) } @@ -121,7 +121,7 @@ func (m *Message) Upload(dir string) { m.Cmdy(SPIDE, DEV, SAVE, p, "GET", m.MergeURL2(path.Join("/share/cache", up[0]))) } } -func (m *Message) Action(arg ...interface{}) *Message { +func (m *Message) Action(arg ...Any) *Message { for i, v := range arg { switch v.(type) { case string: @@ -132,7 +132,7 @@ func (m *Message) Action(arg ...interface{}) *Message { m.Option(MSG_ACTION, kit.Format(arg)) return m } -func (m *Message) Status(arg ...interface{}) { +func (m *Message) Status(arg ...Any) { list := kit.List() args := kit.Simple(arg) for i := 0; i < len(args)-1; i += 2 { @@ -140,17 +140,17 @@ func (m *Message) Status(arg ...interface{}) { } m.Option(MSG_STATUS, kit.Format(list)) } -func (m *Message) StatusTime(arg ...interface{}) { +func (m *Message) StatusTime(arg ...Any) { m.Status(TIME, m.Time(), arg, kit.MDB_COST, m.FormatCost()) } -func (m *Message) StatusTimeCount(arg ...interface{}) { +func (m *Message) StatusTimeCount(arg ...Any) { m.Status(TIME, m.Time(), kit.MDB_COUNT, kit.Split(m.FormatSize())[0], arg, kit.MDB_COST, m.FormatCost()) } -func (m *Message) StatusTimeCountTotal(arg ...interface{}) { +func (m *Message) StatusTimeCountTotal(arg ...Any) { m.Status(TIME, m.Time(), kit.MDB_COUNT, kit.Split(m.FormatSize())[0], kit.MDB_TOTAL, arg, kit.MDB_COST, m.FormatCost()) } -func (m *Message) ToastProcess(arg ...interface{}) func() { +func (m *Message) ToastProcess(arg ...Any) func() { if len(arg) == 0 { arg = kit.List("", "-1") } @@ -160,10 +160,10 @@ func (m *Message) ToastProcess(arg ...interface{}) func() { m.Toast(PROCESS, arg...) return func() { m.Toast(SUCCESS) } } -func (m *Message) ToastRestart(arg ...interface{}) { m.Toast(RESTART, arg...) } -func (m *Message) ToastFailure(arg ...interface{}) { m.Toast(FAILURE, arg...) } -func (m *Message) ToastSuccess(arg ...interface{}) { m.Toast(SUCCESS, arg...) } -func (m *Message) Toast(text string, arg ...interface{}) { // [title [duration [progress]]] +func (m *Message) ToastRestart(arg ...Any) { m.Toast(RESTART, arg...) } +func (m *Message) ToastFailure(arg ...Any) { m.Toast(FAILURE, arg...) } +func (m *Message) ToastSuccess(arg ...Any) { m.Toast(SUCCESS, arg...) } +func (m *Message) Toast(text string, arg ...Any) { // [title [duration [progress]]] if len(arg) > 1 { switch val := arg[1].(type) { case string: @@ -175,26 +175,26 @@ func (m *Message) Toast(text string, arg ...interface{}) { // [title [duration [ m.PushNoticeToast("", text, arg) } -func (m *Message) PushNotice(arg ...interface{}) { +func (m *Message) PushNotice(arg ...Any) { if m.Option(MSG_USERPOD) == "" { m.Cmd(SPACE, m.Option(MSG_DAEMON), arg) } else { m.Cmd("web.spide", OPS, m.MergeURL2("/share/toast/"), kit.Format(kit.Dict(POD, m.Option(MSG_DAEMON), "cmds", kit.Simple(arg...)))) } } -func (m *Message) PushNoticeGrow(arg ...interface{}) { +func (m *Message) PushNoticeGrow(arg ...Any) { m.PushNotice(kit.List("grow", arg)...) } -func (m *Message) PushNoticeToast(arg ...interface{}) { +func (m *Message) PushNoticeToast(arg ...Any) { m.PushNotice(kit.List("toast", arg)...) } -func (m *Message) PushRefresh(arg ...interface{}) { +func (m *Message) PushRefresh(arg ...Any) { m.PushNotice(kit.List("refresh")...) } -func (m *Message) Toast3s(text string, arg ...interface{}) { +func (m *Message) Toast3s(text string, arg ...Any) { m.Toast(text, kit.List(kit.Select("", arg, 0), kit.Select("3s", arg, 1))...) } -func (m *Message) Toast30s(text string, arg ...interface{}) { +func (m *Message) Toast30s(text string, arg ...Any) { m.Toast(text, kit.List(kit.Select("", arg, 0), kit.Select("30s", arg, 1))...) } func (m *Message) GoToast(title string, cb func(toast func(string, int, int))) { @@ -210,20 +210,20 @@ func (m *Message) GoToast(title string, cb func(toast func(string, int, int))) { }) } -func (m *Message) Process(action string, arg ...interface{}) { +func (m *Message) Process(action string, arg ...Any) { m.Option(MSG_PROCESS, action) m.Option("_arg", arg...) } -func (m *Message) ProcessLocation(arg ...interface{}) { +func (m *Message) ProcessLocation(arg ...Any) { m.Process(PROCESS_LOCATION, arg...) } -func (m *Message) ProcessReplace(arg ...interface{}) { +func (m *Message) ProcessReplace(arg ...Any) { m.Process(PROCESS_REPLACE, arg...) } -func (m *Message) ProcessHistory(arg ...interface{}) { +func (m *Message) ProcessHistory(arg ...Any) { m.Process(PROCESS_HISTORY, arg...) } -func (m *Message) ProcessRewrite(arg ...interface{}) { +func (m *Message) ProcessRewrite(arg ...Any) { m.Process(PROCESS_REWRITE, arg...) } func (m *Message) ProcessRefresh(delay string) { @@ -236,7 +236,7 @@ func (m *Message) ProcessRefresh3ms() { m.ProcessRefresh("3ms") } func (m *Message) ProcessRefresh30ms() { m.ProcessRefresh("30ms") } func (m *Message) ProcessRefresh300ms() { m.ProcessRefresh("300ms") } func (m *Message) ProcessRefresh3s() { m.ProcessRefresh("3s") } -func (m *Message) ProcessDisplay(arg ...interface{}) { +func (m *Message) ProcessDisplay(arg ...Any) { m.Process(PROCESS_DISPLAY) m.Option(MSG_DISPLAY, arg...) } @@ -257,7 +257,7 @@ func (m *Message) ProcessCommandOpt(arg []string, args ...string) { } m.Push("opt", kit.Format(m.OptionSimple(args...))) } -func (m *Message) ProcessField(arg ...interface{}) { +func (m *Message) ProcessField(arg ...Any) { m.Process(PROCESS_FIELD) m.Option(FIELD_PREFIX, arg...) } @@ -270,19 +270,19 @@ func (m *Message) ProcessBack() { m.Process(PROCESS_BACK) } func (m *Message) OptionUserWeb() *url.URL { return kit.ParseURL(m.Option(MSG_USERWEB)) } -func (m *Message) MergeURL2(url string, arg ...interface{}) string { +func (m *Message) MergeURL2(url string, arg ...Any) string { return kit.MergeURL2(m.Option(MSG_USERWEB), url, arg...) } -func (m *Message) MergeLink(url string, arg ...interface{}) string { +func (m *Message) MergeLink(url string, arg ...Any) string { return strings.Split(m.MergeURL2(url, arg...), "?")[0] } -func (m *Message) MergePodURL(url string, arg ...interface{}) string { +func (m *Message) MergePodURL(url string, arg ...Any) string { return kit.MergeURL(m.MergeLink(path.Join("/chat/pod/", m.Option(MSG_USERPOD), url)), arg...) } -func (m *Message) MergePod(pod string, arg ...interface{}) string { +func (m *Message) MergePod(pod string, arg ...Any) string { return kit.MergePOD(kit.Select(Info.Domain, m.Option(MSG_USERWEB)), pod, arg...) } -func (m *Message) MergeCmd(cmd string, arg ...interface{}) string { +func (m *Message) MergeCmd(cmd string, arg ...Any) string { if cmd == "" { cmd = m.PrefixKey() } @@ -291,7 +291,7 @@ func (m *Message) MergeCmd(cmd string, arg ...interface{}) string { } return kit.MergeURL2(kit.Select(Info.Domain, m.Option(MSG_USERWEB)), path.Join("cmd", cmd), arg...) } -func (m *Message) MergeWebsite(web string, arg ...interface{}) string { +func (m *Message) MergeWebsite(web string, arg ...Any) string { if m.Option(MSG_USERPOD) == "" { return kit.MergeURL2(kit.Select(Info.Domain, m.Option(MSG_USERWEB)), path.Join("/chat/website", web)) } diff --git a/render.go b/render.go index ac488df2..ecb97aa2 100644 --- a/render.go +++ b/render.go @@ -7,10 +7,10 @@ import ( kit "shylinux.com/x/toolkits" ) -func AddRender(key string, render func(*Message, string, ...interface{}) string) { +func AddRender(key string, render func(*Message, string, ...Any) string) { Info.render[key] = render } -func Render(m *Message, cmd string, args ...interface{}) string { +func Render(m *Message, cmd string, args ...Any) string { if render, ok := Info.render[cmd]; ok { return render(m, cmd, args...) } @@ -48,7 +48,7 @@ func Render(m *Message, cmd string, args ...interface{}) string { return "" } -func (m *Message) Render(cmd string, args ...interface{}) *Message { +func (m *Message) Render(cmd string, args ...Any) *Message { m.Optionv(MSG_OUTPUT, cmd) m.Optionv(MSG_ARGS, args) @@ -63,16 +63,16 @@ func (m *Message) Render(cmd string, args ...interface{}) *Message { } return m } -func (m *Message) RenderResult(args ...interface{}) *Message { +func (m *Message) RenderResult(args ...Any) *Message { return m.Render(RENDER_RESULT, args...) } -func (m *Message) RenderTemplate(args ...interface{}) *Message { +func (m *Message) RenderTemplate(args ...Any) *Message { return m.Render(RENDER_TEMPLATE, args...) } -func (m *Message) RenderRedirect(args ...interface{}) *Message { +func (m *Message) RenderRedirect(args ...Any) *Message { return m.Render(RENDER_REDIRECT, args...) } -func (m *Message) RenderDownload(args ...interface{}) *Message { +func (m *Message) RenderDownload(args ...Any) *Message { return m.Render(RENDER_DOWNLOAD, args...) } func (m *Message) RenderWebsite(pod string, dir string, arg ...string) *Message { @@ -81,7 +81,7 @@ func (m *Message) RenderWebsite(pod string, dir string, arg ...string) *Message func (m *Message) RenderIndex(serve, repos string, file ...string) *Message { return m.RenderDownload(path.Join(m.Conf(serve, kit.Keym(repos, "path")), kit.Select(m.Conf(serve, kit.Keym(repos, INDEX)), path.Join(file...)))) } -func (m *Message) RenderCmd(index string, args ...interface{}) { +func (m *Message) RenderCmd(index string, args ...Any) { list := index if index != "" { msg := m.Cmd(COMMAND, index) @@ -110,12 +110,12 @@ func (m *Message) IsCliUA() bool { } return false } -func (m *Message) PushAnchor(arg ...interface{}) { // [name] link +func (m *Message) PushAnchor(arg ...Any) { // [name] link if !m.IsCliUA() { m.Push(LINK, Render(m, RENDER_ANCHOR, arg...)) } } -func (m *Message) PushButton(arg ...interface{}) { // name... +func (m *Message) PushButton(arg ...Any) { // name... if !m.IsCliUA() { if m.FieldsIsDetail() { for i, k := range m.meta[KEY] { @@ -155,13 +155,13 @@ func (m *Message) PushIFrame(key, src string, arg ...string) { // key src [size] m.Push(key, Render(m, RENDER_IFRAME, src, arg)) } } -func (m *Message) PushDownload(key string, arg ...interface{}) { // [name] file +func (m *Message) PushDownload(key string, arg ...Any) { // [name] file if !m.IsCliUA() { m.Push(key, Render(m, RENDER_DOWNLOAD, arg...)) } } -func (m *Message) PushAction(list ...interface{}) *Message { +func (m *Message) PushAction(list ...Any) *Message { if len(m.meta[MSG_APPEND]) == 0 { return m } @@ -169,7 +169,7 @@ func (m *Message) PushAction(list ...interface{}) *Message { m.PushButton(list...) }) } -func (m *Message) PushSearch(args ...interface{}) { +func (m *Message) PushSearch(args ...Any) { data := kit.Dict(args...) for _, k := range kit.Split(m.OptionFields()) { switch k { @@ -207,10 +207,10 @@ func (m *Message) PushPodCmd(cmd string, arg ...string) { }) } -func (m *Message) EchoAnchor(arg ...interface{}) *Message { // [name] link +func (m *Message) EchoAnchor(arg ...Any) *Message { // [name] link return m.Echo(Render(m, RENDER_ANCHOR, arg...)) } -func (m *Message) EchoButton(arg ...interface{}) *Message { // name... +func (m *Message) EchoButton(arg ...Any) *Message { // name... return m.Echo(Render(m, RENDER_BUTTON, arg...)) } func (m *Message) EchoScript(arg ...string) *Message { // [type] text... @@ -228,24 +228,24 @@ func (m *Message) EchoVideos(src string, arg ...string) *Message { // src [size] func (m *Message) EchoIFrame(src string, arg ...string) *Message { // src [size] return m.Echo(Render(m, RENDER_IFRAME, src, arg)) } -func (m *Message) EchoDownload(arg ...interface{}) *Message { // [name] file +func (m *Message) EchoDownload(arg ...Any) *Message { // [name] file return m.Echo(Render(m, RENDER_DOWNLOAD, arg...)) } -func (m *Message) DisplayBase(file string, arg ...interface{}) *Message { +func (m *Message) DisplayBase(file string, arg ...Any) *Message { if !strings.Contains(file, PT) { file += ".js" } m.Option(MSG_DISPLAY, kit.MergeURL(DisplayBase(file)[DISPLAY], arg...)) return m } -func (m *Message) DisplayStory(file string, arg ...interface{}) *Message { +func (m *Message) DisplayStory(file string, arg ...Any) *Message { if !strings.HasPrefix(file, HTTP) && !strings.HasPrefix(file, PS) { file = path.Join(PLUGIN_STORY, file) } return m.DisplayBase(file, arg...) } -func (m *Message) DisplayLocal(file string, arg ...interface{}) *Message { +func (m *Message) DisplayLocal(file string, arg ...Any) *Message { if file == "" { file = path.Join(kit.PathName(2), kit.Keys(kit.FileName(2), JS)) } @@ -254,11 +254,11 @@ func (m *Message) DisplayLocal(file string, arg ...interface{}) *Message { } return m.DisplayBase(file, arg...) } -func (m *Message) Display(file string, arg ...interface{}) *Message { +func (m *Message) Display(file string, arg ...Any) *Message { m.Option(MSG_DISPLAY, kit.MergeURL(DisplayRequire(2, file)[DISPLAY], arg...)) return m } -func (m *Message) DisplayStoryJSON(arg ...interface{}) *Message { +func (m *Message) DisplayStoryJSON(arg ...Any) *Message { return m.DisplayStory("json", arg...) } diff --git a/type.go b/type.go index 4f443573..fe04a63e 100644 --- a/type.go +++ b/type.go @@ -13,6 +13,8 @@ import ( kit "shylinux.com/x/toolkits" ) +type Any = interface{} +type Map = map[string]interface{} type ActionHandler func(m *Message, arg ...string) type CommandHandler func(m *Message, c *Context, key string, arg ...string) @@ -24,21 +26,21 @@ type Cache struct { type Config struct { Name string Help string - Value interface{} + Value Any } type Action struct { Name string Help string Hand ActionHandler - List []interface{} + List []Any } type Command struct { Name string Help string Action map[string]*Action - Meta map[string]interface{} + Meta Map Hand CommandHandler - List []interface{} + List []Any } type Server interface { Spawn(m *Message, c *Context, arg ...string) Server @@ -69,7 +71,7 @@ type Context struct { func (c *Context) ID() int32 { return atomic.AddInt32(&c.id, 1) } -func (c *Context) Cap(key string, arg ...interface{}) string { +func (c *Context) Cap(key string, arg ...Any) string { if len(arg) > 0 { c.Caches[key].Value = kit.Format(arg[0]) } @@ -266,7 +268,7 @@ type Message struct { Hand bool meta map[string][]string - data map[string]interface{} + data Map message *Message root *Message @@ -284,7 +286,7 @@ type Message struct { I io.Reader } -func (m *Message) Time(args ...interface{}) string { // [duration] [format [args...]] +func (m *Message) Time(args ...Any) string { // [duration] [format [args...]] t := m.time if len(args) > 0 { switch arg := args[0].(type) { @@ -311,10 +313,10 @@ func (m *Message) Target() *Context { func (m *Message) Source() *Context { return m.source } -func (m *Message) Spawn(arg ...interface{}) *Message { +func (m *Message) Spawn(arg ...Any) *Message { msg := &Message{ time: time.Now(), code: int(m.target.root.ID()), - meta: map[string][]string{}, data: map[string]interface{}{}, + meta: map[string][]string{}, data: Map{}, message: m, root: m.root, source: m.target, target: m.target, _cmd: m._cmd, _key: m._key, @@ -327,7 +329,7 @@ func (m *Message) Spawn(arg ...interface{}) *Message { json.Unmarshal(val, &msg.meta) case Option: msg.Option(val.Name, val.Value) - case map[string]interface{}: + case Map: for k, v := range val { msg.Option(k, v) } @@ -351,7 +353,7 @@ func (m *Message) Start(key string, arg ...string) *Message { m.Search(key+PT, func(p *Context, s *Context) { s.Start(m.Spawn(s), arg...) }) return m } -func (m *Message) Travel(cb interface{}) *Message { +func (m *Message) Travel(cb Any) *Message { list := []*Context{m.root.target} for i := 0; i < len(list); i++ { switch cb := cb.(type) { @@ -381,7 +383,7 @@ func (m *Message) Travel(cb interface{}) *Message { } return m } -func (m *Message) Search(key string, cb interface{}) *Message { +func (m *Message) Search(key string, cb Any) *Message { if key == "" { return m } @@ -474,23 +476,23 @@ func (m *Message) Search(key string, cb interface{}) *Message { return m } -func (m *Message) Cmd(arg ...interface{}) *Message { +func (m *Message) Cmd(arg ...Any) *Message { return m.cmd(arg...) } -func (m *Message) Cmds(arg ...interface{}) *Message { +func (m *Message) Cmds(arg ...Any) *Message { return m.Go(func() { m.cmd(arg...) }) } -func (m *Message) Cmdx(arg ...interface{}) string { +func (m *Message) Cmdx(arg ...Any) string { res := kit.Select("", m.cmd(arg...).meta[MSG_RESULT], 0) return kit.Select("", res, res != ErrWarn) } -func (m *Message) Cmdy(arg ...interface{}) *Message { +func (m *Message) Cmdy(arg ...Any) *Message { return m.Copy(m.cmd(arg...)) } func (m *Message) Confi(key string, sub string) int { return kit.Int(m.Conf(key, sub)) } -func (m *Message) Confv(arg ...interface{}) (val interface{}) { // key sub val +func (m *Message) Confv(arg ...Any) (val Any) { // key sub val run := func(conf *Config) { if len(arg) == 1 { val = conf.Value @@ -517,24 +519,24 @@ func (m *Message) Confv(arg ...interface{}) (val interface{}) { // key sub val } return } -func (m *Message) Confm(key string, sub interface{}, cbs ...interface{}) map[string]interface{} { +func (m *Message) Confm(key string, sub Any, cbs ...Any) Map { val := m.Confv(key, sub) if len(cbs) > 0 { kit.Fetch(val, cbs[0]) } - value, _ := val.(map[string]interface{}) + value, _ := val.(Map) return value } -func (m *Message) Conf(arg ...interface{}) string { // key sub val +func (m *Message) Conf(arg ...Any) string { // key sub val return kit.Format(m.Confv(arg...)) } -func (m *Message) Capi(key string, val ...interface{}) int { +func (m *Message) Capi(key string, val ...Any) int { if len(val) > 0 { m.Cap(key, kit.Int(m.Cap(key))+kit.Int(val[0])) } return kit.Int(m.Cap(key)) } -func (m *Message) Capv(arg ...interface{}) interface{} { +func (m *Message) Capv(arg ...Any) Any { key := "" switch val := arg[0].(type) { case string: @@ -553,6 +555,6 @@ func (m *Message) Capv(arg ...interface{}) interface{} { } return nil } -func (m *Message) Cap(arg ...interface{}) string { +func (m *Message) Cap(arg ...Any) string { return kit.Format(m.Capv(arg...)) }