From 356398ece9ce863c717177bbcb291be9ef2f9686 Mon Sep 17 00:00:00 2001 From: Zaq? Wiedmann Date: Tue, 20 Jul 2021 17:03:34 -0700 Subject: [PATCH] feat: (feature/dynamodb/expression) re-add IsSet methods (#1329) * feat: (feature/dynamodb/expression) readd IsSet methods These changes were present in an older version of the expression package before it was moved, originally added in https://github.com/aws/aws-sdk-go-v2/pull/494, when the expression library was reintroduce in https://github.com/aws/aws-sdk-go-v2/pull/981 these changes werent included for some reason, but the need for checking if a condition is set or not still exists * Add Change Annotation Co-authored-by: Sean McGrail --- .../8e261b4b5633485cad821d38e2677e46.json | 8 ++++++ feature/dynamodb/expression/condition.go | 14 ++++++++++ feature/dynamodb/expression/condition_test.go | 27 +++++++++++++++++++ feature/dynamodb/expression/key_condition.go | 14 ++++++++++ .../dynamodb/expression/key_condition_test.go | 27 +++++++++++++++++++ 5 files changed, 90 insertions(+) create mode 100644 .changelog/8e261b4b5633485cad821d38e2677e46.json diff --git a/.changelog/8e261b4b5633485cad821d38e2677e46.json b/.changelog/8e261b4b5633485cad821d38e2677e46.json new file mode 100644 index 00000000000..4cd7940ebec --- /dev/null +++ b/.changelog/8e261b4b5633485cad821d38e2677e46.json @@ -0,0 +1,8 @@ +{ + "id": "8e261b4b-5633-485c-ad82-1d38e2677e46", + "type": "feature", + "description": "Add IsSet helper for ConditionBuilder and KeyConditionBuilder ([#1329](https://github.com/aws/aws-sdk-go-v2/pull/1329))", + "modules": [ + "feature/dynamodb/expression" + ] +} \ No newline at end of file diff --git a/feature/dynamodb/expression/condition.go b/feature/dynamodb/expression/condition.go index 39e1103135e..fbb1a811d29 100644 --- a/feature/dynamodb/expression/condition.go +++ b/feature/dynamodb/expression/condition.go @@ -88,6 +88,20 @@ type ConditionBuilder struct { mode conditionMode } +// IsSet returns true if the ConditionBuilder is set and returns false +// otherwise. A zero value of a ConditionBuilder returns false. +// +// Example: +// +// var condition expression.ConditionBuilder +// condition.IsSet() // returns false +// +// condition := expression.Equal(expression.Name("foo"), expression.Value(5)) +// condition.IsSet() // returns true +func (cb ConditionBuilder) IsSet() bool { + return cb.mode != unsetCond +} + // Equal returns a ConditionBuilder representing the equality clause of the two // argument OperandBuilders. The resulting ConditionBuilder can be used as a // part of other Condition Expressions or as an argument to the WithCondition() diff --git a/feature/dynamodb/expression/condition_test.go b/feature/dynamodb/expression/condition_test.go index 4257cf95217..8e799333ed3 100644 --- a/feature/dynamodb/expression/condition_test.go +++ b/feature/dynamodb/expression/condition_test.go @@ -21,6 +21,33 @@ const ( invalidConditionOperand = "BuildOperand error" ) +//IsSet +func TestIsSet(t *testing.T) { + cases := []struct { + name string + input ConditionBuilder + expected bool + }{ + { + name: "set", + input: Equal(Name("foo"), Value("bar")), + expected: true, + }, + { + name: "unset", + input: ConditionBuilder{}, + expected: false, + }, + } + for _, c := range cases { + t.Run(c.name, func(t *testing.T) { + if actual := c.input.IsSet(); actual != c.expected { + t.Errorf("expected %t, got %t", c.expected, actual) + } + }) + } +} + //Compare func TestCompare(t *testing.T) { cases := []struct { diff --git a/feature/dynamodb/expression/key_condition.go b/feature/dynamodb/expression/key_condition.go index e8917b67746..c913684d0f1 100644 --- a/feature/dynamodb/expression/key_condition.go +++ b/feature/dynamodb/expression/key_condition.go @@ -68,6 +68,20 @@ func KeyEqual(keyBuilder KeyBuilder, valueBuilder ValueBuilder) KeyConditionBuil } } +// IsSet returns true if the KeyConditionBuilder is set and returns +// false otherwise. A zero value of a KeyConditionBuilder returns false. +// +// Example: +// +// var keyCondition expression.KeyConditionBuilder +// keyCondition.IsSet() // returns false +// +// keyCondition := expression.KeyEqual(expression.Key("foo"), expression.Value(5)) +// keyCondition.IsSet() // returns true +func (kcb KeyConditionBuilder) IsSet() bool { + return kcb.mode != unsetKeyCond +} + // Equal returns a KeyConditionBuilder representing the equality clause of // the two argument OperandBuilders. The resulting KeyConditionBuilder can be // used as a part of other Key Condition Expressions or as an argument to the diff --git a/feature/dynamodb/expression/key_condition_test.go b/feature/dynamodb/expression/key_condition_test.go index 5eef7e491a2..da80d0ea34b 100644 --- a/feature/dynamodb/expression/key_condition_test.go +++ b/feature/dynamodb/expression/key_condition_test.go @@ -24,6 +24,33 @@ const ( invalidKeyConditionFormat = "buildKeyCondition error: invalid key condition constructed" ) +//IsSet +func TestKeyIsSet(t *testing.T) { + cases := []struct { + name string + input KeyConditionBuilder + expected bool + }{ + { + name: "set", + input: KeyEqual(Key("foo"), Value("bar")), + expected: true, + }, + { + name: "unset", + input: KeyConditionBuilder{}, + expected: false, + }, + } + for _, c := range cases { + t.Run(c.name, func(t *testing.T) { + if actual := c.input.IsSet(); actual != c.expected { + t.Errorf("expected %t, got %t", c.expected, actual) + } + }) + } +} + func TestKeyCompare(t *testing.T) { cases := []struct { name string