Skip to content

Commit 2bc00eb

Browse files
author
Alex Bok
authored
service/dynamodb/expression: Add IsSet helper for ConditionBuilder and KeyConditionBuilder (#494)
Adds IsSet method to the ConditionBuilder and KeyConditionBuilder types. This methods makes it easier to discover if the condition builders have any conditions added to them. Implements #493.
1 parent ce27111 commit 2bc00eb

File tree

5 files changed

+84
-0
lines changed

5 files changed

+84
-0
lines changed

CHANGELOG_PENDING.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,12 @@ SDK Features
2727
* Fixes [#515](https://github.com/aws/aws-sdk-go-v2/issues/515)
2828
SDK Enhancements
2929
---
30+
* `service/dynamodb/expression`: Add IsSet helper for ConditionBuilder and KeyConditionBuilder ([#494](https://github.com/aws/aws-sdk-go-v2/pull/494))
31+
* Adds a IsSet helper for ConditionBuilder and KeyConditionBuilder to make it easier to determine if the condition builders have any conditions added to them.
32+
* Implements [#493](https://github.com/aws/aws-sdk-go-v2/issues/493).
3033
* `internal/ini`: Normalize Section keys to lowercase ([#495](https://github.com/aws/aws-sdk-go-v2/pull/495))
3134
* Update's SDK's ini utility to store all keys as lowercase. This brings the SDK inline with the AWS CLI's behavior.
3235

36+
3337
SDK Bugs
3438
---

service/dynamodb/expression/condition.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,20 @@ type ConditionBuilder struct {
8888
mode conditionMode
8989
}
9090

91+
// IsSet returns true if the ConditionBuilder is set and returns false
92+
// otherwise. A zero value of a ConditionBuilder returns false.
93+
//
94+
// Example:
95+
//
96+
// var condition expression.ConditionBuilder
97+
// condition.IsSet() // returns false
98+
//
99+
// condition := expression.Equal(expression.Name("foo"), expression.Value(5))
100+
// condition.IsSet() // returns true
101+
func (cb ConditionBuilder) IsSet() bool {
102+
return cb.mode != unsetCond
103+
}
104+
91105
// Equal returns a ConditionBuilder representing the equality clause of the two
92106
// argument OperandBuilders. The resulting ConditionBuilder can be used as a
93107
// part of other Condition Expressions or as an argument to the WithCondition()

service/dynamodb/expression/condition_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,32 @@ const (
2424
invalidConditionOperand = "BuildOperand error"
2525
)
2626

27+
func TestIsSet(t *testing.T) {
28+
cases := []struct {
29+
name string
30+
input ConditionBuilder
31+
expected bool
32+
}{
33+
{
34+
name: "set",
35+
input: Equal(Name("foo"), Value("bar")),
36+
expected: true,
37+
},
38+
{
39+
name: "unset",
40+
input: ConditionBuilder{},
41+
expected: false,
42+
},
43+
}
44+
for _, c := range cases {
45+
t.Run(c.name, func(t *testing.T) {
46+
if actual := c.input.IsSet(); actual != c.expected {
47+
t.Errorf("expected %t, got %t", c.expected, actual)
48+
}
49+
})
50+
}
51+
}
52+
2753
//Compare
2854
func TestCompare(t *testing.T) {
2955
cases := []struct {

service/dynamodb/expression/key_condition.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,20 @@ type KeyConditionBuilder struct {
4040
mode keyConditionMode
4141
}
4242

43+
// IsSet returns true if the KeyConditionBuilder is set and returns
44+
// false otherwise. A zero value of a KeyConditionBuilder returns false.
45+
//
46+
// Example:
47+
//
48+
// var keyCondition expression.KeyConditionBuilder
49+
// keyCondition.IsSet() // returns false
50+
//
51+
// keyCondition := expression.KeyEqual(expression.Key("foo"), expression.Value(5))
52+
// keyCondition.IsSet() // returns true
53+
func (kcb KeyConditionBuilder) IsSet() bool {
54+
return kcb.mode != unsetKeyCond
55+
}
56+
4357
// KeyEqual returns a KeyConditionBuilder representing the equality clause
4458
// of the two argument OperandBuilders. The resulting KeyConditionBuilder can be
4559
// used as a part of other Key Condition Expressions or as an argument to the

service/dynamodb/expression/key_condition_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,32 @@ const (
2727
invalidKeyConditionFormat = "buildKeyCondition error: invalid key condition constructed"
2828
)
2929

30+
func TestIsKeySet(t *testing.T) {
31+
cases := []struct {
32+
name string
33+
input KeyConditionBuilder
34+
expected bool
35+
}{
36+
{
37+
name: "set",
38+
input: KeyEqual(Key("foo"), Value("bar")),
39+
expected: true,
40+
},
41+
{
42+
name: "unset",
43+
input: KeyConditionBuilder{},
44+
expected: false,
45+
},
46+
}
47+
for _, c := range cases {
48+
t.Run(c.name, func(t *testing.T) {
49+
if actual := c.input.IsSet(); actual != c.expected {
50+
t.Errorf("expected %t, got %t", c.expected, actual)
51+
}
52+
})
53+
}
54+
}
55+
3056
func TestKeyCompare(t *testing.T) {
3157
cases := []struct {
3258
name string

0 commit comments

Comments
 (0)