diff --git a/Sources/XXClient/Models/Fact.swift b/Sources/XXClient/Models/Fact.swift index 4fb4134c2b440208f2043bc87578cb41c7d9d434..8b2de9f79e5aaa88bb6eb3f3e94ae592c6d50930 100644 --- a/Sources/XXClient/Models/Fact.swift +++ b/Sources/XXClient/Models/Fact.swift @@ -2,21 +2,21 @@ import Foundation public struct Fact: Equatable { public init( - fact: String, - type: FactType + type: FactType, + value: String ) { - self.fact = fact self.type = type + self.value = value } - public var fact: String public var type: FactType + public var value: String } extension Fact: Codable { enum CodingKeys: String, CodingKey { - case fact = "Fact" case type = "T" + case value = "Fact" } public static func decode(_ data: Data) throws -> Self { @@ -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 { public static func decode(_ data: Data) throws -> Self { if let string = String(data: data, encoding: .utf8), string == "null" { @@ -52,7 +67,7 @@ extension Array where Element == Fact { public mutating func set(_ type: FactType, _ value: String?) { removeAll(where: { $0.type == type }) if let value = value { - append(Fact(fact: value, type: type)) + append(Fact(type: type, value: value)) sort(by: { $0.type < $1.type }) } } diff --git a/Sources/XXMessengerClient/Messenger/Functions/MessengerSearchUsers.swift b/Sources/XXMessengerClient/Messenger/Functions/MessengerSearchUsers.swift index a24a7652de827130a01f145c7df99dcdf7d172dd..5acd76d7c9b05d2c4eee1c01effdbefbcd4be3a7 100644 --- a/Sources/XXMessengerClient/Messenger/Functions/MessengerSearchUsers.swift +++ b/Sources/XXMessengerClient/Messenger/Functions/MessengerSearchUsers.swift @@ -74,13 +74,13 @@ extension MessengerSearchUsers.Query { var facts: [Fact] { var facts: [Fact] = [] 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 { - facts.append(Fact(fact: email, type: 1)) + facts.set(.email, email) } if let phone = phone, phone.isEmpty == false { - facts.append(Fact(fact: phone, type: 2)) + facts.set(.phone, phone) } return facts } diff --git a/Tests/XXClientTests/Models/ContactTests.swift b/Tests/XXClientTests/Models/ContactTests.swift index 8347e0dc6ac997147e11171ceb77869f45f1cb8e..91dc41491650e15caa5b19bbbcd4b03ed26b37c4 100644 --- a/Tests/XXClientTests/Models/ContactTests.swift +++ b/Tests/XXClientTests/Models/ContactTests.swift @@ -7,10 +7,10 @@ final class ContactTests: XCTestCase { var contact = Contact.unimplemented("contact-data".data(using: .utf8)!) contact.getFactsFromContact.run = { _ in [ - Fact(fact: "username", type: 0), - Fact(fact: "email", type: 1), - Fact(fact: "phone", type: 2), - Fact(fact: "other", type: 3), + Fact(type: .username, value: "username"), + Fact(type: .email, value: "email"), + Fact(type: .phone, value: "phone"), + Fact(type: .other(3), value: "other"), ] } @@ -23,10 +23,10 @@ final class ContactTests: XCTestCase { try contact.getFact(.other(4)), ], [ - Fact(fact: "username", type: 0), - Fact(fact: "email", type: 1), - Fact(fact: "phone", type: 2), - Fact(fact: "other", type: 3), + Fact(type: .username, value: "username"), + Fact(type: .email, value: "email"), + Fact(type: .phone, value: "phone"), + Fact(type: .other(3), value: "other"), nil ] ) @@ -35,10 +35,10 @@ final class ContactTests: XCTestCase { func testSetFact() throws { var contact = Contact.unimplemented("contact-data".data(using: .utf8)!) var facts: [Fact] = [ - Fact(fact: "username", type: 0), - Fact(fact: "email", type: 1), - Fact(fact: "phone", type: 2), - Fact(fact: "other-3", type: 3), + Fact(type: .username, value: "username"), + Fact(type: .email, value: "email"), + Fact(type: .phone, value: "phone"), + Fact(type: .other(3), value: "other-3"), ] contact.getFactsFromContact.run = { _ in facts } contact.setFactsOnContact.run = { data, newFacts in @@ -52,10 +52,10 @@ final class ContactTests: XCTestCase { try contact.setFact(.email, nil) XCTAssertNoDifference(facts, [ - Fact(fact: "new-username", type: 0), - Fact(fact: "phone", type: 2), - Fact(fact: "new-other-3", type: 3), - Fact(fact: "new-other-4", type: 4), + Fact(type: .username, value: "new-username"), + Fact(type: .phone, value: "phone"), + Fact(type: .other(3), value: "new-other-3"), + Fact(type: .other(4), value: "new-other-4"), ]) } } diff --git a/Tests/XXClientTests/Models/FactTests.swift b/Tests/XXClientTests/Models/FactTests.swift index 5c218ad07cebb0c0b69ee96a3172518744d1f695..38b6c560f73b049fa7ac33b7f7a4716cc02eee18 100644 --- a/Tests/XXClientTests/Models/FactTests.swift +++ b/Tests/XXClientTests/Models/FactTests.swift @@ -16,8 +16,8 @@ final class FactTests: XCTestCase { let model = try Fact.decode(jsonData) XCTAssertNoDifference(model, Fact( - fact: factValue, - type: 123 + type: .other(123), + value: factValue )) let encodedModel = try model.encode() @@ -28,9 +28,9 @@ final class FactTests: XCTestCase { func testCodingArray() throws { let models = [ - Fact(fact: "abcd", type: 0), - Fact(fact: "efgh", type: 1), - Fact(fact: "ijkl", type: 2), + Fact(type: .username, value: "abcd"), + Fact(type: .email, value: "efgh"), + Fact(type: .phone, value: "ijkl"), ] let encodedModels = try models.encode() @@ -54,10 +54,10 @@ final class FactTests: XCTestCase { func testArrayGetter() { let facts = [ - Fact(fact: "username", type: 0), - Fact(fact: "email", type: 1), - Fact(fact: "phone", type: 2), - Fact(fact: "other", type: 3), + Fact(type: .username, value: "username"), + Fact(type: .email, value: "email"), + Fact(type: .phone, value: "phone"), + Fact(type: .other(3), value: "other"), ] XCTAssertNoDifference( @@ -69,10 +69,10 @@ final class FactTests: XCTestCase { facts.get(.other(4)), ], [ - Fact(fact: "username", type: 0), - Fact(fact: "email", type: 1), - Fact(fact: "phone", type: 2), - Fact(fact: "other", type: 3), + Fact(type: .username, value: "username"), + Fact(type: .email, value: "email"), + Fact(type: .phone, value: "phone"), + Fact(type: .other(3), value: "other"), nil ] ) @@ -89,10 +89,10 @@ final class FactTests: XCTestCase { XCTAssertNoDifference( facts, [ - Fact(fact: "username", type: 0), - Fact(fact: "email", type: 1), - Fact(fact: "phone", type: 2), - Fact(fact: "other", type: 3), + Fact(type: .username, value: "username"), + Fact(type: .email, value: "email"), + Fact(type: .phone, value: "phone"), + Fact(type: .other(3), value: "other"), ] ) } diff --git a/Tests/XXMessengerClientTests/Messenger/Functions/MessengerSearchUsersTests.swift b/Tests/XXMessengerClientTests/Messenger/Functions/MessengerSearchUsersTests.swift index b6eaac39a1d79cfbf665e7b9a8dc90fab2d161af..9ffa1c82bdc0fec3fc7b089c2ef25a60f634d86d 100644 --- a/Tests/XXMessengerClientTests/Messenger/Functions/MessengerSearchUsersTests.swift +++ b/Tests/XXMessengerClientTests/Messenger/Functions/MessengerSearchUsersTests.swift @@ -181,9 +181,9 @@ final class MessengerSearchUsersTests: XCTestCase { phone: "phone" ).facts, [ - Fact(fact: "username", type: 0), - Fact(fact: "email", type: 1), - Fact(fact: "phone", type: 2), + Fact(type: .username, value: "username"), + Fact(type: .email, value: "email"), + Fact(type: .phone, value: "phone"), ] ) @@ -194,7 +194,7 @@ final class MessengerSearchUsersTests: XCTestCase { phone: nil ).facts, [ - Fact(fact: "username", type: 0), + Fact(type: .username, value: "username"), ] ) }