Skip to content

Commit

Permalink
fix: NameBuilder.Contains() supports all types for usage on lists (#2324
Browse files Browse the repository at this point in the history
)
  • Loading branch information
andrewcretin committed Oct 19, 2023
1 parent b78564c commit 26470da
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 5 deletions.
16 changes: 12 additions & 4 deletions feature/dynamodb/expression/condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -1346,6 +1346,10 @@ func (nb NameBuilder) BeginsWith(prefix string) ConditionBuilder {
// // attribute "InviteList" has the value "Ben"
// condition := expression.Contains(expression.Name("InviteList"), "Ben")
//
// // condition represents the boolean condition of whether the item
// // attribute "LocationIds" has the value 2
// condition := expression.Contains(expression.Name("LocationIds"), 2)
//
// // Used in another Condition Expression
// anotherCondition := expression.Not(condition)
// // Used to make an Builder
Expand All @@ -1356,9 +1360,9 @@ func (nb NameBuilder) BeginsWith(prefix string) ConditionBuilder {
// expression.Contains(expression.Name("InviteList"), "Ben")
// // Let :ben be an ExpressionAttributeValue representing the value "Ben"
// "contains (InviteList, :ben)"
func Contains(nameBuilder NameBuilder, substr string) ConditionBuilder {
func Contains(nameBuilder NameBuilder, val interface{}) ConditionBuilder {
v := ValueBuilder{
value: substr,
value: val,
}
return ConditionBuilder{
operandList: []OperandBuilder{nameBuilder, v},
Expand All @@ -1377,6 +1381,10 @@ func Contains(nameBuilder NameBuilder, substr string) ConditionBuilder {
// // attribute "InviteList" has the value "Ben"
// condition := expression.Name("InviteList").Contains("Ben")
//
// // condition represents the boolean condition of whether the item
// // attribute "LocationIds" has the value 2
// condition := expression.Name("LocationIds").Contains(2)
//
// // Used in another Condition Expression
// anotherCondition := expression.Not(condition)
// // Used to make an Builder
Expand All @@ -1387,8 +1395,8 @@ func Contains(nameBuilder NameBuilder, substr string) ConditionBuilder {
// expression.Name("InviteList").Contains("Ben")
// // Let :ben be an ExpressionAttributeValue representing the value "Ben"
// "contains (InviteList, :ben)"
func (nb NameBuilder) Contains(substr string) ConditionBuilder {
return Contains(nb, substr)
func (nb NameBuilder) Contains(val interface{}) ConditionBuilder {
return Contains(nb, val)
}

// buildTree builds a tree structure of exprNodes based on the tree
Expand Down
40 changes: 39 additions & 1 deletion feature/dynamodb/expression/condition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1397,7 +1397,7 @@ func TestContainsCondition(t *testing.T) {
err condErrorMode
}{
{
name: "basic contains",
name: "basic contains string",
input: Name("foo").Contains("bar"),
expectedNode: exprNode{
children: []exprNode{
Expand All @@ -1415,6 +1415,44 @@ func TestContainsCondition(t *testing.T) {
fmtExpr: "contains ($c, $c)",
},
},
{
name: "basic contains number int",
input: Name("foo").Contains(2),
expectedNode: exprNode{
children: []exprNode{
{
names: []string{"foo"},
fmtExpr: "$n",
},
{
values: []types.AttributeValue{
&types.AttributeValueMemberN{Value: "2"},
},
fmtExpr: "$v",
},
},
fmtExpr: "contains ($c, $c)",
},
},
{
name: "basic contains number float",
input: Name("foo").Contains(2.23),
expectedNode: exprNode{
children: []exprNode{
{
names: []string{"foo"},
fmtExpr: "$n",
},
{
values: []types.AttributeValue{
&types.AttributeValueMemberN{Value: "2.23"},
},
fmtExpr: "$v",
},
},
fmtExpr: "contains ($c, $c)",
},
},
{
name: "contains invalid operand",
input: Name("").Contains("bar"),
Expand Down

0 comments on commit 26470da

Please sign in to comment.