examples_test.go 9.8 KB

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