Skip to content

Commit

Permalink
chore: gcli - replace the interface{} to go1.18 any
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Sep 4, 2022
1 parent de96285 commit 93988d8
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 47 deletions.
6 changes: 0 additions & 6 deletions any.go

This file was deleted.

4 changes: 2 additions & 2 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ func (app *App) On(name string, handler HookFunc) {
}

// Fire hook on the app
func (app *App) Fire(event string, data interface{}) bool {
func (app *App) Fire(event string, data any) bool {
Debugf("trigger the application event: <green>%s</>", event)

return app.core.Fire(event, app, data)
Expand Down Expand Up @@ -655,7 +655,7 @@ func (app *App) showApplicationHelp() {

// cmdHelpTemplate = color.ReplaceTag(cmdHelpTemplate)
// render help text template
s := helper.RenderText(AppHelpTemplate, map[string]interface{}{
s := helper.RenderText(AppHelpTemplate, map[string]any{
"Cs": app.commands,
"GOpts": app.gFlags.String(),
// app version
Expand Down
28 changes: 14 additions & 14 deletions base.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,29 +81,29 @@ func (c core) innerHelpVars() map[string]string {
}
}

// simple map[string]interface{} struct
// simple map[string]any struct
// TODO use structs.DataStore
type mapData struct {
data map[string]interface{}
data map[string]any
}

// Data get all
func (md *mapData) Data() map[string]interface{} {
func (md *mapData) Data() map[string]any {
return md.data
}

// SetData set all data
func (md *mapData) SetData(data map[string]interface{}) {
func (md *mapData) SetData(data map[string]any) {
md.data = data
}

// Value get from data
func (md *mapData) Value(key string) interface{} {
func (md *mapData) Value(key string) any {
return md.data[key]
}

// GetVal get from data
func (md *mapData) GetVal(key string) interface{} {
func (md *mapData) GetVal(key string) any {
return md.data[key]
}

Expand All @@ -118,9 +118,9 @@ func (md *mapData) IntValue(key string) int {
}

// SetValue to data
func (md *mapData) SetValue(key string, val interface{}) {
func (md *mapData) SetValue(key string, val any) {
if md.data == nil {
md.data = make(map[string]interface{})
md.data = make(map[string]any)
}
md.data[key] = val
}
Expand All @@ -138,15 +138,15 @@ func (md *mapData) ClearData() {
//
// func arguments:
//
// in app, like: func(app *App, data ...interface{})
// in cmd, like: func(cmd *Command, data ...interface{})
// in app, like: func(app *App, data ...any)
// in cmd, like: func(cmd *Command, data ...any)
//
// type HookFunc func(obj interface{}, data interface{})
// type HookFunc func(obj any, data any)
//
// return:
// - True go on handle. default is True
// - False stop goon handle.
type HookFunc func(data ...interface{}) (stop bool)
type HookFunc func(data ...any) (stop bool)

// HookCtx struct
type HookCtx struct {
Expand All @@ -155,7 +155,7 @@ type HookCtx struct {
Cmd *Command

name string
data map[string]interface{}
data map[string]any
}

// Name of event
Expand Down Expand Up @@ -188,7 +188,7 @@ func (h *Hooks) AddOn(name string, handler HookFunc) {
}

// Fire event by name, allow with event data
func (h *Hooks) Fire(event string, data ...interface{}) (stop bool) {
func (h *Hooks) Fire(event string, data ...any) (stop bool) {
if handler, ok := h.hooks[event]; ok {
return handler(data...)
}
Expand Down
14 changes: 7 additions & 7 deletions base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ func TestApp_Hooks_EvtAppInit(t *testing.T) {
buf.Reset()

cli := newNotExitApp()
cli.On(gcli.EvtAppInit, func(data ...interface{}) bool {
cli.On(gcli.EvtAppInit, func(data ...any) bool {
buf.WriteString("trigger " + gcli.EvtAppInit)
return false
})
cli.Add(simpleCmd)
assert.Eq(t, "trigger "+gcli.EvtAppInit, buf.String())

buf.Reset()
cli.On(gcli.EvtGOptionsParsed, func(data ...interface{}) bool {
cli.On(gcli.EvtGOptionsParsed, func(data ...any) bool {
buf.WriteString("trigger " + gcli.EvtGOptionsParsed + ", args:" + fmt.Sprintf("%v", data[1]))
return false
})
Expand All @@ -45,7 +45,7 @@ func TestApp_Hooks_EvtCmdInit(t *testing.T) {
buf.Reset()

cli := newNotExitApp()
cli.On(gcli.EvtCmdInit, func(data ...interface{}) (stop bool) {
cli.On(gcli.EvtCmdInit, func(data ...any) (stop bool) {
buf.WriteString(gcli.EvtCmdInit)
buf.WriteString(":")

Expand All @@ -70,7 +70,7 @@ func TestCommand_Hooks_EvtCmdOptParsed(t *testing.T) {
Desc: "desc",
Config: func(c *gcli.Command) {
buf.WriteString("run config;")
c.On(gcli.EvtCmdOptParsed, func(data ...interface{}) (stop bool) {
c.On(gcli.EvtCmdOptParsed, func(data ...any) (stop bool) {
dump.P(data[1])
buf.WriteString(gcli.EvtCmdOptParsed)
return
Expand All @@ -90,7 +90,7 @@ func TestApp_On_CmdNotFound(t *testing.T) {
cli.Add(simpleCmd)

fmt.Println("--------- will print command tips ----------")
cli.On(gcli.EvtCmdNotFound, func(data ...interface{}) bool {
cli.On(gcli.EvtCmdNotFound, func(data ...any) bool {
buf.WriteString("trigger: " + gcli.EvtCmdNotFound)
buf.WriteString("; command: " + fmt.Sprint(data[1]))
return false
Expand All @@ -101,7 +101,7 @@ func TestApp_On_CmdNotFound(t *testing.T) {
buf.Reset()

fmt.Println("--------- dont print command tips ----------")
cli.On(gcli.EvtCmdNotFound, func(data ...interface{}) bool {
cli.On(gcli.EvtCmdNotFound, func(data ...any) bool {
buf.WriteString("trigger: " + gcli.EvtCmdNotFound)
buf.WriteString("; command: " + fmt.Sprint(data[1]))
return true
Expand All @@ -120,7 +120,7 @@ func TestApp_On_CmdNotFound_redirect(t *testing.T) {
cli.Add(simpleCmd)

fmt.Println("--------- redirect to run another command ----------")
cli.On(gcli.EvtCmdNotFound, func(data ...interface{}) bool {
cli.On(gcli.EvtCmdNotFound, func(data ...any) bool {
buf.WriteString("trigger:" + gcli.EvtCmdNotFound)
buf.WriteString(" - command:" + fmt.Sprint(data[1]))
buf.WriteString("; redirect:simple - ")
Expand Down
10 changes: 5 additions & 5 deletions cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,7 @@ func (c *Command) ShowHelp() {
c.Help = strings.Join([]string{strings.TrimSpace(c.Help), "\n"}, "")
}

vars := map[string]interface{}{
vars := map[string]any{
"Cmd": c,
"Subs": c.commands,
// global options
Expand Down Expand Up @@ -734,7 +734,7 @@ func (c *Command) goodName() string {
}

// Fire event handler by name
func (c *Command) Fire(event string, data interface{}) (stop bool) {
func (c *Command) Fire(event string, data any) (stop bool) {
Debugf("cmd: %s - trigger the event: <mga>%s</>", c.Name, event)

return c.core.Fire(event, c, data)
Expand Down Expand Up @@ -782,15 +782,15 @@ func (c *Command) PathNames() []string {
}

// Errorf format message and add error to the command
func (c *Command) Errorf(format string, v ...interface{}) error {
func (c *Command) Errorf(format string, v ...any) error {
return fmt.Errorf(format, v...)
}

// NewErr format message and add error to the command
func (c *Command) NewErr(msg string) error { return errors.New(msg) }

// NewErrf format message and add error to the command
func (c *Command) NewErrf(format string, v ...interface{}) error {
func (c *Command) NewErrf(format string, v ...any) error {
return fmt.Errorf(format, v...)
}

Expand Down Expand Up @@ -821,6 +821,6 @@ func (c *Command) HelpDesc() (desc string) {
}

// Logf print log message
// func (c *Command) Logf(level uint, format string, v ...interface{}) {
// func (c *Command) Logf(level uint, format string, v ...any) {
// Logf(level, format, v...)
// }
2 changes: 1 addition & 1 deletion gargs.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ func (a *Argument) goodArgument() string {
}

// GetValue get value by custom handler func
func (a *Argument) GetValue() interface{} {
func (a *Argument) GetValue() any {
val := a.Value.Val()
if a.Handler != nil {
return a.Handler(val)
Expand Down
4 changes: 2 additions & 2 deletions gargs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,14 @@ func TestArgument_GetValue(t *testing.T) {

// custom handler
assert.NoErr(t, arg.SetValue("a-b-c"))
arg.Handler = func(value interface{}) interface{} {
arg.Handler = func(value any) any {
str := value.(string)
return strings.SplitN(str, "-", 2)
}
assert.Eq(t, []string{"a", "b-c"}, arg.GetValue())
}

var str2int = func(val interface{}) (interface{}, error) {
var str2int = func(val any) (any, error) {
return strconv.Atoi(val.(string))
}

Expand Down
4 changes: 2 additions & 2 deletions gcli.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ func IsDebugMode() bool { return gOpts.verbose >= VerbDebug }

// Commander interface
type Commander interface {
Value(string) (interface{}, bool)
SetValue(string, interface{})
Value(string) (any, bool)
SetValue(string, any)
}

/*************************************************************************
Expand Down
File renamed without changes.
16 changes: 8 additions & 8 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ var level2color = map[VerbLevel]color.Color{
}

// Debugf print log message
func Debugf(format string, v ...interface{}) {
func Debugf(format string, v ...any) {
logf(VerbDebug, format, v...)
}

// Logf print log message
func Logf(level VerbLevel, format string, v ...interface{}) {
func Logf(level VerbLevel, format string, v ...any) {
logf(level, format, v...)
}

// print log message
func logf(level VerbLevel, format string, v ...interface{}) {
func logf(level VerbLevel, format string, v ...any) {
if gOpts.verbose < level {
return
}
Expand All @@ -46,7 +46,7 @@ func logf(level VerbLevel, format string, v ...interface{}) {
color.Printf("GCli: [%s] [<gray>%s</>] %s \n", name, logAt, fmt.Sprintf(format, v...))
}

func defaultErrHandler(data ...interface{}) (stop bool) {
func defaultErrHandler(data ...any) (stop bool) {
if len(data) == 2 && data[1] != nil {
if err, ok := data[1].(error); ok {
color.Error.Tips(err.Error())
Expand Down Expand Up @@ -82,21 +82,21 @@ func name2verbLevel(name string) VerbLevel {
*************************************************************/

// Print messages
func Print(args ...interface{}) {
func Print(args ...any) {
color.Print(args...)
}

// Println messages
func Println(args ...interface{}) {
func Println(args ...any) {
color.Println(args...)
}

// Printf messages
func Printf(format string, args ...interface{}) {
func Printf(format string, args ...any) {
color.Printf(format, args...)
}

func panicf(format string, v ...interface{}) {
func panicf(format string, v ...any) {
panic(fmt.Sprintf("GCli: "+format, v...))
}

Expand Down

0 comments on commit 93988d8

Please sign in to comment.