Skip to content

Commit 190e5b6

Browse files
authored
Merge pull request #1551 from Edelweiss-Snow/issue_1550
Feature:(issue_1550):Add support Int64Slice by read toml config file
2 parents e194a18 + a0343df commit 190e5b6

8 files changed

+134
-0
lines changed

altsrc/flag.go

+31
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,37 @@ func (f *IntSliceFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceC
148148
return nil
149149
}
150150

151+
// ApplyInputSourceValue applies a Int64Slice value if required
152+
func (f *Int64SliceFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceContext) error {
153+
if f.set == nil || cCtx.IsSet(f.Name) || isEnvVarSet(f.EnvVars) {
154+
return nil
155+
}
156+
for _, name := range f.Int64SliceFlag.Names() {
157+
if !isc.isSet(name) {
158+
continue
159+
}
160+
value, err := isc.Int64Slice(name)
161+
if err != nil {
162+
return err
163+
}
164+
if value == nil {
165+
continue
166+
}
167+
var sliceValue = *(cli.NewInt64Slice(value...))
168+
for _, n := range f.Names() {
169+
underlyingFlag := f.set.Lookup(n)
170+
if underlyingFlag == nil {
171+
continue
172+
}
173+
underlyingFlag.Value = &sliceValue
174+
}
175+
if f.Destination != nil {
176+
f.Destination.Set(sliceValue.Serialize())
177+
}
178+
}
179+
return nil
180+
}
181+
151182
// ApplyInputSourceValue applies a Bool value to the flagSet if required
152183
func (f *BoolFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceContext) error {
153184
if f.set == nil || cCtx.IsSet(f.Name) || isEnvVarSet(f.EnvVars) {

altsrc/flag_test.go

+23
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,29 @@ func TestIntSliceApplyInputSourceMethodEnvVarSet(t *testing.T) {
257257
refute(t, c.IntSlice("test"), []int{3, 4})
258258
}
259259

260+
func TestInt64SliceFlagApplyInputSourceValue(t *testing.T) {
261+
dest := cli.NewInt64Slice()
262+
tis := testApplyInputSource{
263+
Flag: NewInt64SliceFlag(&cli.Int64SliceFlag{Name: "test", Destination: dest}),
264+
FlagName: "test",
265+
MapValue: []interface{}{int64(1), int64(2)},
266+
}
267+
c := runTest(t, tis)
268+
expect(t, c.Int64Slice("test"), []int64{1, 2})
269+
expect(t, dest.Value(), []int64{1, 2})
270+
271+
// reset dest
272+
dest = cli.NewInt64Slice()
273+
tis = testApplyInputSource{
274+
Flag: NewInt64SliceFlag(&cli.Int64SliceFlag{Name: "test", Destination: dest}),
275+
FlagName: "test",
276+
MapValue: []interface{}{int64(1), int64(2)},
277+
}
278+
c = runRacyTest(t, tis)
279+
refute(t, c.IntSlice("test"), []int64{1, 2})
280+
refute(t, dest.Value(), []int64{1, 2})
281+
}
282+
260283
func TestBoolApplyInputSourceMethodSet(t *testing.T) {
261284
tis := testApplyInputSource{
262285
Flag: NewBoolFlag(&cli.BoolFlag{Name: "test"}),

altsrc/input_source_context.go

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ type InputSourceContext interface {
2020
String(name string) (string, error)
2121
StringSlice(name string) ([]string, error)
2222
IntSlice(name string) ([]int, error)
23+
Int64Slice(name string) ([]int64, error)
2324
Generic(name string) (cli.Generic, error)
2425
Bool(name string) (bool, error)
2526

altsrc/json_source_context.go

+23
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,29 @@ func (x *jsonSource) IntSlice(name string) ([]int, error) {
160160
}
161161
}
162162

163+
func (x *jsonSource) Int64Slice(name string) ([]int64, error) {
164+
i, err := x.getValue(name)
165+
if err != nil {
166+
return nil, err
167+
}
168+
switch v := i.(type) {
169+
default:
170+
return nil, fmt.Errorf("unexpected type %T for %q", i, name)
171+
case []int64:
172+
return v, nil
173+
case []interface{}:
174+
c := []int64{}
175+
for _, s := range v {
176+
if i2, ok := s.(int64); ok {
177+
c = append(c, i2)
178+
} else {
179+
return c, fmt.Errorf("unexpected item type %T in %T for %q", s, c, name)
180+
}
181+
}
182+
return c, nil
183+
}
184+
}
185+
163186
func (x *jsonSource) Generic(name string) (cli.Generic, error) {
164187
i, err := x.getValue(name)
165188
if err != nil {

altsrc/map_input_source.go

+29
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,35 @@ func (fsm *MapInputSource) IntSlice(name string) ([]int, error) {
207207
return intSlice, nil
208208
}
209209

210+
// Int64Slice returns an []int64 from the map if it exists otherwise returns nil
211+
func (fsm *MapInputSource) Int64Slice(name string) ([]int64, error) {
212+
otherGenericValue, exists := fsm.valueMap[name]
213+
if !exists {
214+
otherGenericValue, exists = nestedVal(name, fsm.valueMap)
215+
if !exists {
216+
return nil, nil
217+
}
218+
}
219+
220+
otherValue, isType := otherGenericValue.([]interface{})
221+
if !isType {
222+
return nil, incorrectTypeForFlagError(name, "[]interface{}", otherGenericValue)
223+
}
224+
225+
var int64Slice = make([]int64, 0, len(otherValue))
226+
for i, v := range otherValue {
227+
int64Value, isType := v.(int64)
228+
229+
if !isType {
230+
return nil, incorrectTypeForFlagError(fmt.Sprintf("%s[%d]", name, i), "int", v)
231+
}
232+
233+
int64Slice = append(int64Slice, int64Value)
234+
}
235+
236+
return int64Slice, nil
237+
}
238+
210239
// Generic returns an cli.Generic from the map if it exists otherwise returns nil
211240
func (fsm *MapInputSource) Generic(name string) (cli.Generic, error) {
212241
otherGenericValue, exists := fsm.valueMap[name]

altsrc/map_input_source_test.go

+11
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,14 @@ func TestMapDuration(t *testing.T) {
2222
_, err = inputSource.Duration("duration_of_int_type")
2323
refute(t, nil, err)
2424
}
25+
26+
func TestMapInputSource_Int64Slice(t *testing.T) {
27+
inputSource := NewMapInputSource(
28+
"test",
29+
map[interface{}]interface{}{
30+
"test_num": []interface{}{int64(1), int64(2), int64(3)},
31+
})
32+
d, err := inputSource.Int64Slice("test_num")
33+
expect(t, []int64{1, 2, 3}, d)
34+
expect(t, nil, err)
35+
}

godoc-current.txt

+8
Original file line numberDiff line numberDiff line change
@@ -2437,6 +2437,7 @@ type InputSourceContext interface {
24372437
String(name string) (string, error)
24382438
StringSlice(name string) ([]string, error)
24392439
IntSlice(name string) ([]int, error)
2440+
Int64Slice(name string) ([]int64, error)
24402441
Generic(name string) (cli.Generic, error)
24412442
Bool(name string) (bool, error)
24422443

@@ -2494,6 +2495,9 @@ func (f *Int64SliceFlag) Apply(set *flag.FlagSet) error
24942495
Apply saves the flagSet for later usage calls, then calls the wrapped
24952496
Int64SliceFlag.Apply
24962497

2498+
func (f *Int64SliceFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceContext) error
2499+
ApplyInputSourceValue applies a Int64Slice value if required
2500+
24972501
type IntFlag struct {
24982502
*cli.IntFlag
24992503
// Has unexported fields.
@@ -2554,6 +2558,10 @@ func (fsm *MapInputSource) Generic(name string) (cli.Generic, error)
25542558
func (fsm *MapInputSource) Int(name string) (int, error)
25552559
Int returns an int from the map if it exists otherwise returns 0
25562560

2561+
func (fsm *MapInputSource) Int64Slice(name string) ([]int64, error)
2562+
Int64Slice returns an []int64 from the map if it exists otherwise returns
2563+
nil
2564+
25572565
func (fsm *MapInputSource) IntSlice(name string) ([]int, error)
25582566
IntSlice returns an []int from the map if it exists otherwise returns nil
25592567

testdata/godoc-v2.x.txt

+8
Original file line numberDiff line numberDiff line change
@@ -2437,6 +2437,7 @@ type InputSourceContext interface {
24372437
String(name string) (string, error)
24382438
StringSlice(name string) ([]string, error)
24392439
IntSlice(name string) ([]int, error)
2440+
Int64Slice(name string) ([]int64, error)
24402441
Generic(name string) (cli.Generic, error)
24412442
Bool(name string) (bool, error)
24422443

@@ -2494,6 +2495,9 @@ func (f *Int64SliceFlag) Apply(set *flag.FlagSet) error
24942495
Apply saves the flagSet for later usage calls, then calls the wrapped
24952496
Int64SliceFlag.Apply
24962497

2498+
func (f *Int64SliceFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceContext) error
2499+
ApplyInputSourceValue applies a Int64Slice value if required
2500+
24972501
type IntFlag struct {
24982502
*cli.IntFlag
24992503
// Has unexported fields.
@@ -2554,6 +2558,10 @@ func (fsm *MapInputSource) Generic(name string) (cli.Generic, error)
25542558
func (fsm *MapInputSource) Int(name string) (int, error)
25552559
Int returns an int from the map if it exists otherwise returns 0
25562560

2561+
func (fsm *MapInputSource) Int64Slice(name string) ([]int64, error)
2562+
Int64Slice returns an []int64 from the map if it exists otherwise returns
2563+
nil
2564+
25572565
func (fsm *MapInputSource) IntSlice(name string) ([]int, error)
25582566
IntSlice returns an []int from the map if it exists otherwise returns nil
25592567

0 commit comments

Comments
 (0)