From b9411fc072c1470fcc16ca9a8b7659c5ecd111b1 Mon Sep 17 00:00:00 2001 From: Drew Zhang Date: Fri, 5 Mar 2021 03:12:05 -0800 Subject: [PATCH] add getBoolParam func into plugin to support bollean config Signed-off-by: Drew Zhang --- plugins/plugins.go | 11 +++++++++++ plugins/plugins_test.go | 9 +++++++++ 2 files changed, 20 insertions(+) diff --git a/plugins/plugins.go b/plugins/plugins.go index 8788713..e83b80e 100644 --- a/plugins/plugins.go +++ b/plugins/plugins.go @@ -230,3 +230,14 @@ func contains(s []string, e string) bool { } return false } + +func getBoolParam(param string, defaultVal bool) bool { + val := strings.ToLower(param) + if val == "true" { + return true + } else if val == "false" { + return false + } else { + return defaultVal + } +} diff --git a/plugins/plugins_test.go b/plugins/plugins_test.go index c6b5f4c..cd3a390 100644 --- a/plugins/plugins_test.go +++ b/plugins/plugins_test.go @@ -66,3 +66,12 @@ func TestDataKeys(t *testing.T) { assert.Equal(t, record["pudding"], "is a dog", "Expected data key to have correct value") assert.Equal(t, record["dumpling"], "is a dog", "Expected data key to have correct value") } + +func TestGetBoolParam(t *testing.T) { + value1 := getBoolParam("true", false) + assert.Equal(t, value1, true, "Expected option value is true") + value2 := getBoolParam("false", false) + assert.Equal(t, value2, false, "Expected option value is false") + value3 := getBoolParam("fakeString", false) + assert.Equal(t, value3, false, "Expected option value is false") +}