1
mirror of https://github.com/rclone/rclone synced 2024-11-02 23:09:23 +01:00

rc: add GetStructMissingOK

This commit is contained in:
Nick Craig-Wood 2020-04-28 12:49:19 +01:00
parent 1f61027f51
commit 195d152785
2 changed files with 39 additions and 0 deletions

View File

@ -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)
}

View File

@ -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())
}