Skip to content

Commit

Permalink
Merge pull request #1647 from dearchap/issue_1592
Browse files Browse the repository at this point in the history
Fix:(issue_1592) Add support for float64slice, uint, int, int64 for a…
  • Loading branch information
dearchap authored Jan 22, 2023
2 parents 52ff0c6 + 3f207ee commit 9d6c243
Show file tree
Hide file tree
Showing 7 changed files with 583 additions and 0 deletions.
88 changes: 88 additions & 0 deletions altsrc/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,37 @@ func (f *Int64SliceFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourc
return nil
}

// ApplyInputSourceValue applies a Float64Slice value if required
func (f *Float64SliceFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceContext) error {
if f.set == nil || cCtx.IsSet(f.Name) || isEnvVarSet(f.EnvVars) {
return nil
}
for _, name := range f.Float64SliceFlag.Names() {
if !isc.isSet(name) {
continue
}
value, err := isc.Float64Slice(name)
if err != nil {
return err
}
if value == nil {
continue
}
var sliceValue = *(cli.NewFloat64Slice(value...))
for _, n := range f.Names() {
underlyingFlag := f.set.Lookup(n)
if underlyingFlag == nil {
continue
}
underlyingFlag.Value = &sliceValue
}
if f.Destination != nil {
f.Destination.Set(sliceValue.Serialize())
}
}
return nil
}

