1
0
Fork 0

mcfg: implement ParamDefaultOrRequired

gh-v1-backup
Brian Picciano 5 years ago
parent 481b3668c3
commit 4791c02122
  1. 20
      mcfg/mcfg_test.go
  2. 12
      mcfg/param.go

@ -47,3 +47,23 @@ func TestPopulate(t *T) {
assert.Equal(t, 3, *c)
}
}
func TestParamDefaultOrRequired(t *T) {
{
cmp := new(mcmp.Component)
Int(cmp, "a", ParamDefaultOrRequired(0))
params := CollectParams(cmp)
assert.Equal(t, "a", params[0].Name)
assert.Equal(t, true, params[0].Required)
assert.Equal(t, new(int), params[0].Into)
}
{
cmp := new(mcmp.Component)
Int(cmp, "a", ParamDefaultOrRequired(1))
i := 1
params := CollectParams(cmp)
assert.Equal(t, "a", params[0].Name)
assert.Equal(t, false, params[0].Required)
assert.Equal(t, &i, params[0].Into)
}
}

@ -83,6 +83,18 @@ func ParamDefault(value interface{}) ParamOption {
}
}
// ParamDefaultOrRequired returns a ParamOption whose behavior depends on the
// given value. If the given value is the zero value for its type, then this returns
// ParamRequired(), otherwise this returns ParamDefault(value).
func ParamDefaultOrRequired(value interface{}) ParamOption {
v := reflect.ValueOf(value)
zero := reflect.Zero(v.Type())
if v.Interface() == zero.Interface() {
return ParamRequired()
}
return ParamDefault(value)
}
// ParamUsage returns a ParamOption which sets the usage string on the Param.
// This is used in some Sources, like SourceCLI, when displaying information
// about available parameters.

Loading…
Cancel
Save