-
Notifications
You must be signed in to change notification settings - Fork 111
/
Copy pathTypes.swift
81 lines (72 loc) · 2.28 KB
/
Types.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//
// Types.swift
// TinodeSDK
//
// Copyright © 2019-2022 Tinode LLC. All rights reserved.
//
public protocol Mergeable {
// Merges self with |another|.
// Retuns true if the target object was modified.
mutating func merge(with another: Mergeable) -> Bool
}
extension String: Mergeable {
public mutating func merge(with another: Mergeable) -> Bool {
guard another is String else { return false }
self = another as! String
return true
}
}
public typealias PrivateType = [String: JSONValue]
extension PrivateType: Mergeable {
public func getBoolValue(name: String) -> Bool? {
return self[name]?.asBool()
}
public func getStringValue(name: String) -> String? {
return self[name]?.asString()
}
public var comment: String? {
get {
return self["comment"]?.asString()
}
set {
self["comment"] = .string(newValue ?? Tinode.kNullValue)
}
}
public var archived: Bool? {
get {
self["arch"]?.asBool()
}
set {
self["arch"] = newValue != nil ? .bool(newValue!) : nil
}
}
public mutating func merge(with another: Mergeable) -> Bool {
guard another is PrivateType else { return false }
let anotherPT = another as! PrivateType
for (k, v) in anotherPT {
self[k] = v
}
return !anotherPT.isEmpty
}
}
public typealias TrustedType = [String: JSONValue]
extension TrustedType {
public var isVerified: Bool? {
return getBoolValue(name: "verified")
}
public var isStaffManaged: Bool? {
return getBoolValue(name: "staff")
}
public var isDangerous: Bool? {
return getBoolValue(name: "danger")
}
}
// Topic and Subscription types.
public typealias DefaultDescription = Description<TheCard, PrivateType>
public typealias DefaultSubscription = Subscription<TheCard, PrivateType>
public typealias FndDescription = Description<String, String>
public typealias FndSubscription = Subscription<TheCard, [String]>
public typealias DefaultTopic = Topic<TheCard, PrivateType, TheCard, PrivateType>
public typealias DefaultComTopic = ComTopic
public typealias DefaultMeTopic = MeTopic<TheCard>
public typealias DefaultFndTopic = FndTopic<TheCard>