// ApplyInputSourceValue applies a Bool value to the flagSet if required
func (f *BoolFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceContext) error {
if f.set == nil || cCtx.IsSet(f.Name) || isEnvVarSet(f.EnvVars) {
Expand Down Expand Up @@ -269,6 +300,63 @@ func (f *IntFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceContex
return nil
}

func (f *Int64Flag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceContext) error {
if f.set == nil || cCtx.IsSet(f.Name) || isEnvVarSet(f.EnvVars) {
return nil
}
for _, name := range f.Int64Flag.Names() {
if !isc.isSet(name) {
continue
}
value, err := isc.Int64(name)
if err != nil {
return err
}
for _, n := range f.Names() {
_ = f.set.Set(n, strconv.FormatInt(value, 10))
}
}
return nil
}

func (f *UintFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceContext) error {
if f.set == nil || cCtx.IsSet(f.Name) || isEnvVarSet(f.EnvVars) {
return nil
}
for _, name := range f.UintFlag.Names() {
if !isc.isSet(name) {
continue
}
value, err := isc.Uint(name)
if err != nil {
return err
}
for _, n := range f.Names() {
_ = f.set.Set(n, strconv.FormatUint(uint64(value), 10))
}
}
return nil
}

func (f *Uint64Flag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceContext) error {
if f.set == nil || cCtx.IsSet(f.Name) || isEnvVarSet(f.EnvVars) {
return nil
}
for _, name := range f.Uint64Flag.Names() {
if !isc.isSet(name) {
continue
}
value, err := isc.Uint64(name)
if err != nil {
return err
}
for _, n := range f.Names() {
_ = f.set.Set(n, strconv.FormatUint(value, 10))
}
}
return nil
}

// ApplyInputSourceValue applies a Duration value to the flagSet if required
func (f *DurationFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceContext) error {
if f.set == nil || cCtx.IsSet(f.Name) || isEnvVarSet(f.EnvVars) {
Expand Down
264 changes: 264 additions & 0 deletions altsrc/flag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,65 @@ func TestInt64SliceFlagApplyInputSourceValue(t *testing.T) {
refute(t, dest.Value(), []int64{1, 2})
}

func TestInt64SliceFlagApplyInputSourceValueNotSet(t *testing.T) {
dest := cli.NewInt64Slice()
tis := testApplyInputSource{
Flag: NewInt64SliceFlag(&cli.Int64SliceFlag{Name: "test", Destination: dest}),
FlagName: "test1",
MapValue: []interface{}{int64(1), int64(2)},
}
c := runTest(t, tis)
expect(t, c.Int64Slice("test"), []int64{})
expect(t, dest.Value(), []int64{})
}

func TestFloat64SliceFlagApplyInputSourceValue(t *testing.T) {
dest := cli.NewFloat64Slice()
tis := testApplyInputSource{
Flag: NewFloat64SliceFlag(&cli.Float64SliceFlag{Name: "test", Destination: dest}),
FlagName: "test",
MapValue: []interface{}{float64(1.0), float64(2.1)},
}
c := runTest(t, tis)
expect(t, c.Float64Slice("test"), []float64{1.0, 2.1})
expect(t, dest.Value(), []float64{1.0, 2.1})

// reset dest
dest = cli.NewFloat64Slice()
tis = testApplyInputSource{
Flag: NewFloat64SliceFlag(&cli.Float64SliceFlag{Name: "test", Destination: dest}),
FlagName: "test",
MapValue: []interface{}{float64(1.0), float64(2.1)},
}
c = runRacyTest(t, tis)
refute(t, c.IntSlice("test"), []int64{1, 2})
refute(t, dest.Value(), []int64{1, 2})
}

func TestFloat64SliceFlagApplyInputSourceValueNotSet(t *testing.T) {
dest := cli.NewFloat64Slice()
tis := testApplyInputSource{
Flag: NewFloat64SliceFlag(&cli.Float64SliceFlag{Name: "test", Destination: dest}),
FlagName: "test1",
MapValue: []interface{}{float64(1.0), float64(2.1)},
}
c := runTest(t, tis)
expect(t, c.Float64Slice("test"), []float64{})
expect(t, dest.Value(), []float64{})
}

func TestFloat64SliceFlagApplyInputSourceValueInvalidType(t *testing.T) {
dest := cli.NewFloat64Slice()
tis := testApplyInputSource{
Flag: NewFloat64SliceFlag(&cli.Float64SliceFlag{Name: "test", Destination: dest}),
FlagName: "test",
MapValue: []interface{}{1, 2},
}
c := runTest(t, tis)
expect(t, c.Float64Slice("test"), []float64{})
expect(t, dest.Value(), []float64{})
}

func TestBoolApplyInputSourceMethodSet(t *testing.T) {
tis := testApplyInputSource{
Flag: NewBoolFlag(&cli.BoolFlag{Name: "test"}),
Expand Down Expand Up @@ -524,6 +583,27 @@ func TestIntApplyInputSourceMethodContextSet(t *testing.T) {
refute(t, 7, c.Int("test"))
}

func TestIntApplyInputSourceMethodContextNotSet(t *testing.T) {
tis := testApplyInputSource{
Flag: NewIntFlag(&cli.IntFlag{Name: "test"}),
FlagName: "test1",
MapValue: 15,
ContextValueString: "7",
}
c := runTest(t, tis)
expect(t, 0, c.Int("test"))
}

func TestIntApplyInputSourceMethodContextSetInvalidType(t *testing.T) {
tis := testApplyInputSource{
Flag: NewIntFlag(&cli.IntFlag{Name: "test"}),
FlagName: "test",
ContextValueString: "d",
}
c := runTest(t, tis)
expect(t, 0, c.Int("test"))
}

func TestIntApplyInputSourceMethodEnvVarSet(t *testing.T) {
tis := testApplyInputSource{
Flag: NewIntFlag(&cli.IntFlag{Name: "test", EnvVars: []string{"TEST"}}),
Expand All @@ -539,6 +619,190 @@ func TestIntApplyInputSourceMethodEnvVarSet(t *testing.T) {
refute(t, 12, c.Int("test"))
}

func TestInt64ApplyInputSourceMethodSet_Alias(t *testing.T) {
tis := testApplyInputSource{
Flag: NewInt64Flag(&cli.Int64Flag{Name: "test", Aliases: []string{"test_alias"}}),
FlagName: "test_alias",
MapValue: int64(15),
}
c := runTest(t, tis)
expect(t, int64(15), c.Int64("test_alias"))

c = runRacyTest(t, tis)
refute(t, int64(15), c.Int64("test_alias"))
}

func TestInt64ApplyInputSourceMethodSet(t *testing.T) {
tis := testApplyInputSource{
Flag: NewInt64Flag(&cli.Int64Flag{Name: "test"}),
FlagName: "test",
MapValue: int64(15),
}
c := runTest(t, tis)
expect(t, int64(15), c.Int64("test"))

c = runRacyTest(t, tis)
refute(t, int64(15), c.Int("test"))
}

func TestInt64ApplyInputSourceMethodSetNegativeValue(t *testing.T) {
tis := testApplyInputSource{
Flag: NewInt64Flag(&cli.Int64Flag{Name: "test"}),
FlagName: "test",
MapValue: int64(-1),
}
c := runTest(t, tis)
expect(t, int64(-1), c.Int64("test"))

c = runRacyTest(t, tis)
refute(t, int64(-1), c.Int("test"))
}

func TestInt64ApplyInputSourceMethodContextSet(t *testing.T) {
tis := testApplyInputSource{
Flag: NewInt64Flag(&cli.Int64Flag{Name: "test"}),
FlagName: "test",
MapValue: 15,
ContextValueString: "7",
}
c := runTest(t, tis)
expect(t, int64(7), c.Int64("test"))

c = runRacyTest(t, tis)
refute(t, int64(7), c.Int64("test"))
}

func TestInt64ApplyInputSourceMethodContextNotSet(t *testing.T) {
tis := testApplyInputSource{
Flag: NewInt64Flag(&cli.Int64Flag{Name: "test"}),
FlagName: "test1",
MapValue: 15,
ContextValueString: "7",
}
c := runTest(t, tis)
expect(t, int64(0), c.Int64("test"))
}

func TestInt64ApplyInputSourceMethodContextSetInvalidType(t *testing.T) {
tis := testApplyInputSource{
Flag: NewInt64Flag(&cli.Int64Flag{Name: "test"}),
FlagName: "test",
ContextValueString: "d",
}
c := runTest(t, tis)
expect(t, int64(0), c.Int64("test"))
}

func TestInt64ApplyInputSourceMethodEnvVarSet(t *testing.T) {
tis := testApplyInputSource{
Flag: NewInt64Flag(&cli.Int64Flag{Name: "test", EnvVars: []string{"TEST"}}),
FlagName: "test",
MapValue: 15,
EnvVarName: "TEST",
EnvVarValue: "12",
}
c := runTest(t, tis)
expect(t, int64(12), c.Int64("test"))

c = runRacyTest(t, tis)
refute(t, int64(12), c.Int64("test"))
}

func TestUintApplyInputSourceMethodSet_Alias(t *testing.T) {
tis := testApplyInputSource{
Flag: NewUintFlag(&cli.UintFlag{Name: "test", Aliases: []string{"test_alias"}}),
FlagName: "test_alias",
MapValue: uint(15),
}
c := runTest(t, tis)
expect(t, uint(15), c.Uint("test_alias"))

c = runRacyTest(t, tis)
refute(t, uint(15), c.Uint("test_alias"))
}

func TestUintApplyInputSourceMethodSet(t *testing.T) {
tis := testApplyInputSource{
Flag: NewUintFlag(&cli.UintFlag{Name: "test"}),
FlagName: "test",
MapValue: uint(15),
}
c := runTest(t, tis)
expect(t, uint(15), c.Uint("test"))

c = runRacyTest(t, tis)
refute(t, uint(15), c.Uint("test"))
}

func TestUintApplyInputSourceMethodContextSet(t *testing.T) {
tis := testApplyInputSource{
Flag: NewUintFlag(&cli.UintFlag{Name: "test"}),
FlagName: "test",
MapValue: uint(15),
ContextValueString: "7",
}
c := runTest(t, tis)
expect(t, uint(7), c.Uint("test"))

c = runRacyTest(t, tis)
refute(t, uint(7), c.Uint("test"))
}

func TestUint64ApplyInputSourceMethodSet_Alias(t *testing.T) {
tis := testApplyInputSource{
Flag: NewUint64Flag(&cli.Uint64Flag{Name: "test", Aliases: []string{"test_alias"}}),
FlagName: "test_alias",
MapValue: uint64(15),
}
c := runTest(t, tis)
expect(t, uint64(15), c.Uint64("test_alias"))

c = runRacyTest(t, tis)
refute(t, uint64(15), c.Uint64("test_alias"))
}

func TestUint64ApplyInputSourceMethodSet(t *testing.T) {
tis := testApplyInputSource{
Flag: NewUint64Flag(&cli.Uint64Flag{Name: "test"}),
FlagName: "test",
MapValue: uint64(15),
}
c := runTest(t, tis)
expect(t, uint64(15), c.Uint64("test"))

c = runRacyTest(t, tis)
refute(t, uint64(15), c.Uint64("test"))
}

func TestUint64ApplyInputSourceMethodContextSet(t *testing.T) {
tis := testApplyInputSource{
Flag: NewUint64Flag(&cli.Uint64Flag{Name: "test"}),
FlagName: "test",
MapValue: uint64(15),
ContextValueString: "7",
}
c := runTest(t, tis)
expect(t, uint64(7), c.Uint64("test"))

c = runRacyTest(t, tis)
refute(t, uint64(7), c.Uint64("test"))
}

func TestUint64ApplyInputSourceMethodEnvVarSet(t *testing.T) {
tis := testApplyInputSource{
Flag: NewUint64Flag(&cli.Uint64Flag{Name: "test", EnvVars: []string{"TEST"}}),
FlagName: "test",
MapValue: uint64(15),
EnvVarName: "TEST",
EnvVarValue: "12",
}
c := runTest(t, tis)
expect(t, uint64(12), c.Uint64("test"))

c = runRacyTest(t, tis)
refute(t, uint64(12), c.Uint64("test"))
}

func TestDurationApplyInputSourceMethodSet_Alias(t *testing.T) {
tis := testApplyInputSource{
Flag: NewDurationFlag(&cli.DurationFlag{Name: "test", Aliases: []string{"test_alias"}}),
Expand Down
Loading

0 comments on commit 9d6c243

Please sign in to comment.