From ef4a3e7a29dc84bb3fabb23142e350fbb5bfdd42 Mon Sep 17 00:00:00 2001 From: Eric Douglas Date: Fri, 17 Mar 2017 10:41:24 -0700 Subject: [PATCH] conditions: Updated the type coerciion performed on the value received in the StringPairsEqualCondition to respect JSON Signed-off-by: Eric Douglas --- condition_string_pairs_equal.go | 28 +++++++++++++++------------- condition_string_pairs_equal_test.go | 17 ++++++++--------- 2 files changed, 23 insertions(+), 22 deletions(-) diff --git a/condition_string_pairs_equal.go b/condition_string_pairs_equal.go index 676f3c0..c272537 100644 --- a/condition_string_pairs_equal.go +++ b/condition_string_pairs_equal.go @@ -8,24 +8,26 @@ type StringPairsEqualCondition struct{} // Fulfills returns true if the given value is an array of string arrays and // each string array has exactly two values which are equal func (c *StringPairsEqualCondition) Fulfills(value interface{}, _ *Request) bool { - pairs, PairsOk := value.([][]interface{}) + pairs, PairsOk := value.([]interface{}) + if !PairsOk { + return false + } + + for _, v := range pairs { + pair, PairOk := v.([]interface{}) + if !PairOk || (len(pair) != 2) { + return false + } - if PairsOk { - for _, pair := range pairs { - if (len(pair) != 2) { - return false - } - a, AOk := pair[0].(string) - b, BOk := pair[1].(string) + a, AOk := pair[0].(string) + b, BOk := pair[1].(string) - if !AOk || !BOk || (a != b) { - return false - } + if !AOk || !BOk || (a != b) { + return false } - return true } - return false + return true } // GetName returns the condition's name. diff --git a/condition_string_pairs_equal_test.go b/condition_string_pairs_equal_test.go index bf5482b..1be3924 100644 --- a/condition_string_pairs_equal_test.go +++ b/condition_string_pairs_equal_test.go @@ -12,15 +12,14 @@ func TestStringPairsEqualMatch(t *testing.T) { pass bool }{ {pairs: "junk", pass: false}, - {pairs: [][]interface{}{{}}, pass: false}, - {pairs: [][]interface{}{{"1"}}, pass: false}, - {pairs: [][]interface{}{{"1", "1", "2"}}, pass: false}, - {pairs: [][]interface{}{{"1", "2"}}, pass: false}, - {pairs: [][]interface{}{{"1", "1"},{"2", "3"}}, pass: false}, - {pairs: []interface{}{}, pass: false}, - {pairs: [][]interface{}{}, pass: true}, - {pairs: [][]interface{}{{"1", "1"}}, pass: true}, - {pairs: [][]interface{}{{"1", "1"},{"2", "2"}}, pass: true}, + {pairs: []interface{}{[]interface{}{}}, pass: false}, + {pairs: []interface{}{[]interface{}{"1"}}, pass: false}, + {pairs: []interface{}{[]interface{}{"1", "1", "2"}}, pass: false}, + {pairs: []interface{}{[]interface{}{"1", "2"}}, pass: false}, + {pairs: []interface{}{[]interface{}{"1", "1"},[]interface{}{"2", "3"}}, pass: false}, + {pairs: []interface{}{}, pass: true}, + {pairs: []interface{}{[]interface{}{"1", "1"}}, pass: true}, + {pairs: []interface{}{[]interface{}{"1", "1"},[]interface{}{"2", "2"}}, pass: true}, } { condition := &StringPairsEqualCondition{}