Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

service/dynamodb/expression: Add IsSet helper for ConditionBuilder and KeyConditionBuilder #494

Merged
merged 6 commits into from
Apr 7, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG_PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ SDK Features

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).

SDK Bugs
---
16 changes: 16 additions & 0 deletions service/dynamodb/expression/condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,22 @@ type ConditionBuilder struct {
mode conditionMode
}

// IsSet returns true if the provided ConditionBuilder is set and returns false
// otherwise. A zero value of a ConditionBuilder returns false.
//
// Example:
//
// var condition expression.ConditionBuilder
// expression.IsSet(condition) // returns false
//
// expression.IsSet(expression.ConditionBuilder{}) // returns false
//
// condition := expression.Equal(expression.Name("foo"), expression.Value(5))
// expressions.IsSet(condition) // returns true
func IsSet(builder ConditionBuilder) bool {
return builder.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 := IsSet(c.input); actual != c.expected {
t.Errorf("expected %t, got %t", c.expected, actual)
}
})
}
}

//Compare
func TestCompare(t *testing.T) {
cases := []struct {
Expand Down
16 changes: 16 additions & 0 deletions service/dynamodb/expression/key_condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,22 @@ type KeyConditionBuilder struct {
mode keyConditionMode
}

// IsKeySet returns true if the provided KeyConditionBuilder is set and returns
// false otherwise. A zero value of a KeyConditionBuilder returns false.
//
// Example:
//
// var keyCondition expression.KeyConditionBuilder
// expression.IsKeySet(keyCondition) // returns false
//
// expression.IsKeySet(expression.KeyConditionBuilder{}) // returns false
//
// keyCondition := expression.KeyEqual(expression.Key("foo"), expression.Value(5))
// expressions.IsKeySet(keyCondition) // returns true
func IsKeySet(builder KeyConditionBuilder) bool {
return builder.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 := IsKeySet(c.input); actual != c.expected {
t.Errorf("expected %t, got %t", c.expected, actual)
}
})
}
}

func TestKeyCompare(t *testing.T) {
cases := []struct {
name string
Expand Down