Skip to content
Snippets Groups Projects
Commit af9b26e2 authored by Dariusz Rybicki's avatar Dariusz Rybicki
Browse files

Refactor Fact properties

parent 4fef1f7f
No related branches found
No related tags found
2 merge requests!102Release 1.0.0,!71Fact improvements & helpers
...@@ -2,21 +2,21 @@ import Foundation ...@@ -2,21 +2,21 @@ import Foundation
public struct Fact: Equatable { public struct Fact: Equatable {
public init( public init(
fact: String, type: FactType,
type: FactType value: String
) { ) {
self.fact = fact
self.type = type self.type = type
self.value = value
} }
public var fact: String
public var type: FactType public var type: FactType
public var value: String
} }
extension Fact: Codable { extension Fact: Codable {
enum CodingKeys: String, CodingKey { enum CodingKeys: String, CodingKey {
case fact = "Fact"
case type = "T" case type = "T"
case value = "Fact"
} }
public static func decode(_ data: Data) throws -> Self { public static func decode(_ data: Data) throws -> Self {
...@@ -28,6 +28,21 @@ extension Fact: Codable { ...@@ -28,6 +28,21 @@ extension Fact: Codable {
} }
} }
extension Fact {
@available(iOS, deprecated: 9999.0, message: "This API has been soft-deprecated in favor of `Fact.init(type:value:)`.")
@available(macOS, deprecated: 9999.0, message: "This API has been soft-deprecated in favor of `Fact.init(type:value:)`.")
public init(fact: String, type: Int) {
self.init(type: .init(rawValue: type), value: fact)
}
@available(iOS, deprecated: 9999.0, message: "This API has been soft-deprecated in favor of `Fact.value`.")
@available(macOS, deprecated: 9999.0, message: "This API has been soft-deprecated in favor of `Fact.value`.")
public var fact: String {
get { value }
set { value = newValue }
}
}
extension Array where Element == Fact { extension Array where Element == Fact {
public static func decode(_ data: Data) throws -> Self { public static func decode(_ data: Data) throws -> Self {
if let string = String(data: data, encoding: .utf8), string == "null" { if let string = String(data: data, encoding: .utf8), string == "null" {
...@@ -52,7 +67,7 @@ extension Array where Element == Fact { ...@@ -52,7 +67,7 @@ extension Array where Element == Fact {
public mutating func set(_ type: FactType, _ value: String?) { public mutating func set(_ type: FactType, _ value: String?) {
removeAll(where: { $0.type == type }) removeAll(where: { $0.type == type })
if let value = value { if let value = value {
append(Fact(fact: value, type: type)) append(Fact(type: type, value: value))
sort(by: { $0.type < $1.type }) sort(by: { $0.type < $1.type })
} }
} }
......
...@@ -74,13 +74,13 @@ extension MessengerSearchUsers.Query { ...@@ -74,13 +74,13 @@ extension MessengerSearchUsers.Query {
var facts: [Fact] { var facts: [Fact] {
var facts: [Fact] = [] var facts: [Fact] = []
if let username = username, username.isEmpty == false { if let username = username, username.isEmpty == false {
facts.append(Fact(fact: username, type: 0)) facts.set(.username, username)
} }
if let email = email, email.isEmpty == false { if let email = email, email.isEmpty == false {
facts.append(Fact(fact: email, type: 1)) facts.set(.email, email)
} }
if let phone = phone, phone.isEmpty == false { if let phone = phone, phone.isEmpty == false {
facts.append(Fact(fact: phone, type: 2)) facts.set(.phone, phone)
} }
return facts return facts
} }
......
...@@ -7,10 +7,10 @@ final class ContactTests: XCTestCase { ...@@ -7,10 +7,10 @@ final class ContactTests: XCTestCase {
var contact = Contact.unimplemented("contact-data".data(using: .utf8)!) var contact = Contact.unimplemented("contact-data".data(using: .utf8)!)
contact.getFactsFromContact.run = { _ in contact.getFactsFromContact.run = { _ in
[ [
Fact(fact: "username", type: 0), Fact(type: .username, value: "username"),
Fact(fact: "email", type: 1), Fact(type: .email, value: "email"),
Fact(fact: "phone", type: 2), Fact(type: .phone, value: "phone"),
Fact(fact: "other", type: 3), Fact(type: .other(3), value: "other"),
] ]
} }
...@@ -23,10 +23,10 @@ final class ContactTests: XCTestCase { ...@@ -23,10 +23,10 @@ final class ContactTests: XCTestCase {
try contact.getFact(.other(4)), try contact.getFact(.other(4)),
], ],
[ [
Fact(fact: "username", type: 0), Fact(type: .username, value: "username"),
Fact(fact: "email", type: 1), Fact(type: .email, value: "email"),
Fact(fact: "phone", type: 2), Fact(type: .phone, value: "phone"),
Fact(fact: "other", type: 3), Fact(type: .other(3), value: "other"),
nil nil
] ]
) )
...@@ -35,10 +35,10 @@ final class ContactTests: XCTestCase { ...@@ -35,10 +35,10 @@ final class ContactTests: XCTestCase {
func testSetFact() throws { func testSetFact() throws {
var contact = Contact.unimplemented("contact-data".data(using: .utf8)!) var contact = Contact.unimplemented("contact-data".data(using: .utf8)!)
var facts: [Fact] = [ var facts: [Fact] = [
Fact(fact: "username", type: 0), Fact(type: .username, value: "username"),
Fact(fact: "email", type: 1), Fact(type: .email, value: "email"),
Fact(fact: "phone", type: 2), Fact(type: .phone, value: "phone"),
Fact(fact: "other-3", type: 3), Fact(type: .other(3), value: "other-3"),
] ]
contact.getFactsFromContact.run = { _ in facts } contact.getFactsFromContact.run = { _ in facts }
contact.setFactsOnContact.run = { data, newFacts in contact.setFactsOnContact.run = { data, newFacts in
...@@ -52,10 +52,10 @@ final class ContactTests: XCTestCase { ...@@ -52,10 +52,10 @@ final class ContactTests: XCTestCase {
try contact.setFact(.email, nil) try contact.setFact(.email, nil)
XCTAssertNoDifference(facts, [ XCTAssertNoDifference(facts, [
Fact(fact: "new-username", type: 0), Fact(type: .username, value: "new-username"),
Fact(fact: "phone", type: 2), Fact(type: .phone, value: "phone"),
Fact(fact: "new-other-3", type: 3), Fact(type: .other(3), value: "new-other-3"),
Fact(fact: "new-other-4", type: 4), Fact(type: .other(4), value: "new-other-4"),
]) ])
} }
} }
...@@ -16,8 +16,8 @@ final class FactTests: XCTestCase { ...@@ -16,8 +16,8 @@ final class FactTests: XCTestCase {
let model = try Fact.decode(jsonData) let model = try Fact.decode(jsonData)
XCTAssertNoDifference(model, Fact( XCTAssertNoDifference(model, Fact(
fact: factValue, type: .other(123),
type: 123 value: factValue
)) ))
let encodedModel = try model.encode() let encodedModel = try model.encode()
...@@ -28,9 +28,9 @@ final class FactTests: XCTestCase { ...@@ -28,9 +28,9 @@ final class FactTests: XCTestCase {
func testCodingArray() throws { func testCodingArray() throws {
let models = [ let models = [
Fact(fact: "abcd", type: 0), Fact(type: .username, value: "abcd"),
Fact(fact: "efgh", type: 1), Fact(type: .email, value: "efgh"),
Fact(fact: "ijkl", type: 2), Fact(type: .phone, value: "ijkl"),
] ]
let encodedModels = try models.encode() let encodedModels = try models.encode()
...@@ -54,10 +54,10 @@ final class FactTests: XCTestCase { ...@@ -54,10 +54,10 @@ final class FactTests: XCTestCase {
func testArrayGetter() { func testArrayGetter() {
let facts = [ let facts = [
Fact(fact: "username", type: 0), Fact(type: .username, value: "username"),
Fact(fact: "email", type: 1), Fact(type: .email, value: "email"),
Fact(fact: "phone", type: 2), Fact(type: .phone, value: "phone"),
Fact(fact: "other", type: 3), Fact(type: .other(3), value: "other"),
] ]
XCTAssertNoDifference( XCTAssertNoDifference(
...@@ -69,10 +69,10 @@ final class FactTests: XCTestCase { ...@@ -69,10 +69,10 @@ final class FactTests: XCTestCase {
facts.get(.other(4)), facts.get(.other(4)),
], ],
[ [
Fact(fact: "username", type: 0), Fact(type: .username, value: "username"),
Fact(fact: "email", type: 1), Fact(type: .email, value: "email"),
Fact(fact: "phone", type: 2), Fact(type: .phone, value: "phone"),
Fact(fact: "other", type: 3), Fact(type: .other(3), value: "other"),
nil nil
] ]
) )
...@@ -89,10 +89,10 @@ final class FactTests: XCTestCase { ...@@ -89,10 +89,10 @@ final class FactTests: XCTestCase {
XCTAssertNoDifference( XCTAssertNoDifference(
facts, facts,
[ [
Fact(fact: "username", type: 0), Fact(type: .username, value: "username"),
Fact(fact: "email", type: 1), Fact(type: .email, value: "email"),
Fact(fact: "phone", type: 2), Fact(type: .phone, value: "phone"),
Fact(fact: "other", type: 3), Fact(type: .other(3), value: "other"),
] ]
) )
} }
......
...@@ -181,9 +181,9 @@ final class MessengerSearchUsersTests: XCTestCase { ...@@ -181,9 +181,9 @@ final class MessengerSearchUsersTests: XCTestCase {
phone: "phone" phone: "phone"
).facts, ).facts,
[ [
Fact(fact: "username", type: 0), Fact(type: .username, value: "username"),
Fact(fact: "email", type: 1), Fact(type: .email, value: "email"),
Fact(fact: "phone", type: 2), Fact(type: .phone, value: "phone"),
] ]
) )
...@@ -194,7 +194,7 @@ final class MessengerSearchUsersTests: XCTestCase { ...@@ -194,7 +194,7 @@ final class MessengerSearchUsersTests: XCTestCase {
phone: nil phone: nil
).facts, ).facts,
[ [
Fact(fact: "username", type: 0), Fact(type: .username, value: "username"),
] ]
) )
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment