mirror of
https://github.com/rclone/rclone
synced 2024-11-21 22:50:16 +01:00
rc: add GetStructMissingOK
This commit is contained in:
parent
1f61027f51
commit
195d152785
@ -202,3 +202,13 @@ func (p Params) GetStruct(key string, out interface{}) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetStructMissingOK works like GetStruct but doesn't return an error
|
||||
// if the key is missing
|
||||
func (p Params) GetStructMissingOK(key string, out interface{}) error {
|
||||
_, ok := p[key]
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
return p.GetStruct(key, out)
|
||||
}
|
||||
|
@ -249,3 +249,32 @@ func TestParamsGetStruct(t *testing.T) {
|
||||
assert.Equal(t, 4.2, out.Float)
|
||||
assert.Equal(t, true, IsErrParamInvalid(e3), e3.Error())
|
||||
}
|
||||
|
||||
func TestParamsGetStructMissingOK(t *testing.T) {
|
||||
in := Params{
|
||||
"struct": Params{
|
||||
"String": "one",
|
||||
"Float": 4.2,
|
||||
},
|
||||
}
|
||||
var out struct {
|
||||
String string
|
||||
Float float64
|
||||
}
|
||||
e1 := in.GetStructMissingOK("struct", &out)
|
||||
assert.NoError(t, e1)
|
||||
assert.Equal(t, "one", out.String)
|
||||
assert.Equal(t, 4.2, out.Float)
|
||||
|
||||
e2 := in.GetStructMissingOK("notOK", &out)
|
||||
assert.NoError(t, e2)
|
||||
assert.Equal(t, "one", out.String)
|
||||
assert.Equal(t, 4.2, out.Float)
|
||||
|
||||
in["struct"] = "string"
|
||||
e3 := in.GetStructMissingOK("struct", &out)
|
||||
assert.Error(t, e3)
|
||||
assert.Equal(t, "one", out.String)
|
||||
assert.Equal(t, 4.2, out.Float)
|
||||
assert.Equal(t, true, IsErrParamInvalid(e3), e3.Error())
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user