Skip to content

Commit

Permalink
chore: interface{} -> any
Browse files Browse the repository at this point in the history
  • Loading branch information
alecthomas committed Dec 28, 2024
1 parent 3625768 commit a32b94b
Show file tree
Hide file tree
Showing 16 changed files with 63 additions and 63 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,7 @@ normal validation.

## Modifying Kong's behaviour

Each Kong parser can be configured via functional options passed to `New(cli interface{}, options...Option)`.
Each Kong parser can be configured via functional options passed to `New(cli any, options...Option)`.

The full set of options can be found [here](https://godoc.org/github.com/alecthomas/kong#Option).

Expand Down Expand Up @@ -706,7 +706,7 @@ All builtin Go types (as well as a bunch of useful stdlib types like `time.Time`
1. `NamedMapper(string, Mapper)` and using the tag key `type:"<name>"`.
2. `KindMapper(reflect.Kind, Mapper)`.
3. `TypeMapper(reflect.Type, Mapper)`.
4. `ValueMapper(interface{}, Mapper)`, passing in a pointer to a field of the grammar.
4. `ValueMapper(any, Mapper)`, passing in a pointer to a field of the grammar.

### `ConfigureHelp(HelpOptions)` and `Help(HelpFunc)` - customising help

Expand Down
4 changes: 2 additions & 2 deletions build.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
// Plugins are dynamically embedded command-line structures.
//
// Each element in the Plugins list *must* be a pointer to a structure.
type Plugins []interface{}
type Plugins []any

func build(k *Kong, ast interface{}) (app *Application, err error) {
func build(k *Kong, ast any) (app *Application, err error) {
v := reflect.ValueOf(ast)
iv := reflect.Indirect(v)
if v.Kind() != reflect.Ptr || iv.Kind() != reflect.Struct {
Expand Down
6 changes: 3 additions & 3 deletions callbacks.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@ func (b bindings) String() string {
return "bindings{" + strings.Join(out, ", ") + "}"
}

func (b bindings) add(values ...interface{}) bindings {
func (b bindings) add(values ...any) bindings {
for _, v := range values {
v := v
b[reflect.TypeOf(v)] = func() (any, error) { return v, nil }
}
return b
}

func (b bindings) addTo(impl, iface interface{}) {
func (b bindings) addTo(impl, iface any) {
b[reflect.TypeOf(iface).Elem()] = func() (any, error) { return impl, nil }
}

func (b bindings) addProvider(provider interface{}) error {
func (b bindings) addProvider(provider any) error {
pv := reflect.ValueOf(provider)
t := pv.Type()
if t.Kind() != reflect.Func || t.NumOut() != 2 || t.Out(1) != reflect.TypeOf((*error)(nil)).Elem() {
Expand Down
2 changes: 1 addition & 1 deletion config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestConfigValidation(t *testing.T) {
assert.Error(t, err)
}

func makeConfig(t *testing.T, config interface{}) (path string, cleanup func()) {
func makeConfig(t *testing.T, config any) (path string, cleanup func()) {
t.Helper()
w, err := os.CreateTemp("", "")
assert.NoError(t, err)
Expand Down
18 changes: 9 additions & 9 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func Trace(k *Kong, args []string) (*Context, error) {
}

// Bind adds bindings to the Context.
func (c *Context) Bind(args ...interface{}) {
func (c *Context) Bind(args ...any) {
c.bindings.add(args...)
}

Expand All @@ -111,15 +111,15 @@ func (c *Context) Bind(args ...interface{}) {
// This will typically have to be called like so:
//
// BindTo(impl, (*MyInterface)(nil))
func (c *Context) BindTo(impl, iface interface{}) {
func (c *Context) BindTo(impl, iface any) {
c.bindings.addTo(impl, iface)
}

// BindToProvider allows binding of provider functions.
//
// This is useful when the Run() function of different commands require different values that may
// not all be initialisable from the main() function.
func (c *Context) BindToProvider(provider interface{}) error {
func (c *Context) BindToProvider(provider any) error {
return c.bindings.addProvider(provider)
}

Expand Down Expand Up @@ -306,7 +306,7 @@ func (c *Context) AddResolver(resolver Resolver) {
}

// FlagValue returns the set value of a flag if it was encountered and exists, or its default value.
func (c *Context) FlagValue(flag *Flag) interface{} {
func (c *Context) FlagValue(flag *Flag) any {
for _, trace := range c.Path {
if trace.Flag == flag {
v, ok := c.values[trace.Flag.Value]
Expand Down Expand Up @@ -572,7 +572,7 @@ func (c *Context) Resolve() error {
}

// Pick the last resolved value.
var selected interface{}
var selected any
for _, resolver := range resolvers {
s, err := resolver.Resolve(c, path, flag)
if err != nil {
Expand Down Expand Up @@ -757,7 +757,7 @@ func (e *unknownFlagError) Unwrap() error { return e.Cause }
func (e *unknownFlagError) Error() string { return e.Cause.Error() }

// Call an arbitrary function filling arguments with bound values.
func (c *Context) Call(fn any, binds ...interface{}) (out []interface{}, err error) {
func (c *Context) Call(fn any, binds ...any) (out []any, err error) {
fv := reflect.ValueOf(fn)
bindings := c.Kong.bindings.clone().add(binds...).add(c).merge(c.bindings)
return callAnyFunction(fv, bindings)
Expand All @@ -769,7 +769,7 @@ func (c *Context) Call(fn any, binds ...interface{}) (out []interface{}, err err
//
// Any passed values will be bindable to arguments of the target Run() method. Additionally,
// all parent nodes in the command structure will be bound.
func (c *Context) RunNode(node *Node, binds ...interface{}) (err error) {
func (c *Context) RunNode(node *Node, binds ...any) (err error) {
type targetMethod struct {
node *Node
method reflect.Value
Expand Down Expand Up @@ -815,7 +815,7 @@ func (c *Context) RunNode(node *Node, binds ...interface{}) (err error) {
//
// Any passed values will be bindable to arguments of the target Run() method. Additionally,
// all parent nodes in the command structure will be bound.
func (c *Context) Run(binds ...interface{}) (err error) {
func (c *Context) Run(binds ...any) (err error) {
node := c.Selected()
if node == nil {
if len(c.Path) == 0 {
Expand Down Expand Up @@ -1087,7 +1087,7 @@ func checkAndMissing(paths []*Path) error {
return nil
}

func findPotentialCandidates(needle string, haystack []string, format string, args ...interface{}) error {
func findPotentialCandidates(needle string, haystack []string, format string, args ...any) error {
if len(haystack) == 0 {
return fmt.Errorf(format, args...)
}
Expand Down
2 changes: 1 addition & 1 deletion defaults.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package kong

// ApplyDefaults if they are not already set.
func ApplyDefaults(target interface{}, options ...Option) error {
func ApplyDefaults(target any, options ...Option) error {
app, err := New(target, options...)
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion global.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
)

// Parse constructs a new parser and parses the default command-line.
func Parse(cli interface{}, options ...Option) *Context {
func Parse(cli any, options ...Option) *Context {
parser, err := New(cli, options...)
if err != nil {
panic(err)
Expand Down
2 changes: 1 addition & 1 deletion help.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ func newHelpWriter(ctx *Context, options HelpOptions) *helpWriter {
return w
}

func (h *helpWriter) Printf(format string, args ...interface{}) {
func (h *helpWriter) Printf(format string, args ...any) {
h.Print(fmt.Sprintf(format, args...))
}

Expand Down
16 changes: 8 additions & 8 deletions kong.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var (
callbackReturnSignature = reflect.TypeOf((*error)(nil)).Elem()
)

func failField(parent reflect.Value, field reflect.StructField, format string, args ...interface{}) error {
func failField(parent reflect.Value, field reflect.StructField, format string, args ...any) error {
name := parent.Type().Name()
if name == "" {
name = "<anonymous struct>"
Expand All @@ -24,7 +24,7 @@ func failField(parent reflect.Value, field reflect.StructField, format string, a
}

// Must creates a new Parser or panics if there is an error.
func Must(ast interface{}, options ...Option) *Kong {
func Must(ast any, options ...Option) *Kong {
k, err := New(ast, options...)
if err != nil {
panic(err)
Expand Down Expand Up @@ -76,7 +76,7 @@ type Kong struct {
// New creates a new Kong parser on grammar.
//
// See the README (https://github.com/alecthomas/kong) for usage instructions.
func New(grammar interface{}, options ...Option) (*Kong, error) {
func New(grammar any, options ...Option) (*Kong, error) {
k := &Kong{
Exit: os.Exit,
Stdout: os.Stdout,
Expand Down Expand Up @@ -401,7 +401,7 @@ func (k *Kong) applyHookToDefaultFlags(ctx *Context, node *Node, name string) er
})
}

func formatMultilineMessage(w io.Writer, leaders []string, format string, args ...interface{}) {
func formatMultilineMessage(w io.Writer, leaders []string, format string, args ...any) {
lines := strings.Split(strings.TrimRight(fmt.Sprintf(format, args...), "\n"), "\n")
leader := ""
for _, l := range leaders {
Expand All @@ -417,25 +417,25 @@ func formatMultilineMessage(w io.Writer, leaders []string, format string, args .
}

// Printf writes a message to Kong.Stdout with the application name prefixed.
func (k *Kong) Printf(format string, args ...interface{}) *Kong {
func (k *Kong) Printf(format string, args ...any) *Kong {
formatMultilineMessage(k.Stdout, []string{k.Model.Name}, format, args...)
return k
}

// Errorf writes a message to Kong.Stderr with the application name prefixed.
func (k *Kong) Errorf(format string, args ...interface{}) *Kong {
func (k *Kong) Errorf(format string, args ...any) *Kong {
formatMultilineMessage(k.Stderr, []string{k.Model.Name, "error"}, format, args...)
return k
}

// Fatalf writes a message to Kong.Stderr with the application name prefixed then exits with a non-zero status.
func (k *Kong) Fatalf(format string, args ...interface{}) {
func (k *Kong) Fatalf(format string, args ...any) {
k.Errorf(format, args...)
k.Exit(1)
}

// FatalIfErrorf terminates with an error message if err != nil.
func (k *Kong) FatalIfErrorf(err error, args ...interface{}) {
func (k *Kong) FatalIfErrorf(err error, args ...any) {
if err == nil {
return
}
Expand Down
8 changes: 4 additions & 4 deletions kong_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/alecthomas/kong"
)

func mustNew(t *testing.T, cli interface{}, options ...kong.Option) *kong.Kong {
func mustNew(t *testing.T, cli any, options ...kong.Option) *kong.Kong {
t.Helper()
options = append([]kong.Option{
kong.Name("test"),
Expand Down Expand Up @@ -1680,7 +1680,7 @@ func TestOptionReturnsErr(t *testing.T) {
func TestEnumValidation(t *testing.T) {
tests := []struct {
name string
cli interface{}
cli any
fail bool
}{
{
Expand Down Expand Up @@ -1954,7 +1954,7 @@ func TestVersionFlagShouldStillWork(t *testing.T) {
func TestSliceDecoderHelpfulErrorMsg(t *testing.T) {
tests := []struct {
name string
cli interface{}
cli any
args []string
err string
}{
Expand Down Expand Up @@ -2004,7 +2004,7 @@ func TestSliceDecoderHelpfulErrorMsg(t *testing.T) {
func TestMapDecoderHelpfulErrorMsg(t *testing.T) {
tests := []struct {
name string
cli interface{}
cli any
args []string
expected string
}{
Expand Down
12 changes: 6 additions & 6 deletions mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ func (r *Registry) RegisterType(typ reflect.Type, mapper Mapper) *Registry {
}

// RegisterValue registers a Mapper by pointer to the field value.
func (r *Registry) RegisterValue(ptr interface{}, mapper Mapper) *Registry {
func (r *Registry) RegisterValue(ptr any, mapper Mapper) *Registry {
key := reflect.ValueOf(ptr)
if key.Kind() != reflect.Ptr {
panic("expected a pointer")
Expand Down Expand Up @@ -473,7 +473,7 @@ func mapDecoder(r *Registry) MapperFunc {
case string:
childScanner = ScanAsType(t.Type, SplitEscaped(v, mapsep)...)

case []map[string]interface{}:
case []map[string]any:
for _, m := range v {
err := jsonTranscode(m, target.Addr().Interface())
if err != nil {
Expand All @@ -482,7 +482,7 @@ func mapDecoder(r *Registry) MapperFunc {
}
return nil

case map[string]interface{}:
case map[string]any:
return jsonTranscode(v, target.Addr().Interface())

default:
Expand Down Expand Up @@ -548,11 +548,11 @@ func sliceDecoder(r *Registry) MapperFunc {
case string:
childScanner = ScanAsType(t.Type, SplitEscaped(v, sep)...)

case []interface{}:
case []any:
return jsonTranscode(v, target.Addr().Interface())

default:
v = []interface{}{v}
v = []any{v}
return jsonTranscode(v, target.Addr().Interface())
}
} else {
Expand Down Expand Up @@ -922,7 +922,7 @@ func (f *FileContentFlag) Decode(ctx *DecodeContext) error { //nolint: revive
return nil
}

func jsonTranscode(in, out interface{}) error {
func jsonTranscode(in, out any) error {
data, err := json.Marshal(in)
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion model.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (n *Node) Leaf() bool {
// Find a command/argument/flag by pointer to its field.
//
// Returns nil if not found. Panics if ptr is not a pointer.
func (n *Node) Find(ptr interface{}) *Node {
func (n *Node) Find(ptr any) *Node {
key := reflect.ValueOf(ptr)
if key.Kind() != reflect.Ptr {
panic("expected a pointer")
Expand Down
14 changes: 7 additions & 7 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,15 @@ type dynamicCommand struct {
help string
group string
tags []string
cmd interface{}
cmd any
}

// DynamicCommand registers a dynamically constructed command with the root of the CLI.
//
// This is useful for command-line structures that are extensible via user-provided plugins.
//
// "tags" is a list of extra tag strings to parse, in the form <key>:"<value>".
func DynamicCommand(name, help, group string, cmd interface{}, tags ...string) Option {
func DynamicCommand(name, help, group string, cmd any, tags ...string) Option {
return OptionFunc(func(k *Kong) error {
if run := getMethod(reflect.Indirect(reflect.ValueOf(cmd)), "Run"); !run.IsValid() {
return fmt.Errorf("kong: DynamicCommand %q must be a type with a 'Run' method; got %T", name, cmd)
Expand Down Expand Up @@ -156,7 +156,7 @@ func KindMapper(kind reflect.Kind, mapper Mapper) Option {
}

// ValueMapper registers a mapper to a field value.
func ValueMapper(ptr interface{}, mapper Mapper) Option {
func ValueMapper(ptr any, mapper Mapper) Option {
return OptionFunc(func(k *Kong) error {
k.registry.RegisterValue(ptr, mapper)
return nil
Expand Down Expand Up @@ -191,7 +191,7 @@ func Writers(stdout, stderr io.Writer) Option {
// AfterApply(...) error
//
// Called before validation/assignment, and immediately after validation/assignment, respectively.
func Bind(args ...interface{}) Option {
func Bind(args ...any) Option {
return OptionFunc(func(k *Kong) error {
k.bindings.add(args...)
return nil
Expand All @@ -201,7 +201,7 @@ func Bind(args ...interface{}) Option {
// BindTo allows binding of implementations to interfaces.
//
// BindTo(impl, (*iface)(nil))
func BindTo(impl, iface interface{}) Option {
func BindTo(impl, iface any) Option {
return OptionFunc(func(k *Kong) error {
k.bindings.addTo(impl, iface)
return nil
Expand All @@ -212,11 +212,11 @@ func BindTo(impl, iface interface{}) Option {
//
// The provider function must have the signature:
//
// func() (interface{}, error)
// func() (any, error)
//
// This is useful when the Run() function of different commands require different values that may
// not all be initialisable from the main() function.
func BindToProvider(provider interface{}) Option {
func BindToProvider(provider any) Option {
return OptionFunc(func(k *Kong) error {
return k.bindings.addProvider(provider)
})
Expand Down
Loading

0 comments on commit a32b94b

Please sign in to comment.