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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
|
package expression_test
import (
"context"
"errors"
"fmt"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/feature/dynamodb/expression"
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
)
var config = struct {
LoadDefaultConfig func(ctx context.Context) (aws.Config, error)
}{
LoadDefaultConfig: func(ctx context.Context) (aws.Config, error) {
return aws.Config{}, nil
},
}
// Using Projection Expression
//
// This example queries items in the Music table. The table has a partition key and
// sort key (Artist and SongTitle), but this query only specifies the partition key
// value. It returns song titles by the artist named "No One You Know".
func ExampleBuilder_WithProjection() {
cfg, err := config.LoadDefaultConfig(context.TODO())
if err != nil {
fmt.Println(err.Error())
return
}
client := dynamodb.NewFromConfig(cfg)
// Construct the Key condition builder
keyCond := expression.Key("Artist").Equal(expression.Value("No One You Know"))
// Create the project expression builder with a names list.
proj := expression.NamesList(expression.Name("SongTitle"))
// Combine the key condition, and projection together as a DynamoDB expression
// builder.
expr, err := expression.NewBuilder().
WithKeyCondition(keyCond).
WithProjection(proj).
Build()
if err != nil {
fmt.Println(err)
return
}
// Use the built expression to populate the DynamoDB Query's API input
// parameters.
input := &dynamodb.QueryInput{
ExpressionAttributeValues: expr.Values(),
KeyConditionExpression: expr.KeyCondition(),
ProjectionExpression: expr.Projection(),
TableName: aws.String("Music"),
}
result, err := client.Query(context.TODO(), input)
if err != nil {
if apiErr := new(types.ProvisionedThroughputExceededException); errors.As(err, &apiErr) {
fmt.Println("throughput exceeded")
} else if apiErr := new(types.ResourceNotFoundException); errors.As(err, &apiErr) {
fmt.Println("resource not found")
} else if apiErr := new(types.InternalServerError); errors.As(err, &apiErr) {
fmt.Println("internal server error")
} else {
fmt.Println(err)
}
return
}
fmt.Println(result)
}
// Using Key Condition Expression
//
// This example queries items in the Music table. The table has a partition key and
// sort key (Artist and SongTitle), but this query only specifies the partition key
// value. It returns song titles by the artist named "No One You Know".
func ExampleBuilder_WithKeyCondition() {
config, err := config.LoadDefaultConfig(context.TODO())
if err != nil {
fmt.Println(err.Error())
return
}
client := dynamodb.NewFromConfig(config)
// Construct the Key condition builder
keyCond := expression.Key("Artist").Equal(expression.Value("No One You Know"))
// Create the project expression builder with a names list.
proj := expression.NamesList(expression.Name("SongTitle"))
// Combine the key condition, and projection together as a DynamoDB expression
// builder.
expr, err := expression.NewBuilder().
WithKeyCondition(keyCond).
WithProjection(proj).
Build()
if err != nil {
fmt.Println(err)
return
}
// Use the built expression to populate the DynamoDB Query's API input
// parameters.
input := &dynamodb.QueryInput{
ExpressionAttributeNames: expr.Names(),
ExpressionAttributeValues: expr.Values(),
KeyConditionExpression: expr.KeyCondition(),
ProjectionExpression: expr.Projection(),
TableName: aws.String("Music"),
}
result, err := client.Query(context.TODO(), input)
if err != nil {
if apiErr := new(types.ProvisionedThroughputExceededException); errors.As(err, &apiErr) {
fmt.Println("throughput exceeded")
} else if apiErr := new(types.ResourceNotFoundException); errors.As(err, &apiErr) {
fmt.Println("resource not found")
} else if apiErr := new(types.InternalServerError); errors.As(err, &apiErr) {
fmt.Println("internal server error")
} else {
fmt.Println(err)
}
return
}
fmt.Println(result)
}
// Using Filter Expression
//
// This example scans the entire Music table, and then narrows the results to songs
// by the artist "No One You Know". For each item, only the album title and song title
// are returned.
func ExampleBuilder_WithFilter() {
config, err := config.LoadDefaultConfig(context.TODO())
if err != nil {
fmt.Println(err.Error())
return
}
client := dynamodb.NewFromConfig(config)
// Construct the filter builder with a name and value.
filt := expression.Name("Artist").Equal(expression.Value("No One You Know"))
// Create the names list projection of names to project.
proj := expression.NamesList(
expression.Name("AlbumTitle"),
expression.Name("SongTitle"),
)
// Using the filter and projections create a DynamoDB expression from the two.
expr, err := expression.NewBuilder().
WithFilter(filt).
WithProjection(proj).
Build()
if err != nil {
fmt.Println(err)
return
}
// Use the built expression to populate the DynamoDB Scan API input parameters.
input := &dynamodb.ScanInput{
ExpressionAttributeNames: expr.Names(),
ExpressionAttributeValues: expr.Values(),
FilterExpression: expr.Filter(),
ProjectionExpression: expr.Projection(),
TableName: aws.String("Music"),
}
result, err := client.Scan(context.TODO(), input)
if err != nil {
if apiErr := new(types.ProvisionedThroughputExceededException); errors.As(err, &apiErr) {
fmt.Println("throughput exceeded")
} else if apiErr := new(types.ResourceNotFoundException); errors.As(err, &apiErr) {
fmt.Println("resource not found")
} else if apiErr := new(types.InternalServerError); errors.As(err, &apiErr) {
fmt.Println("internal server error")
} else {
fmt.Println(err)
}
return
}
fmt.Println(result)
}
// Using Update Expression
//
// This example updates an item in the Music table. It adds a new attribute (Year) and
// modifies the AlbumTitle attribute. All of the attributes in the item, as they appear
// after the update, are returned in the response.
func ExampleBuilder_WithUpdate() {
cfg, err := config.LoadDefaultConfig(context.TODO())
if err != nil {
fmt.Println(err.Error())
return
}
client := dynamodb.NewFromConfig(cfg)
// Create an update to set two fields in the table.
update := expression.Set(
expression.Name("Year"),
expression.Value(2015),
).Set(
expression.Name("AlbumTitle"),
expression.Value("Louder Than Ever"),
)
// Create the DynamoDB expression from the Update.
expr, err := expression.NewBuilder().
WithUpdate(update).
Build()
if err != nil {
fmt.Println(err)
return
}
// Use the built expression to populate the DynamoDB UpdateItem API
// input parameters.
input := &dynamodb.UpdateItemInput{
ExpressionAttributeNames: expr.Names(),
ExpressionAttributeValues: expr.Values(),
Key: map[string]types.AttributeValue{
"Artist": &types.AttributeValueMemberS{Value: "Acme Band"},
"SongTitle": &types.AttributeValueMemberS{Value: "Happy Day"},
},
ReturnValues: "ALL_NEW",
TableName: aws.String("Music"),
UpdateExpression: expr.Update(),
}
result, err := client.UpdateItem(context.TODO(), input)
if err != nil {
if apiErr := new(types.ProvisionedThroughputExceededException); errors.As(err, &apiErr) {
fmt.Println("throughput exceeded")
} else if apiErr := new(types.ResourceNotFoundException); errors.As(err, &apiErr) {
fmt.Println("resource not found")
} else if apiErr := new(types.InternalServerError); errors.As(err, &apiErr) {
fmt.Println("internal server error")
} else {
fmt.Println(err)
}
return
}
fmt.Println(result)
}
// Using Condition Expression
//
// This example deletes an item from the Music table if the rating is lower than
// 7.
func ExampleBuilder_WithCondition() {
cfg, err := config.LoadDefaultConfig(context.TODO())
if err != nil {
fmt.Println(err)
return
}
svc := dynamodb.NewFromConfig(cfg)
// Create a condition where the Rating field must be less than 7.
cond := expression.Name("Rating").LessThan(expression.Value(7))
// Create a DynamoDB expression from the condition.
expr, err := expression.NewBuilder().
WithCondition(cond).
Build()
if err != nil {
fmt.Println(err)
return
}
// Use the built expression to populate the DeleteItem API operation with the
// condition expression.
input := &dynamodb.DeleteItemInput{
Key: map[string]types.AttributeValue{
"Artist": &types.AttributeValueMemberS{Value: "No One You Know"},
"SongTitle": &types.AttributeValueMemberS{Value: "Scared of My Shadow"},
},
ExpressionAttributeNames: expr.Names(),
ExpressionAttributeValues: expr.Values(),
ConditionExpression: expr.Condition(),
TableName: aws.String("Music"),
}
result, err := svc.DeleteItem(context.TODO(), input)
if err != nil {
if apiErr := new(types.ProvisionedThroughputExceededException); errors.As(err, &apiErr) {
fmt.Println("throughput exceeded")
} else if apiErr := new(types.ResourceNotFoundException); errors.As(err, &apiErr) {
fmt.Println("resource not found")
} else if apiErr := new(types.InternalServerError); errors.As(err, &apiErr) {
fmt.Println("internal server error")
} else {
fmt.Println(err)
}
return
}
fmt.Println(result)
}
|