Skip to content

Commit 75f909d

Browse files
committed
DOC-8512 -- Apply swift code feedback (couchbase#471) (couchbase#475)
https://issues.couchbase.com/browse/DOC-8512 (cherry picked from commit 5020a04)
1 parent 098db47 commit 75f909d

File tree

1 file changed

+44
-43
lines changed

1 file changed

+44
-43
lines changed

modules/swift/examples/code_snippets/SampleCodeTest.swift

Lines changed: 44 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2114,7 +2114,10 @@ class QueryResultSets {
21142114
// seedHotel()
21152115

21162116
// QUERY RESULT SET HANDLING EXAMPLES
2117-
// tag::query-syntax-all[
2117+
// tag::query-syntax-all[]
2118+
let db = try! Database(name: "hotel")
2119+
var hotels = [String:Any]()
2120+
var hotel:Hotel = Hotel.init()
21182121

21192122
let listQuery = QueryBuilder.select(SelectResult.all())
21202123
.from(DataSource.database( db))
@@ -2125,37 +2128,27 @@ class QueryResultSets {
21252128

21262129
do {
21272130

2128-
for (row) in try! listQuery.execute() {
2131+
for row in try! listQuery.execute() {
21292132

2130-
print(row.toDictionary())
2133+
let thisDocsProps =
2134+
row.dictionary(at: 0)?.toDictionary() // <.>
21312135

2132-
if let hotel = row.dictionary(forKey: "_doc") {
2136+
let docid = thisDocsProps!["id"] as! String
21332137

2134-
let hotelid = hotel["id"].string!
2138+
let name = thisDocsProps!["name"] as! String
21352139

2136-
hotels[hotelid] = hotel.toDictionary()
2140+
let type = thisDocsProps!["type"] as! String
21372141

2142+
let city = thisDocsProps!["city"] as! String
21382143

2139-
if let thisDocsProperties = hotel.toDictionary() as? [String:Any] {
2144+
let hotel = row.dictionary(at: 0)?.toDictionary() //<.>
21402145

2141-
let docid = thisDocsProperties["id"] as! String
2146+
let hotelId = hotel!["id"] as! String
21422147

2143-
let name = thisDocsProperties["name"] as! String
2144-
2145-
let type = thisDocsProperties["type"] as! String
2146-
2147-
let city = thisDocsProperties["city"] as! String
2148-
2149-
print("thisDocsProperties are: ", docid,name,type,city)
2150-
2151-
} // end if <.>
2152-
2153-
} // end if
2148+
hotels[hotelId] = hotel
21542149

21552150
} // end for
2156-
} catch let err {
2157-
print(err.localizedDescription)
2158-
// ... handle errors as required
2151+
21592152
} //end do-block
21602153

21612154
// end::query-access-all[]
@@ -2199,6 +2192,10 @@ class QueryResultSets {
21992192
//
22002193
func dontTestQueryProps () throws {
22012194
// tag::query-syntax-props[]
2195+
let db = try! Database(name: "hotel")
2196+
var hotels = [String:Any]()
2197+
var hotel:Hotel = Hotel.init()
2198+
22022199
let listQuery = QueryBuilder
22032200
.select(SelectResult.expression(Meta.id).as("metaId"),
22042201
SelectResult.expression(Expression.property("id")),
@@ -2212,32 +2209,33 @@ class QueryResultSets {
22122209
// tag::query-access-props[]
22132210
for (_, result) in try! listQuery.execute().enumerated() {
22142211

2215-
print(result.toDictionary())
2216-
2217-
if let thisDoc = result.toDictionary() as? [String:Any] {
2218-
2219-
let docid = thisDoc["metaId"] as! String
22202212

2221-
let hotelId = thisDoc["id"] as! String
2213+
let thisDoc = result.toDictionary() as? [String:Any] // <.>
2214+
// Store dictionary data in hotel object and save in array
2215+
hotel.id = thisDoc!["id"] as! String
2216+
hotel.name = thisDoc!["name"] as! String
2217+
hotel.city = thisDoc!["city"] as! String
2218+
hotel.type = thisDoc!["type"] as! String
2219+
hotels[hotel.id] = hotel
22222220

2223-
let name = thisDoc["name"] as! String
2224-
2225-
let city = thisDoc["city"] as! String
2226-
2227-
let type = thisDoc["type"] as! String
2228-
2229-
// ... process document properties as required
2230-
print("Result properties are: ", docid, hotelId,name, city, type)
2231-
}
2232-
}
2221+
// Use result content directly
2222+
let docid = result.string(forKey: "metaId")
2223+
let hotelId = result.string(forKey: "id")
2224+
let name = result.string(forKey: "name")
2225+
let city = result.string(forKey: "city")
2226+
let type = result.string(forKey: "type")
22332227

2228+
// ... process document properties as required
2229+
print("Result properties are: ", docid, hotelId,name, city, type)
2230+
} // end for
22342231
// end::query-access-props[]
22352232
}
22362233

22372234
//
22382235
func dontTestQueryCount () throws {
22392236

22402237
// tag::query-syntax-count-only[]
2238+
let db = try! Database(name: "hotel")
22412239
do {
22422240
let listQuery = QueryBuilder
22432241
.select(SelectResult.expression(Function.count(Expression.all())).as("mycount"))
@@ -2247,13 +2245,15 @@ class QueryResultSets {
22472245

22482246

22492247
// tag::query-access-count-only[]
2248+
22502249
for result in try! listQuery.execute() {
2251-
if let dict = result.toDictionary() as? [String: Int] {
2252-
let thiscount = dict["mycount"]! // <.>
2253-
print("There are ", thiscount, " rows")
2254-
}
2255-
}
2250+
let dict = result.toDictionary() as? [String: Int]
2251+
let thiscount = dict!["mycount"]! // <.>
2252+
print("There are ", thiscount, " rows")
2253+
} // end for
2254+
22562255
} // end do
2256+
22572257
} // end function
22582258

22592259
// end::query-access-count-only[]
@@ -2262,6 +2262,7 @@ class QueryResultSets {
22622262
func dontTestQueryId () throws {
22632263

22642264
// tag::query-syntax-id[]
2265+
let db = try! Database(name: "hotel")
22652266
let listQuery = QueryBuilder.select(SelectResult.expression(Meta.id).as("metaId"))
22662267
.from(DataSource.database(db))
22672268

0 commit comments

Comments
 (0)