github.com/aavshr/aws-sdk-go@v1.41.3/service/dynamodb/expression/examples_test.go (about) 1 package expression_test 2 3 import ( 4 "fmt" 5 6 "github.com/aavshr/aws-sdk-go/aws" 7 "github.com/aavshr/aws-sdk-go/aws/awserr" 8 "github.com/aavshr/aws-sdk-go/aws/session" 9 "github.com/aavshr/aws-sdk-go/service/dynamodb" 10 "github.com/aavshr/aws-sdk-go/service/dynamodb/expression" 11 ) 12 13 // Using Projection Expression 14 // 15 // This example queries items in the Music table. The table has a partition key and 16 // sort key (Artist and SongTitle), but this query only specifies the partition key 17 // value. It returns song titles by the artist named "No One You Know". 18 func ExampleBuilder_WithProjection() { 19 svc := dynamodb.New(session.New()) 20 21 // Construct the Key condition builder 22 keyCond := expression.Key("Artist").Equal(expression.Value("No One You Know")) 23 24 // Create the project expression builder with a names list. 25 proj := expression.NamesList(expression.Name("SongTitle")) 26 27 // Combine the key condition, and projection together as a DynamoDB expression 28 // builder. 29 expr, err := expression.NewBuilder(). 30 WithKeyCondition(keyCond). 31 WithProjection(proj). 32 Build() 33 if err != nil { 34 fmt.Println(err) 35 } 36 37 // Use the built expression to populate the DynamoDB Query's API input 38 // parameters. 39 input := &dynamodb.QueryInput{ 40 ExpressionAttributeNames: expr.Names(), 41 ExpressionAttributeValues: expr.Values(), 42 KeyConditionExpression: expr.KeyCondition(), 43 ProjectionExpression: expr.Projection(), 44 TableName: aws.String("Music"), 45 } 46 47 result, err := svc.Query(input) 48 if err != nil { 49 if aerr, ok := err.(awserr.Error); ok { 50 switch aerr.Code() { 51 case dynamodb.ErrCodeProvisionedThroughputExceededException: 52 fmt.Println(dynamodb.ErrCodeProvisionedThroughputExceededException, aerr.Error()) 53 case dynamodb.ErrCodeResourceNotFoundException: 54 fmt.Println(dynamodb.ErrCodeResourceNotFoundException, aerr.Error()) 55 case dynamodb.ErrCodeInternalServerError: 56 fmt.Println(dynamodb.ErrCodeInternalServerError, aerr.Error()) 57 default: 58 fmt.Println(aerr.Error()) 59 } 60 } else { 61 // Print the error, cast err to awserr.Error to get the Code and 62 // Message from an error. 63 fmt.Println(err.Error()) 64 } 65 return 66 } 67 68 fmt.Println(result) 69 } 70 71 // Using Key Condition Expression 72 // 73 // This example queries items in the Music table. The table has a partition key and 74 // sort key (Artist and SongTitle), but this query only specifies the partition key 75 // value. It returns song titles by the artist named "No One You Know". 76 func ExampleBuilder_WithKeyCondition() { 77 svc := dynamodb.New(session.New()) 78 79 // Construct the Key condition builder 80 keyCond := expression.Key("Artist").Equal(expression.Value("No One You Know")) 81 82 // Create the project expression builder with a names list. 83 proj := expression.NamesList(expression.Name("SongTitle")) 84 85 // Combine the key condition, and projection together as a DynamoDB expression 86 // builder. 87 expr, err := expression.NewBuilder(). 88 WithKeyCondition(keyCond). 89 WithProjection(proj). 90 Build() 91 if err != nil { 92 fmt.Println(err) 93 } 94 95 // Use the built expression to populate the DynamoDB Query's API input 96 // parameters. 97 input := &dynamodb.QueryInput{ 98 ExpressionAttributeNames: expr.Names(), 99 ExpressionAttributeValues: expr.Values(), 100 KeyConditionExpression: expr.KeyCondition(), 101 ProjectionExpression: expr.Projection(), 102 TableName: aws.String("Music"), 103 } 104 105 result, err := svc.Query(input) 106 if err != nil { 107 if aerr, ok := err.(awserr.Error); ok { 108 switch aerr.Code() { 109 case dynamodb.ErrCodeProvisionedThroughputExceededException: 110 fmt.Println(dynamodb.ErrCodeProvisionedThroughputExceededException, aerr.Error()) 111 case dynamodb.ErrCodeResourceNotFoundException: 112 fmt.Println(dynamodb.ErrCodeResourceNotFoundException, aerr.Error()) 113 case dynamodb.ErrCodeInternalServerError: 114 fmt.Println(dynamodb.ErrCodeInternalServerError, aerr.Error()) 115 default: 116 fmt.Println(aerr.Error()) 117 } 118 } else { 119 // Print the error, cast err to awserr.Error to get the Code and 120 // Message from an error. 121 fmt.Println(err.Error()) 122 } 123 return 124 } 125 126 fmt.Println(result) 127 } 128 129 // Using Filter Expression 130 // 131 // This example scans the entire Music table, and then narrows the results to songs 132 // by the artist "No One You Know". For each item, only the album title and song title 133 // are returned. 134 func ExampleBuilder_WithFilter() { 135 svc := dynamodb.New(session.New()) 136 137 // Construct the filter builder with a name and value. 138 filt := expression.Name("Artist").Equal(expression.Value("No One You Know")) 139 140 // Create the names list projection of names to project. 141 proj := expression.NamesList( 142 expression.Name("AlbumTitle"), 143 expression.Name("SongTitle"), 144 ) 145 146 // Using the filter and projections create a DynamoDB expression from the two. 147 expr, err := expression.NewBuilder(). 148 WithFilter(filt). 149 WithProjection(proj). 150 Build() 151 if err != nil { 152 fmt.Println(err) 153 } 154 155 // Use the built expression to populate the DynamoDB Scan API input parameters. 156 input := &dynamodb.ScanInput{ 157 ExpressionAttributeNames: expr.Names(), 158 ExpressionAttributeValues: expr.Values(), 159 FilterExpression: expr.Filter(), 160 ProjectionExpression: expr.Projection(), 161 TableName: aws.String("Music"), 162 } 163 164 result, err := svc.Scan(input) 165 if err != nil { 166 if aerr, ok := err.(awserr.Error); ok { 167 switch aerr.Code() { 168 case dynamodb.ErrCodeProvisionedThroughputExceededException: 169 fmt.Println(dynamodb.ErrCodeProvisionedThroughputExceededException, aerr.Error()) 170 case dynamodb.ErrCodeResourceNotFoundException: 171 fmt.Println(dynamodb.ErrCodeResourceNotFoundException, aerr.Error()) 172 case dynamodb.ErrCodeInternalServerError: 173 fmt.Println(dynamodb.ErrCodeInternalServerError, aerr.Error()) 174 default: 175 fmt.Println(aerr.Error()) 176 } 177 } else { 178 // Print the error, cast err to awserr.Error to get the Code and 179 // Message from an error. 180 fmt.Println(err.Error()) 181 } 182 return 183 } 184 185 fmt.Println(result) 186 } 187 188 // Using Update Expression 189 // 190 // This example updates an item in the Music table. It adds a new attribute (Year) and 191 // modifies the AlbumTitle attribute. All of the attributes in the item, as they appear 192 // after the update, are returned in the response. 193 func ExampleBuilder_WithUpdate() { 194 svc := dynamodb.New(session.New()) 195 196 // Create an update to set two fields in the table. 197 update := expression.Set( 198 expression.Name("Year"), 199 expression.Value(2015), 200 ).Set( 201 expression.Name("AlbumTitle"), 202 expression.Value("Louder Than Ever"), 203 ) 204 205 // Create the DynamoDB expression from the Update. 206 expr, err := expression.NewBuilder(). 207 WithUpdate(update). 208 Build() 209 210 // Use the built expression to populate the DynamoDB UpdateItem API 211 // input parameters. 212 input := &dynamodb.UpdateItemInput{ 213 ExpressionAttributeNames: expr.Names(), 214 ExpressionAttributeValues: expr.Values(), 215 Key: map[string]*dynamodb.AttributeValue{ 216 "Artist": { 217 S: aws.String("Acme Band"), 218 }, 219 "SongTitle": { 220 S: aws.String("Happy Day"), 221 }, 222 }, 223 ReturnValues: aws.String("ALL_NEW"), 224 TableName: aws.String("Music"), 225 UpdateExpression: expr.Update(), 226 } 227 228 result, err := svc.UpdateItem(input) 229 if err != nil { 230 if aerr, ok := err.(awserr.Error); ok { 231 switch aerr.Code() { 232 case dynamodb.ErrCodeConditionalCheckFailedException: 233 fmt.Println(dynamodb.ErrCodeConditionalCheckFailedException, aerr.Error()) 234 case dynamodb.ErrCodeProvisionedThroughputExceededException: 235 fmt.Println(dynamodb.ErrCodeProvisionedThroughputExceededException, aerr.Error()) 236 case dynamodb.ErrCodeResourceNotFoundException: 237 fmt.Println(dynamodb.ErrCodeResourceNotFoundException, aerr.Error()) 238 case dynamodb.ErrCodeItemCollectionSizeLimitExceededException: 239 fmt.Println(dynamodb.ErrCodeItemCollectionSizeLimitExceededException, aerr.Error()) 240 case dynamodb.ErrCodeInternalServerError: 241 fmt.Println(dynamodb.ErrCodeInternalServerError, aerr.Error()) 242 default: 243 fmt.Println(aerr.Error()) 244 } 245 } else { 246 // Print the error, cast err to awserr.Error to get the Code and 247 // Message from an error. 248 fmt.Println(err.Error()) 249 } 250 return 251 } 252 253 fmt.Println(result) 254 } 255 256 // Using Condition Expression 257 // 258 // This example deletes an item from the Music table if the rating is lower than 259 // 7. 260 func ExampleBuilder_WithCondition() { 261 svc := dynamodb.New(session.New()) 262 263 // Create a condition where the Rating field must be less than 7. 264 cond := expression.Name("Rating").LessThan(expression.Value(7)) 265 266 // Create a DynamoDB expression from the condition. 267 expr, err := expression.NewBuilder(). 268 WithCondition(cond). 269 Build() 270 if err != nil { 271 fmt.Println(err) 272 } 273 274 // Use the built expression to populate the DeleteItem API operation with the 275 // condition expression. 276 input := &dynamodb.DeleteItemInput{ 277 Key: map[string]*dynamodb.AttributeValue{ 278 "Artist": { 279 S: aws.String("No One You Know"), 280 }, 281 "SongTitle": { 282 S: aws.String("Scared of My Shadow"), 283 }, 284 }, 285 ExpressionAttributeNames: expr.Names(), 286 ExpressionAttributeValues: expr.Values(), 287 ConditionExpression: expr.Condition(), 288 TableName: aws.String("Music"), 289 } 290 291 result, err := svc.DeleteItem(input) 292 if err != nil { 293 if aerr, ok := err.(awserr.Error); ok { 294 switch aerr.Code() { 295 case dynamodb.ErrCodeConditionalCheckFailedException: 296 fmt.Println(dynamodb.ErrCodeConditionalCheckFailedException, aerr.Error()) 297 case dynamodb.ErrCodeProvisionedThroughputExceededException: 298 fmt.Println(dynamodb.ErrCodeProvisionedThroughputExceededException, aerr.Error()) 299 case dynamodb.ErrCodeResourceNotFoundException: 300 fmt.Println(dynamodb.ErrCodeResourceNotFoundException, aerr.Error()) 301 case dynamodb.ErrCodeItemCollectionSizeLimitExceededException: 302 fmt.Println(dynamodb.ErrCodeItemCollectionSizeLimitExceededException, aerr.Error()) 303 case dynamodb.ErrCodeInternalServerError: 304 fmt.Println(dynamodb.ErrCodeInternalServerError, aerr.Error()) 305 default: 306 fmt.Println(aerr.Error()) 307 } 308 } else { 309 // Print the error, cast err to awserr.Error to get the Code and 310 // Message from an error. 311 fmt.Println(err.Error()) 312 } 313 return 314 } 315 316 fmt.Println(result) 317 }