diff --git a/altsrc/flag.go b/altsrc/flag.go index 725f58f843..4b1c95d5fb 100644 --- a/altsrc/flag.go +++ b/altsrc/flag.go @@ -2,7 +2,6 @@ package altsrc import ( "fmt" - "log" "path/filepath" "strconv" "syscall" @@ -125,7 +124,6 @@ func (f *Int64SliceFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourc if f.set != nil && !cCtx.IsSet(f.Name) && !isEnvVarSet(f.EnvVars) && isc.isSet(f.Int64SliceFlag.Name) { value, err := isc.Int64Slice(f.Int64SliceFlag.Name) if err != nil { - log.Print(err) return err } if value != nil { diff --git a/altsrc/json_command_test.go b/altsrc/json_command_test.go index bd0022b1cd..f6f49890ba 100644 --- a/altsrc/json_command_test.go +++ b/altsrc/json_command_test.go @@ -12,6 +12,12 @@ import ( const ( fileName = "current.json" simpleJSON = `{"test": 15}` + sliceJSON = ` + { + "test1" : [214767949494, 23333495956], + "test2" : [1,2,3,-566] + } + ` nestedJSON = `{"top": {"test": 15}}` ) @@ -46,6 +52,41 @@ func TestCommandJSONFileTest(t *testing.T) { expect(t, err, nil) } +func TestCommandJSONFileTestSlices(t *testing.T) { + cleanup := writeTempFile(t, fileName, sliceJSON) + defer cleanup() + + app := &cli.App{} + set := flag.NewFlagSet("test", 0) + test := []string{"test-cmd", "--load", fileName} + _ = set.Parse(test) + + c := cli.NewContext(app, set, nil) + + command := &cli.Command{ + Name: "test-cmd", + Aliases: []string{"tc"}, + Usage: "this is for testing", + Description: "testing", + Action: func(c *cli.Context) error { + val1 := c.Uint64Slice("test1") + expect(t, val1, []uint64{214767949494, 23333495956}) + + val2 := c.Int64Slice("test2") + expect(t, val2, []int64{1, 2, 3, -566}) + return nil + }, + Flags: []cli.Flag{ + NewUint64SliceFlag(&cli.Uint64SliceFlag{Name: "test1"}), + NewInt64SliceFlag(&cli.Int64SliceFlag{Name: "test2"}), + &cli.StringFlag{Name: "load"}}, + } + command.Before = InitInputSourceWithContext(command.Flags, NewJSONSourceFromFlagFunc("load")) + err := command.Run(c) + + expect(t, err, nil) +} + func TestCommandJSONFileTestGlobalEnvVarWins(t *testing.T) { cleanup := writeTempFile(t, fileName, simpleJSON) defer cleanup() diff --git a/altsrc/json_source_context.go b/altsrc/json_source_context.go index 5c30349053..5ce3121e31 100644 --- a/altsrc/json_source_context.go +++ b/altsrc/json_source_context.go @@ -179,6 +179,8 @@ func (x *jsonSource) Int64Slice(name string) ([]int64, error) { for _, s := range v { if i2, ok := s.(int64); ok { c = append(c, i2) + } else if f2, ok := s.(float64); ok { + c = append(c, int64(f2)) } else { return c, fmt.Errorf("unexpected item type %T in %T for %q", s, c, name) } @@ -206,6 +208,8 @@ func (x *jsonSource) Uint64Slice(name string) ([]uint64, error) { for _, s := range v { if i2, ok := s.(uint64); ok { c = append(c, i2) + } else if f2, ok := s.(float64); ok { + c = append(c, uint64(f2)) } else { return c, fmt.Errorf("unexpected item type %T in %T for %q", s, c, name) }