Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions core/bootstrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestInterruptError(t *testing.T) {
Namespace: "test",
Resource: "interrupt",
Verb: "error",
ArgsType: reflect.TypeOf(args.RawArgs{}),
ArgsType: reflect.TypeFor[args.RawArgs](),
Run: func(_ context.Context, _ any) (i any, e error) {
return nil, &interactive.InterruptError{}
},
Expand All @@ -38,7 +38,7 @@ func TestInterruptError(t *testing.T) {
Namespace: "test",
Resource: "code",
Verb: "error",
ArgsType: reflect.TypeOf(args.RawArgs{}),
ArgsType: reflect.TypeFor[args.RawArgs](),
Run: func(_ context.Context, _ any) (i any, e error) {
return nil, &core.CliError{Code: 99}
},
Expand All @@ -55,7 +55,7 @@ func TestInterruptError(t *testing.T) {
Namespace: "test",
Resource: "empty",
Verb: "error",
ArgsType: reflect.TypeOf(args.RawArgs{}),
ArgsType: reflect.TypeFor[args.RawArgs](),
Run: func(_ context.Context, _ any) (i any, e error) {
return nil, &core.CliError{Code: 99, Empty: true}
},
Expand All @@ -75,7 +75,7 @@ func TestInterruptError(t *testing.T) {
Namespace: "test",
Resource: "empty",
Verb: "error",
ArgsType: reflect.TypeOf(args.RawArgs{}),
ArgsType: reflect.TypeFor[args.RawArgs](),
Run: func(_ context.Context, _ any) (i any, e error) {
return nil, &core.CliError{Code: 99, Empty: true}
},
Expand All @@ -95,7 +95,7 @@ func TestInterruptError(t *testing.T) {
Namespace: "test",
Resource: "empty",
Verb: "success",
ArgsType: reflect.TypeOf(args.RawArgs{}),
ArgsType: reflect.TypeFor[args.RawArgs](),
Run: func(_ context.Context, _ any) (i any, e error) {
return &core.SuccessResult{
Empty: true,
Expand All @@ -118,7 +118,7 @@ func TestInterruptError(t *testing.T) {
Namespace: "test",
Resource: "empty",
Verb: "success",
ArgsType: reflect.TypeOf(args.RawArgs{}),
ArgsType: reflect.TypeFor[args.RawArgs](),
Run: func(_ context.Context, _ any) (i any, e error) {
return &core.SuccessResult{
Empty: true,
Expand All @@ -141,7 +141,7 @@ func TestInterruptError(t *testing.T) {
Namespace: "test",
Resource: "empty",
Verb: "success",
ArgsType: reflect.TypeOf(args.RawArgs{}),
ArgsType: reflect.TypeFor[args.RawArgs](),
Run: func(_ context.Context, _ any) (i any, e error) {
return []int(nil), nil
},
Expand Down
2 changes: 1 addition & 1 deletion core/build_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (

var fakeCommand = &core.Command{
Namespace: "plop",
ArgsType: reflect.TypeOf(args.RawArgs{}),
ArgsType: reflect.TypeFor[args.RawArgs](),
AllowAnonymousClient: true,
Run: func(_ context.Context, _ any) (i any, e error) {
return &core.SuccessResult{}, nil
Expand Down
2 changes: 1 addition & 1 deletion core/checks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestCheckAPIKey(t *testing.T) {
&core.Command{
Namespace: "test",
ArgSpecs: core.ArgSpecs{},
ArgsType: reflect.TypeOf(testType{}),
ArgsType: reflect.TypeFor[testType](),
Run: func(ctx context.Context, _ any) (i any, e error) {
// Test command reload the client so the profile used is the edited one
return "", core.ReloadClient(ctx)
Expand Down
2 changes: 1 addition & 1 deletion core/cobra_usage_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func Test_buildUsageArgs(t *testing.T) {
[additional-volumes.{index}.name] Additional volume name`

got := core.BuildUsageArgs(t.Context(), &core.Command{
ArgsType: reflect.TypeOf(instanceListServerArgs{}),
ArgsType: reflect.TypeFor[instanceListServerArgs](),
ArgSpecs: core.ArgSpecs{
{
Name: "name",
Expand Down
10 changes: 5 additions & 5 deletions core/cobra_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func testGetCommands() *core.Commands {
},
},
AllowAnonymousClient: true,
ArgsType: reflect.TypeOf(testType{}),
ArgsType: reflect.TypeFor[testType](),
Run: func(_ context.Context, _ any) (i any, e error) {
return "", nil
},
Expand All @@ -55,7 +55,7 @@ func testGetCommands() *core.Commands {
},
},
AllowAnonymousClient: true,
ArgsType: reflect.TypeOf(testType{}),
ArgsType: reflect.TypeFor[testType](),
Run: func(_ context.Context, argsI any) (i any, e error) {
return argsI, nil
},
Expand All @@ -74,15 +74,15 @@ func testGetCommands() *core.Commands {
},
AcceptMultiplePositionalArgs: true,
AllowAnonymousClient: true,
ArgsType: reflect.TypeOf(testAcceptMultiPositionalArgsType{}),
ArgsType: reflect.TypeFor[testAcceptMultiPositionalArgsType](),
Run: func(_ context.Context, argsI any) (i any, e error) {
return argsI, nil
},
},
&core.Command{
Namespace: "test",
Resource: "raw-args",
ArgsType: reflect.TypeOf(args.RawArgs{}),
ArgsType: reflect.TypeFor[args.RawArgs](),
AllowAnonymousClient: true,
Run: func(_ context.Context, argsI any) (i any, e error) {
rawArgs := *argsI.(*args.RawArgs)
Expand All @@ -93,7 +93,7 @@ func testGetCommands() *core.Commands {
&core.Command{
Namespace: "test",
Resource: "date",
ArgsType: reflect.TypeOf(testDate{}),
ArgsType: reflect.TypeFor[testDate](),
AllowAnonymousClient: true,
Run: func(_ context.Context, argsI any) (i any, e error) {
a := argsI.(*testDate)
Expand Down
8 changes: 4 additions & 4 deletions core/human/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@ func Marshal(data any, opt *MarshalOpt) (string, error) {
return marshalerFunc(rValue.Interface(), opt)

// Handle special well known interface
case rType.Implements(reflect.TypeOf((*Marshaler)(nil)).Elem()):
case rType.Implements(reflect.TypeFor[Marshaler]()):
Comment thread
remyleone marked this conversation as resolved.
return rValue.Interface().(Marshaler).MarshalHuman()

// Handle errors
case rType.Implements(reflect.TypeOf((*error)(nil)).Elem()):
case rType.Implements(reflect.TypeFor[error]()):
Comment thread
remyleone marked this conversation as resolved.
return terminal.Style(Capitalize(rValue.Interface().(error).Error()), color.FgRed), nil

// Handle stringers
case rType.Implements(reflect.TypeOf((*fmt.Stringer)(nil)).Elem()):
case rType.Implements(reflect.TypeFor[fmt.Stringer]()):
Comment thread
remyleone marked this conversation as resolved.
return rValue.Interface().(fmt.Stringer).String(), nil

// If data is a pointer dereference an call Marshal again
Expand Down Expand Up @@ -143,7 +143,7 @@ func marshalStruct(value reflect.Value, opt *MarshalOpt) (string, error) {
return [][]string{{strings.Join(keys, "."), str}}, err

// If data is a stringers
case rType.Implements(reflect.TypeOf((*fmt.Stringer)(nil)).Elem()):
case rType.Implements(reflect.TypeFor[fmt.Stringer]()):
return [][]string{
{strings.Join(keys, "."), value.Interface().(fmt.Stringer).String()},
}, nil
Expand Down
38 changes: 19 additions & 19 deletions core/human/marshal_func.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ type MarshalerFunc func(any, *MarshalOpt) (string, error)
var marshalerFuncs sync.Map

func init() {
marshalerFuncs.Store(reflect.TypeOf(int(0)), defaultMarshalerFunc)
marshalerFuncs.Store(reflect.TypeOf(int32(0)), defaultMarshalerFunc)
marshalerFuncs.Store(reflect.TypeOf(int64(0)), defaultMarshalerFunc)
marshalerFuncs.Store(reflect.TypeOf(uint32(0)), defaultMarshalerFunc)
marshalerFuncs.Store(reflect.TypeOf(uint64(0)), defaultMarshalerFunc)
marshalerFuncs.Store(reflect.TypeOf(string("")), defaultMarshalerFunc)
marshalerFuncs.Store(reflect.TypeFor[int](), defaultMarshalerFunc)
marshalerFuncs.Store(reflect.TypeFor[int32](), defaultMarshalerFunc)
marshalerFuncs.Store(reflect.TypeFor[int64](), defaultMarshalerFunc)
marshalerFuncs.Store(reflect.TypeFor[uint32](), defaultMarshalerFunc)
marshalerFuncs.Store(reflect.TypeFor[uint64](), defaultMarshalerFunc)
marshalerFuncs.Store(reflect.TypeFor[string](), defaultMarshalerFunc)
marshalerFuncs.Store(
reflect.TypeOf(bool(false)),
Comment thread
remyleone marked this conversation as resolved.
func(i any, _ *MarshalOpt) (string, error) {
Expand All @@ -40,13 +40,13 @@ func init() {
},
)
marshalerFuncs.Store(
reflect.TypeOf(time.Time{}),
reflect.TypeFor[time.Time](),
func(i any, _ *MarshalOpt) (string, error) {
return humanize.Time(i.(time.Time)), nil
},
)
marshalerFuncs.Store(
reflect.TypeOf(&time.Time{}),
reflect.TypeFor[*time.Time](),
func(i any, _ *MarshalOpt) (string, error) {
t := i.(*time.Time)
if t == nil {
Expand All @@ -57,7 +57,7 @@ func init() {
},
)
marshalerFuncs.Store(
reflect.TypeOf(scw.Size(0)),
reflect.TypeFor[scw.Size](),
func(i any, _ *MarshalOpt) (string, error) {
size := uint64(i.(scw.Size))

Expand All @@ -69,7 +69,7 @@ func init() {
},
)
marshalerFuncs.Store(
reflect.TypeOf(new(scw.Size(0))),
reflect.TypeFor[*scw.Size](),
func(i any, _ *MarshalOpt) (string, error) {
size := uint64(*i.(*scw.Size))

Expand All @@ -81,7 +81,7 @@ func init() {
},
)
marshalerFuncs.Store(
reflect.TypeOf([]scw.Size{}),
reflect.TypeFor[[]scw.Size](),
func(i any, _ *MarshalOpt) (string, error) {
sizes := i.([]scw.Size)
strs := []string(nil)
Expand All @@ -97,19 +97,19 @@ func init() {
},
)
marshalerFuncs.Store(
reflect.TypeOf(net.IP{}),
reflect.TypeFor[net.IP](),
func(i any, _ *MarshalOpt) (string, error) {
return fmt.Sprintf("%v", i.(net.IP)), nil
},
)
marshalerFuncs.Store(
reflect.TypeOf([]net.IP{}),
reflect.TypeFor[[]net.IP](),
func(i any, _ *MarshalOpt) (string, error) {
return fmt.Sprintf("%v", i), nil
},
)
marshalerFuncs.Store(
reflect.TypeOf(scw.IPNet{}),
reflect.TypeFor[scw.IPNet](),
func(i any, _ *MarshalOpt) (string, error) {
v := i.(scw.IPNet)
str := v.String()
Expand All @@ -121,15 +121,15 @@ func init() {
},
)
marshalerFuncs.Store(
reflect.TypeOf(version.Version{}),
reflect.TypeFor[version.Version](),
func(i any, _ *MarshalOpt) (string, error) {
v := i.(version.Version)

return v.String(), nil
},
)
marshalerFuncs.Store(
reflect.TypeOf(scw.Duration{}),
reflect.TypeFor[scw.Duration](),
func(i any, _ *MarshalOpt) (string, error) {
v := i.(scw.Duration)
const (
Expand Down Expand Up @@ -232,9 +232,9 @@ func isMarshalable(t reflect.Type) bool {

return (t.Kind() != reflect.Struct && t.Kind() != reflect.Map && t.Kind() != reflect.Pointer) ||
hasMarshalerFunc ||
t.Implements(reflect.TypeOf((*Marshaler)(nil)).Elem()) ||
t.Implements(reflect.TypeOf((*error)(nil)).Elem()) ||
t.Implements(reflect.TypeOf((*fmt.Stringer)(nil)).Elem()) ||
t.Implements(reflect.TypeFor[Marshaler]()) ||
t.Implements(reflect.TypeFor[error]()) ||
t.Implements(reflect.TypeFor[fmt.Stringer]()) ||
(t.Kind() == reflect.Pointer && isMarshalable(t.Elem()))
}

Expand Down
8 changes: 4 additions & 4 deletions core/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ func Test_ValidateDeprecated(t *testing.T) {
t.Run("Deprecated", core.Test(&core.TestConfig{
Commands: core.NewCommands(&core.Command{
Namespace: "plop",
ArgsType: reflect.TypeOf(args.RawArgs{}),
ArgsType: reflect.TypeFor[args.RawArgs](),
AllowAnonymousClient: true,
Run: func(_ context.Context, _ any) (i any, e error) {
return &core.SuccessResult{}, nil
Expand Down Expand Up @@ -738,7 +738,7 @@ func Test_ValidateOneOf(t *testing.T) {
t.Run("Simple one-of validation check", core.Test(&core.TestConfig{
Commands: core.NewCommands(&core.Command{
Namespace: "oneof",
ArgsType: reflect.TypeOf(args.RawArgs{}),
ArgsType: reflect.TypeFor[args.RawArgs](),
AllowAnonymousClient: true,
Run: func(_ context.Context, _ any) (i any, e error) {
return &core.SuccessResult{}, nil
Expand All @@ -763,7 +763,7 @@ func Test_ValidateOneOf(t *testing.T) {
core.Test(&core.TestConfig{
Commands: core.NewCommands(&core.Command{
Namespace: "oneof",
ArgsType: reflect.TypeOf(args.RawArgs{}),
ArgsType: reflect.TypeFor[args.RawArgs](),
AllowAnonymousClient: true,
Run: func(_ context.Context, _ any) (i any, e error) {
return &core.SuccessResult{}, nil
Expand Down Expand Up @@ -792,7 +792,7 @@ func Test_ValidateOneOf(t *testing.T) {
core.Test(&core.TestConfig{
Commands: core.NewCommands(&core.Command{
Namespace: "oneof",
ArgsType: reflect.TypeOf(args.RawArgs{}),
ArgsType: reflect.TypeFor[args.RawArgs](),
AllowAnonymousClient: true,
Run: func(_ context.Context, _ any) (i any, e error) {
return &core.SuccessResult{}, nil
Expand Down
18 changes: 9 additions & 9 deletions internal/args/args_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,47 +193,47 @@ func TestGetArgType(t *testing.T) {
}

t.Run("Simple", run(&TestCase{
ArgType: reflect.TypeOf(&Basic{}),
ArgType: reflect.TypeFor[*Basic](),
Name: "string",
ExpectedKind: reflect.String,
}))
t.Run("Simple int", run(&TestCase{
ArgType: reflect.TypeOf(&Basic{}),
ArgType: reflect.TypeFor[*Basic](),
Name: "int-64",
ExpectedKind: reflect.Int64,
}))
t.Run("Ptr", run(&TestCase{
ArgType: reflect.TypeOf(&Basic{}),
ArgType: reflect.TypeFor[*Basic](),
Name: "string-ptr",
ExpectedKind: reflect.String,
}))
t.Run("simple slice", run(&TestCase{
ArgType: reflect.TypeOf(&Slice{}),
ArgType: reflect.TypeFor[*Slice](),
Name: "strings.{index}",
ExpectedKind: reflect.String,
}))
t.Run("simple slice ptr", run(&TestCase{
ArgType: reflect.TypeOf(&Slice{}),
ArgType: reflect.TypeFor[*Slice](),
Name: "slice-ptr.{index}",
ExpectedKind: reflect.String,
}))
t.Run("nested simple", run(&TestCase{
ArgType: reflect.TypeOf(&Nested{}),
ArgType: reflect.TypeFor[*Nested](),
Name: "basic.string",
ExpectedKind: reflect.String,
}))
t.Run("nested empty", run(&TestCase{
ArgType: reflect.TypeOf(&Nested{}),
ArgType: reflect.TypeFor[*Nested](),
Name: "empty",
ExpectedKind: reflect.Struct,
}))
t.Run("merge simple", run(&TestCase{
ArgType: reflect.TypeOf(&Merge{}),
ArgType: reflect.TypeFor[*Merge](),
Name: "merge1",
ExpectedKind: reflect.String,
}))
t.Run("merge simple all", run(&TestCase{
ArgType: reflect.TypeOf(&Merge{}),
ArgType: reflect.TypeFor[*Merge](),
Name: "all",
ExpectedKind: reflect.String,
}))
Expand Down
4 changes: 2 additions & 2 deletions internal/args/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ type Marshaler interface {
type MarshalFunc func(src any) (string, error)

var marshalFuncs = map[reflect.Type]MarshalFunc{
reflect.TypeOf((*scw.Size)(nil)).Elem(): func(src any) (s string, e error) {
reflect.TypeFor[scw.Size](): func(src any) (s string, e error) {
v := src.(*scw.Size)
value := humanize.Bytes(uint64(*v))
value = strings.ReplaceAll(value, " ", "")

return value, nil
},
reflect.TypeOf((*time.Time)(nil)).Elem(): func(src any) (string, error) {
reflect.TypeFor[time.Time](): func(src any) (string, error) {
v := src.(*time.Time)

return v.Format(time.RFC3339), nil
Expand Down
Loading
Loading