Skip to content

Commit a32b94b

Browse files
committed
chore: interface{} -> any
1 parent 3625768 commit a32b94b

16 files changed

+63
-63
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,7 @@ normal validation.
648648

649649
## Modifying Kong's behaviour
650650

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

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

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

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

build.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import (
99
// Plugins are dynamically embedded command-line structures.
1010
//
1111
// Each element in the Plugins list *must* be a pointer to a structure.
12-
type Plugins []interface{}
12+
type Plugins []any
1313

14-
func build(k *Kong, ast interface{}) (app *Application, err error) {
14+
func build(k *Kong, ast any) (app *Application, err error) {
1515
v := reflect.ValueOf(ast)
1616
iv := reflect.Indirect(v)
1717
if v.Kind() != reflect.Ptr || iv.Kind() != reflect.Struct {

callbacks.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,19 @@ func (b bindings) String() string {
1919
return "bindings{" + strings.Join(out, ", ") + "}"
2020
}
2121

22-
func (b bindings) add(values ...interface{}) bindings {
22+
func (b bindings) add(values ...any) bindings {
2323
for _, v := range values {
2424
v := v
2525
b[reflect.TypeOf(v)] = func() (any, error) { return v, nil }
2626
}
2727
return b
2828
}
2929

30-
func (b bindings) addTo(impl, iface interface{}) {
30+
func (b bindings) addTo(impl, iface any) {
3131
b[reflect.TypeOf(iface).Elem()] = func() (any, error) { return impl, nil }
3232
}
3333

34-
func (b bindings) addProvider(provider interface{}) error {
34+
func (b bindings) addProvider(provider any) error {
3535
pv := reflect.ValueOf(provider)
3636
t := pv.Type()
3737
if t.Kind() != reflect.Func || t.NumOut() != 2 || t.Out(1) != reflect.TypeOf((*error)(nil)).Elem() {

config_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func TestConfigValidation(t *testing.T) {
4242
assert.Error(t, err)
4343
}
4444

45-
func makeConfig(t *testing.T, config interface{}) (path string, cleanup func()) {
45+
func makeConfig(t *testing.T, config any) (path string, cleanup func()) {
4646
t.Helper()
4747
w, err := os.CreateTemp("", "")
4848
assert.NoError(t, err)

context.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ func Trace(k *Kong, args []string) (*Context, error) {
102102
}
103103

104104
// Bind adds bindings to the Context.
105-
func (c *Context) Bind(args ...interface{}) {
105+
func (c *Context) Bind(args ...any) {
106106
c.bindings.add(args...)
107107
}
108108

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

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

@@ -306,7 +306,7 @@ func (c *Context) AddResolver(resolver Resolver) {
306306
}
307307

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

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

759759
// Call an arbitrary function filling arguments with bound values.
760-
func (c *Context) Call(fn any, binds ...interface{}) (out []interface{}, err error) {
760+
func (c *Context) Call(fn any, binds ...any) (out []any, err error) {
761761
fv := reflect.ValueOf(fn)
762762
bindings := c.Kong.bindings.clone().add(binds...).add(c).merge(c.bindings)
763763
return callAnyFunction(fv, bindings)
@@ -769,7 +769,7 @@ func (c *Context) Call(fn any, binds ...interface{}) (out []interface{}, err err
769769
//
770770
// Any passed values will be bindable to arguments of the target Run() method. Additionally,
771771
// all parent nodes in the command structure will be bound.
772-
func (c *Context) RunNode(node *Node, binds ...interface{}) (err error) {
772+
func (c *Context) RunNode(node *Node, binds ...any) (err error) {
773773
type targetMethod struct {
774774
node *Node
775775
method reflect.Value
@@ -815,7 +815,7 @@ func (c *Context) RunNode(node *Node, binds ...interface{}) (err error) {
815815
//
816816
// Any passed values will be bindable to arguments of the target Run() method. Additionally,
817817
// all parent nodes in the command structure will be bound.
818-
func (c *Context) Run(binds ...interface{}) (err error) {
818+
func (c *Context) Run(binds ...any) (err error) {
819819
node := c.Selected()
820820
if node == nil {
821821
if len(c.Path) == 0 {
@@ -1087,7 +1087,7 @@ func checkAndMissing(paths []*Path) error {
10871087
return nil
10881088
}
10891089

1090-
func findPotentialCandidates(needle string, haystack []string, format string, args ...interface{}) error {
1090+
func findPotentialCandidates(needle string, haystack []string, format string, args ...any) error {
10911091
if len(haystack) == 0 {
10921092
return fmt.Errorf(format, args...)
10931093
}

defaults.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package kong
22

33
// ApplyDefaults if they are not already set.
4-
func ApplyDefaults(target interface{}, options ...Option) error {
4+
func ApplyDefaults(target any, options ...Option) error {
55
app, err := New(target, options...)
66
if err != nil {
77
return err

global.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
)
66

77
// Parse constructs a new parser and parses the default command-line.
8-
func Parse(cli interface{}, options ...Option) *Context {
8+
func Parse(cli any, options ...Option) *Context {
99
parser, err := New(cli, options...)
1010
if err != nil {
1111
panic(err)

help.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ func newHelpWriter(ctx *Context, options HelpOptions) *helpWriter {
386386
return w
387387
}
388388

389-
func (h *helpWriter) Printf(format string, args ...interface{}) {
389+
func (h *helpWriter) Printf(format string, args ...any) {
390390
h.Print(fmt.Sprintf(format, args...))
391391
}
392392

kong.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ var (
1515
callbackReturnSignature = reflect.TypeOf((*error)(nil)).Elem()
1616
)
1717

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

2626
// Must creates a new Parser or panics if there is an error.
27-
func Must(ast interface{}, options ...Option) *Kong {
27+
func Must(ast any, options ...Option) *Kong {
2828
k, err := New(ast, options...)
2929
if err != nil {
3030
panic(err)
@@ -76,7 +76,7 @@ type Kong struct {
7676
// New creates a new Kong parser on grammar.
7777
//
7878
// See the README (https://github.com/alecthomas/kong) for usage instructions.
79-
func New(grammar interface{}, options ...Option) (*Kong, error) {
79+
func New(grammar any, options ...Option) (*Kong, error) {
8080
k := &Kong{
8181
Exit: os.Exit,
8282
Stdout: os.Stdout,
@@ -401,7 +401,7 @@ func (k *Kong) applyHookToDefaultFlags(ctx *Context, node *Node, name string) er
401401
})
402402
}
403403

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

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

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

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

437437
// FatalIfErrorf terminates with an error message if err != nil.
438-
func (k *Kong) FatalIfErrorf(err error, args ...interface{}) {
438+
func (k *Kong) FatalIfErrorf(err error, args ...any) {
439439
if err == nil {
440440
return
441441
}

kong_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
"github.com/alecthomas/kong"
1515
)
1616

17-
func mustNew(t *testing.T, cli interface{}, options ...kong.Option) *kong.Kong {
17+
func mustNew(t *testing.T, cli any, options ...kong.Option) *kong.Kong {
1818
t.Helper()
1919
options = append([]kong.Option{
2020
kong.Name("test"),
@@ -1680,7 +1680,7 @@ func TestOptionReturnsErr(t *testing.T) {
16801680
func TestEnumValidation(t *testing.T) {
16811681
tests := []struct {
16821682
name string
1683-
cli interface{}
1683+
cli any
16841684
fail bool
16851685
}{
16861686
{
@@ -1954,7 +1954,7 @@ func TestVersionFlagShouldStillWork(t *testing.T) {
19541954
func TestSliceDecoderHelpfulErrorMsg(t *testing.T) {
19551955
tests := []struct {
19561956
name string
1957-
cli interface{}
1957+
cli any
19581958
args []string
19591959
err string
19601960
}{
@@ -2004,7 +2004,7 @@ func TestSliceDecoderHelpfulErrorMsg(t *testing.T) {
20042004
func TestMapDecoderHelpfulErrorMsg(t *testing.T) {
20052005
tests := []struct {
20062006
name string
2007-
cli interface{}
2007+
cli any
20082008
args []string
20092009
expected string
20102010
}{

mapper.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ func (r *Registry) RegisterType(typ reflect.Type, mapper Mapper) *Registry {
251251
}
252252

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

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

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

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

551-
case []interface{}:
551+
case []any:
552552
return jsonTranscode(v, target.Addr().Interface())
553553

554554
default:
555-
v = []interface{}{v}
555+
v = []any{v}
556556
return jsonTranscode(v, target.Addr().Interface())
557557
}
558558
} else {
@@ -922,7 +922,7 @@ func (f *FileContentFlag) Decode(ctx *DecodeContext) error { //nolint: revive
922922
return nil
923923
}
924924

925-
func jsonTranscode(in, out interface{}) error {
925+
func jsonTranscode(in, out any) error {
926926
data, err := json.Marshal(in)
927927
if err != nil {
928928
return err

model.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func (n *Node) Leaf() bool {
6969
// Find a command/argument/flag by pointer to its field.
7070
//
7171
// Returns nil if not found. Panics if ptr is not a pointer.
72-
func (n *Node) Find(ptr interface{}) *Node {
72+
func (n *Node) Find(ptr any) *Node {
7373
key := reflect.ValueOf(ptr)
7474
if key.Kind() != reflect.Ptr {
7575
panic("expected a pointer")

options.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,15 @@ type dynamicCommand struct {
7979
help string
8080
group string
8181
tags []string
82-
cmd interface{}
82+
cmd any
8383
}
8484

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

158158
// ValueMapper registers a mapper to a field value.
159-
func ValueMapper(ptr interface{}, mapper Mapper) Option {
159+
func ValueMapper(ptr any, mapper Mapper) Option {
160160
return OptionFunc(func(k *Kong) error {
161161
k.registry.RegisterValue(ptr, mapper)
162162
return nil
@@ -191,7 +191,7 @@ func Writers(stdout, stderr io.Writer) Option {
191191
// AfterApply(...) error
192192
//
193193
// Called before validation/assignment, and immediately after validation/assignment, respectively.
194-
func Bind(args ...interface{}) Option {
194+
func Bind(args ...any) Option {
195195
return OptionFunc(func(k *Kong) error {
196196
k.bindings.add(args...)
197197
return nil
@@ -201,7 +201,7 @@ func Bind(args ...interface{}) Option {
201201
// BindTo allows binding of implementations to interfaces.
202202
//
203203
// BindTo(impl, (*iface)(nil))
204-
func BindTo(impl, iface interface{}) Option {
204+
func BindTo(impl, iface any) Option {
205205
return OptionFunc(func(k *Kong) error {
206206
k.bindings.addTo(impl, iface)
207207
return nil
@@ -212,11 +212,11 @@ func BindTo(impl, iface interface{}) Option {
212212
//
213213
// The provider function must have the signature:
214214
//
215-
// func() (interface{}, error)
215+
// func() (any, error)
216216
//
217217
// This is useful when the Run() function of different commands require different values that may
218218
// not all be initialisable from the main() function.
219-
func BindToProvider(provider interface{}) Option {
219+
func BindToProvider(provider any) Option {
220220
return OptionFunc(func(k *Kong) error {
221221
return k.bindings.addProvider(provider)
222222
})

0 commit comments

Comments
 (0)