Skip to content

Commit

Permalink
service/dynamodb/expression: Add IsSet helper for ConditionBuilder an…
Browse files Browse the repository at this point in the history
…d 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.
  • Loading branch information
Alex Bok authored Apr 7, 2020
1 parent ce27111 commit 2bc00eb
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG_PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,12 @@ SDK Features
* Fixes [#515](https://github.com/aws/aws-sdk-go-v2/issues/515)
SDK Enhancements
---
* `service/dynamodb/expression`: Add IsSet helper for ConditionBuilder and KeyConditionBuilder ([#494](https://github.com/aws/aws-sdk-go-v2/pull/494))
* Adds a IsSet helper for ConditionBuilder and KeyConditionBuilder to make it easier to determine if the condition builders have any conditions added to them.
* Implements [#493](https://github.com/aws/aws-sdk-go-v2/issues/493).
* `internal/ini`: Normalize Section keys to lowercase ([#495](https://github.com/aws/aws-sdk-go-v2/pull/495))
* Update's SDK's ini utility to store all keys as lowercase. This brings the SDK inline with the AWS CLI's behavior.


SDK Bugs
---
14 changes: 14 additions & 0 deletions service/dynamodb/expression/condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
26 changes: 26 additions & 0 deletions service/dynamodb/expression/condition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,32 @@ const (
invalidConditionOperand = "BuildOperand error"
)

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 {
Expand Down
14 changes: 14 additions & 0 deletions service/dynamodb/expression/key_condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,20 @@ type KeyConditionBuilder struct {
mode keyConditionMode
}

// 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
}

// KeyEqual 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
Expand Down
26 changes: 26 additions & 0 deletions service/dynamodb/expression/key_condition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,32 @@ const (
invalidKeyConditionFormat = "buildKeyCondition error: invalid key condition constructed"
)

func TestIsKeySet(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
Expand Down

0 comments on commit 2bc00eb

Please sign in to comment.