Skip to content

Commit 9fe0a26

Browse files
authored
Add BatchGetItem sample for Swift (#4539)
* Add BatchGetItem sample for Swift In addition to demonstrating the use of BatchGetItem, this commit updates the metadata and README to include this function. Also included is a `.gitignore` file that prevents Swift's `.build` directories from being added to the repository by accident.
1 parent 413dc5f commit 9fe0a26

File tree

9 files changed

+1211
-5
lines changed

9 files changed

+1211
-5
lines changed

.doc_gen/metadata/dynamodb_metadata.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,14 @@ dynamodb_BatchGetItem:
166166
- description:
167167
snippet_tags:
168168
- cpp.example_code.dynamodb.batch_get_item
169+
Swift:
170+
versions:
171+
- sdk_version: 1
172+
github: swift/example_code/ddb
173+
excerpts:
174+
- description:
175+
snippet_tags:
176+
- ddb.swift.batchgetitem.batchget
169177
services:
170178
dynamodb: {BatchGetItem}
171179
dynamodb_DescribeTable:

swift/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
**/.build
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// swift-tools-version:5.6
2+
// The swift-tools-version declares the minimum version of Swift required to
3+
// build this package.
4+
//
5+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
6+
// SPDX-License-Identifier: Apache-2.0
7+
8+
import PackageDescription
9+
10+
let package = Package(
11+
// snippet-start:[ddb.swift.batchgetitem.package.attributes]
12+
name: "batchgetitem",
13+
platforms: [
14+
.macOS(.v11),
15+
.iOS(.v13)
16+
],
17+
// snippet-end:[ddb.swift.batchgetitem.package.attributes]
18+
// snippet-start:[ddb.swift.batchgetitem.package.dependencies]
19+
dependencies: [
20+
// Dependencies declare other packages that this package depends on.
21+
.package(
22+
url: "https://github.com/awslabs/aws-sdk-swift",
23+
from: "0.10.0"
24+
),
25+
.package(
26+
url: "https://github.com/apple/swift-argument-parser.git",
27+
branch: "main"
28+
),
29+
.package(
30+
name: "SwiftUtilities",
31+
path: "../../../modules/SwiftUtilities"
32+
),
33+
],
34+
// snippet-end:[ddb.swift.batchgetitem.package.dependencies]
35+
// snippet-start:[ddb.swift.batchgetitem.package.targets]
36+
targets: [
37+
// A target defines a module or a test suite. A target can depend on
38+
// other targets in this package. They can also depend on products in
39+
// other packages that this package depends on.
40+
// snippet-start:[ddb.swift.batchgetitem.package.target.executable]
41+
.executableTarget(
42+
name: "batchgetitem",
43+
dependencies: [
44+
.product(name: "AWSDynamoDB", package: "aws-sdk-swift"),
45+
.product(name: "ArgumentParser", package: "swift-argument-parser"),
46+
"SwiftUtilities",
47+
],
48+
path: "./Sources"
49+
),
50+
// snippet-end:[ddb.swift.batchgetitem.package.target.executable]
51+
// snippet-start:[ddb.swift.batchgetitem.package.target.tests]
52+
.testTarget(
53+
name: "batchgetitem-tests",
54+
dependencies: [
55+
.product(name: "AWSDynamoDB", package: "aws-sdk-swift"),
56+
"batchgetitem",
57+
"SwiftUtilities"
58+
],
59+
path: "./Tests"
60+
)
61+
// snippet-end:[ddb.swift.batchgetitem.package.target.tests]
62+
]
63+
// snippet-end:[ddb.swift.batchgetitem.package.targets]
64+
)
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
// A structure to represent the information about a movie.
2+
//
3+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
4+
// SPDX-License-Identifier: Apache-2.0
5+
6+
// snippet-start:[ddb.swift.batchgetitem.movie]
7+
import Foundation
8+
import AWSDynamoDB
9+
10+
// snippet-start:[ddb.swift.batchgetitem.info]
11+
/// The optional details about a movie.
12+
public struct Info: Codable {
13+
/// The movie's rating, if available.
14+
var rating: Double?
15+
/// The movie's plot, if available.
16+
var plot: String?
17+
}
18+
// snippet-end:[ddb.swift.batchgetitem.info]
19+
20+
public struct Movie: Codable {
21+
/// The year in which the movie was released.
22+
var year: Int
23+
/// The movie's title.
24+
var title: String
25+
/// An `Info` object providing the optional movie rating and plot
26+
/// information.
27+
var info: Info
28+
29+
// snippet-start:[ddb.swift.batchgetitem.movie.init]
30+
/// Create a `Movie` object representing a movie, given the movie's
31+
/// details.
32+
///
33+
/// - Parameters:
34+
/// - title: The movie's title (`String`).
35+
/// - year: The year in which the movie was released (`Int`).
36+
/// - rating: The movie's rating (optional `Double`).
37+
/// - plot: The movie's plot (optional `String`).
38+
init(title: String, year: Int, rating: Double? = nil, plot: String? = nil) {
39+
self.title = title
40+
self.year = year
41+
42+
self.info = Info(rating: rating, plot: plot)
43+
}
44+
// snippet-end:[ddb.swift.batchgetitem.movie.init]
45+
46+
// snippet-start:[ddb.swift.batchgetitem.movie.init-info]
47+
/// Create a `Movie` object representing a movie, given the movie's
48+
/// details.
49+
///
50+
/// - Parameters:
51+
/// - title: The movie's title (`String`).
52+
/// - year: The year in which the movie was released (`Int`).
53+
/// - info: The optional rating and plot information for the movie in an
54+
/// `Info` object.
55+
init(title: String, year: Int, info: Info?) {
56+
self.title = title
57+
self.year = year
58+
59+
if info != nil {
60+
self.info = info!
61+
} else {
62+
self.info = Info(rating: nil, plot: nil)
63+
}
64+
}
65+
// snippet-end:[ddb.swift.batchgetitem.movie.init-info]
66+
67+
// snippet-start:[ddb.swift.batchgetitem.movie.init-withitem]
68+
///
69+
/// Return a new `MovieTable` object, given an array mapping string to Amazon
70+
/// DynamoDB attribute values.
71+
///
72+
/// - Parameter item: The item information provided in the form used by
73+
/// DynamoDB. This is an array of strings mapped to
74+
/// `DynamoDBClientTypes.AttributeValue` values.
75+
init(withItem item: [Swift.String:DynamoDBClientTypes.AttributeValue]) throws {
76+
// Read the attributes.
77+
78+
guard let titleAttr = item["title"],
79+
let yearAttr = item["year"] else {
80+
throw MovieError.ItemNotFound
81+
}
82+
let infoAttr = item["info"] ?? nil
83+
84+
// Extract the values of the title and year attributes.
85+
86+
if case .s(let titleVal) = titleAttr {
87+
self.title = titleVal
88+
} else {
89+
throw MovieError.InvalidAttributes
90+
}
91+
92+
if case .n(let yearVal) = yearAttr {
93+
self.year = Int(yearVal)!
94+
} else {
95+
throw MovieError.InvalidAttributes
96+
}
97+
98+
// Extract the rating and/or plot from the `info` attribute, if
99+
// they're present.
100+
101+
var rating: Double? = nil
102+
var plot: String? = nil
103+
104+
if case .m(let infoVal) = infoAttr {
105+
let ratingAttr = infoVal["rating"] ?? nil
106+
let plotAttr = infoVal["plot"] ?? nil
107+
108+
if ratingAttr != nil, case .n(let ratingVal) = ratingAttr {
109+
rating = Double(ratingVal) ?? nil
110+
}
111+
if plotAttr != nil, case .s(let plotVal) = plotAttr {
112+
plot = plotVal
113+
}
114+
}
115+
116+
self.info = Info(rating: rating, plot: plot)
117+
}
118+
// snippet-end:[ddb.swift.batchgetitem.movie.init-withitem]
119+
120+
// snippet-start:[ddb.swift.batchgetitem.movie.getasitem]
121+
///
122+
/// Return an array mapping attribute names to Amazon DynamoDB attribute
123+
/// values, representing the contents of the `Movie` record as a DynamoDB
124+
/// item.
125+
///
126+
/// - Returns: The movie item as an array of type
127+
/// `[Swift.String:DynamoDBClientTypes.AttributeValue]`.
128+
///
129+
func getAsItem() async throws -> [Swift.String:DynamoDBClientTypes.AttributeValue] {
130+
// Build the item record, starting with the year and title, which are
131+
// always present.
132+
133+
var item: [Swift.String:DynamoDBClientTypes.AttributeValue] = [
134+
"year": .n(String(self.year)),
135+
"title": .s(self.title)
136+
]
137+
138+
// Add the `info` field with the rating and/or plot if they're
139+
// available.
140+
141+
var info: [Swift.String:DynamoDBClientTypes.AttributeValue] = [:]
142+
if self.info.rating != nil {
143+
info["rating"] = .n(String(self.info.rating!))
144+
}
145+
if self.info.plot != nil {
146+
info["plot"] = .s(self.info.plot!)
147+
}
148+
item["info"] = .m(info)
149+
150+
return item
151+
}
152+
// snippet-end:[ddb.swift.batchgetitem.movie.getasitem]
153+
}
154+
// snippet-end:[ddb.swift.batchgetitem.movie]

0 commit comments

Comments
 (0)