fs: fix option types printing incorrectly for backend flags

Before this change backend types were printing incorrectly as the name
of the type, not what was defined by the Type() method.

This was not working due to not calling the Type() method. However
this needed to be defined on a non-pointer type due to the way the
options are handled.
This commit is contained in:
Nick Craig-Wood 2023-09-27 13:58:27 +01:00
parent b8591b230d
commit 3553cc4a5f
15 changed files with 73 additions and 26 deletions

View File

@ -9,8 +9,11 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
// Check it satisfies the interface // Check it satisfies the interfaces
var _ flagger = (*BwTimetable)(nil) var (
_ flagger = (*BwTimetable)(nil)
_ flaggerNP = BwTimetable{}
)
func TestBwTimetableSet(t *testing.T) { func TestBwTimetableSet(t *testing.T) {
for _, test := range []struct { for _, test := range []struct {

View File

@ -154,7 +154,7 @@ func (x *CountSuffix) Set(s string) error {
} }
// Type of the value // Type of the value
func (x *CountSuffix) Type() string { func (x CountSuffix) Type() string {
return "CountSuffix" return "CountSuffix"
} }

View File

@ -9,8 +9,11 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
// Check it satisfies the interface // Check it satisfies the interfaces
var _ flagger = (*CountSuffix)(nil) var (
_ flagger = (*CountSuffix)(nil)
_ flaggerNP = CountSuffix(0)
)
func TestCountSuffixString(t *testing.T) { func TestCountSuffixString(t *testing.T) {
for _, test := range []struct { for _, test := range []struct {

View File

@ -42,7 +42,7 @@ func (m *CutoffMode) Set(s string) error {
} }
// Type of the value // Type of the value
func (m *CutoffMode) Type() string { func (m CutoffMode) Type() string {
return "string" return "string"
} }

View File

@ -9,8 +9,11 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
// Check it satisfies the interface // Check it satisfies the interfaces
var _ flagger = (*CutoffMode)(nil) var (
_ flagger = (*CutoffMode)(nil)
_ flaggerNP = CutoffMode(0)
)
func TestCutoffModeString(t *testing.T) { func TestCutoffModeString(t *testing.T) {
for _, test := range []struct { for _, test := range []struct {

View File

@ -86,7 +86,7 @@ func (f *DumpFlags) Set(s string) error {
} }
// Type of the value // Type of the value
func (f *DumpFlags) Type() string { func (f DumpFlags) Type() string {
return "DumpFlags" return "DumpFlags"
} }

View File

@ -8,8 +8,11 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
// Check it satisfies the interface // Check it satisfies the interfaces
var _ flagger = (*DumpFlags)(nil) var (
_ flagger = (*DumpFlags)(nil)
_ flaggerNP = DumpFlags(0)
)
func TestDumpFlagsString(t *testing.T) { func TestDumpFlagsString(t *testing.T) {
assert.Equal(t, "", DumpFlags(0).String()) assert.Equal(t, "", DumpFlags(0).String())

View File

@ -65,8 +65,8 @@ func (l *LogLevel) Set(s string) error {
} }
// Type of the value // Type of the value
func (l *LogLevel) Type() string { func (l LogLevel) Type() string {
return "string" return "LogLevel"
} }
// UnmarshalJSON makes sure the value can be parsed as a string or integer in JSON // UnmarshalJSON makes sure the value can be parsed as a string or integer in JSON

View File

@ -10,9 +10,12 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
// Check it satisfies the interface // Check it satisfies the interfac(
var _ flagger = (*LogLevel)(nil) var (
var _ fmt.Stringer = LogValueItem{} _ flagger = (*LogLevel)(nil)
_ flaggerNP = LogLevel(0)
_ fmt.Stringer = LogValueItem{}
)
type withString struct{} type withString struct{}

View File

@ -11,8 +11,11 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
// Check it satisfies the interface // Check it satisfies the interfaces
var _ flagger = (*Duration)(nil) var (
_ flagger = (*Duration)(nil)
_ flaggerNP = Duration(0)
)
func TestParseDuration(t *testing.T) { func TestParseDuration(t *testing.T) {
now := time.Date(2020, 9, 5, 8, 15, 5, 250, time.UTC) now := time.Date(2020, 9, 5, 8, 15, 5, 250, time.UTC)

View File

@ -10,8 +10,11 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
// Check it satisfies the interface // Check it satisfies the interfaces
var _ flagger = (*Time)(nil) var (
_ flagger = (*Time)(nil)
_ flaggerNP = Time{}
)
func TestParseTime(t *testing.T) { func TestParseTime(t *testing.T) {
now := time.Date(2020, 9, 5, 8, 15, 5, 250, time.UTC) now := time.Date(2020, 9, 5, 8, 15, 5, 250, time.UTC)

View File

@ -207,9 +207,20 @@ func (o *Option) Set(s string) (err error) {
return nil return nil
} }
type typer interface {
Type() string
}
// Type of the value // Type of the value
func (o *Option) Type() string { func (o *Option) Type() string {
return reflect.TypeOf(o.GetValue()).Name() v := o.GetValue()
// Try to call Type method on non-pointer
if do, ok := v.(typer); ok {
return do.Type()
}
return reflect.TypeOf(v).Name()
} }
// FlagName for the option // FlagName for the option

View File

@ -192,7 +192,7 @@ func (x *SizeSuffix) Set(s string) error {
} }
// Type of the value // Type of the value
func (x *SizeSuffix) Type() string { func (x SizeSuffix) Type() string {
return "SizeSuffix" return "SizeSuffix"
} }

View File

@ -17,8 +17,20 @@ type flagger interface {
json.Unmarshaler json.Unmarshaler
} }
// Check it satisfies the interface // Interface which non-pointer flags must satisfy
var _ flagger = (*SizeSuffix)(nil) //
// These are from pflag.Value and need to be non-pointer due the the
// way the backend flags are inserted into the flags.
type flaggerNP interface {
String() string
Type() string
}
// Check it satisfies the interfaces
var (
_ flagger = (*SizeSuffix)(nil)
_ flaggerNP = SizeSuffix(0)
)
func TestSizeSuffixString(t *testing.T) { func TestSizeSuffixString(t *testing.T) {
for _, test := range []struct { for _, test := range []struct {

View File

@ -9,8 +9,11 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
// Check it satisfies the interface // Check it satisfies the interfaces
var _ flagger = (*Tristate)(nil) var (
_ flagger = (*Tristate)(nil)
_ flaggerNP = Tristate{}
)
func TestTristateString(t *testing.T) { func TestTristateString(t *testing.T) {
for _, test := range []struct { for _, test := range []struct {