-
Notifications
You must be signed in to change notification settings - Fork 0
/
expressioncondition.go
99 lines (85 loc) · 3.97 KB
/
expressioncondition.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package dygo
import (
"strings"
"github.com/aws/aws-sdk-go-v2/feature/dynamodb/expression"
)
// ConditionFunc returns a condition builder for an attribute.
type ConditionFunc func(string) expression.ConditionBuilder
// ConditionEqual returns a ConditionFunc that checks if the attribute value is equal to the specified value.
func ConditionEqual(value any) ConditionFunc {
return func(keyName string) expression.ConditionBuilder {
return expression.Name(keyName).Equal(expression.Value(value))
}
}
// ConditionBeginsWith returns a ConditionFunc that checks item based on whether the attribute's value begins with the specified prefix.
func ConditionBeginsWith(prefix any) ConditionFunc {
return func(keyName string) expression.ConditionBuilder {
return expression.Name(keyName).BeginsWith(prefix.(string))
}
}
// ConditionBetween returns a ConditionFunc that checks if the attribute value is between two specified values.
func ConditionBetween(start, end any) ConditionFunc {
return func(keyName string) expression.ConditionBuilder {
return expression.Name(keyName).Between(expression.Value(start), expression.Value(end))
}
}
// ConditionLessThan returns a ConditionFunc that checks if the attribute value is less than the specified value.
func ConditionLessThan(value any) ConditionFunc {
return func(keyName string) expression.ConditionBuilder {
return expression.Name(keyName).LessThan(expression.Value(value))
}
}
// ConditionLessThanOrEqual returns a ConditionFunc that checks if the attribute value is less than or equal to the specified value.
func ConditionLessThanOrEqual(value any) ConditionFunc {
return func(keyName string) expression.ConditionBuilder {
return expression.Name(keyName).LessThanEqual(expression.Value(value))
}
}
// ConditionGreaterThan returns a ConditionFunc that checks if the attribute value is greater than the specified value.
func ConditionGreaterThan(value any) ConditionFunc {
return func(keyName string) expression.ConditionBuilder {
return expression.Name(keyName).GreaterThan(expression.Value(value))
}
}
// ConditionGreaterThanOrEqual returns a ConditionFunc that checks if the attribute value is greater than or equal to the specified value.
func ConditionGreaterThanOrEqual(value any) ConditionFunc {
return func(keyName string) expression.ConditionBuilder {
return expression.Name(keyName).GreaterThanEqual(expression.Value(value))
}
}
// ConditionNotEqual returns a ConditionFunc that checks if the attribute value is not equal to the specified value.
func ConditionNotEqual(value any) ConditionFunc {
return func(keyName string) expression.ConditionBuilder {
return expression.Name(keyName).NotEqual(expression.Value(value))
}
}
// ConditionIn returns a ConditionFunc that checks if the attribute value is one of the specified values.
func ConditionIn(value any) ConditionFunc {
return func(keyName string) expression.ConditionBuilder {
csv := getStringValue(value)
if csv != "" {
// Split the comma-separated string into a slice of values.
values := strings.Split(csv, ",")
// Create a slice of OperandBuilders from the values.
operands := make([]expression.OperandBuilder, len(values))
for i, v := range values {
operands[i] = expression.Value(strings.TrimSpace(v))
}
// Return the ConditionBuilder with the IN condition for the keyName.
return expression.Name(keyName).In(operands[0], operands[1:]...)
}
return expression.Name(keyName).In(expression.Value(csv))
}
}
// ConditionAttributeExists returns a ConditionFunc that checks if the specified attribute exists in the item.
func ConditionAttributeExists() ConditionFunc {
return func(keyName string) expression.ConditionBuilder {
return expression.Name(keyName).AttributeExists()
}
}
// ConditionAttributeNotExists returns a ConditionFunc that checks if the specified attribute does not exist in the item.
func ConditionAttributeNotExists() ConditionFunc {
return func(keyName string) expression.ConditionBuilder {
return expression.Name(keyName).AttributeNotExists()
}
}