Fortune.swift 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Copyright IBM Corporation 2018
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import Foundation
  17. public struct Fortune: Codable {
  18. /// The id of this Fortune
  19. public let id: Int
  20. /// The message contained within this Fortune
  21. public let message: String
  22. public init(id: Int, message: String) {
  23. self.id = id
  24. self.message = message
  25. }
  26. /// Create a Fortune instance from a [String: Any?] dictionary,
  27. /// such as that retrieved by Kuery using `QueryResult.asRows()`.
  28. ///
  29. /// - Parameter row: A dictionary representing the fields of a
  30. /// Fortune database row.
  31. /// - throws: if the fields and types contained in the dictionary
  32. /// do not match those expected.
  33. public init(row: [String:Any?]) throws {
  34. guard let idField = row["id"] else {
  35. throw AppError.DataFormatError("Missing 'id' field")
  36. }
  37. guard let msgField = row["message"] else {
  38. throw AppError.DataFormatError("Missing 'message' field")
  39. }
  40. guard let message = msgField as? String else {
  41. throw AppError.DataFormatError("'message' field not a String")
  42. }
  43. guard let id = idField as? Int32 else {
  44. throw AppError.DataFormatError("'id' field not an Int32")
  45. }
  46. self.init(id: Int(id), message: message)
  47. }
  48. /// Create a Fortune instance from an [Any?] array, such as that retrieved
  49. /// by Kuery using `ResultSet.forEach()`.
  50. ///
  51. /// - Parameter row: An array representing the fields of a Fortune
  52. /// database row.
  53. /// - throws: if the fields and types contained in the array do not match
  54. /// those expected.
  55. public init(values: [Any?]) throws {
  56. // There should be two columns
  57. guard values.count == 2 else {
  58. throw AppError.DBKueryError("Expected 2 values but found \(values.count)")
  59. }
  60. // First should be an Int32
  61. guard let id = values[0] as? Int32 else {
  62. throw AppError.DataFormatError("Fortune id '\(String(describing: values[0]))' is not an Int")
  63. }
  64. // Second should be a String
  65. guard let msg = values[1] as? String else {
  66. throw AppError.DataFormatError("Fortune message '\(String(describing: values[1]))' is not a String")
  67. }
  68. self.init(id: Int(id), message: msg)
  69. }
  70. }
  71. extension Fortune: Comparable {
  72. public static func == (lhs: Fortune, rhs: Fortune) -> Bool {
  73. return lhs.id == rhs.id && lhs.message == rhs.message
  74. }
  75. public static func < (lhs: Fortune, rhs: Fortune) -> Bool {
  76. return lhs.message < rhs.message || (lhs.message == rhs.message && lhs.id < rhs.id)
  77. }
  78. }