This repository was archived by the owner on Sep 15, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathRemoteDiff.swift
More file actions
110 lines (86 loc) · 2.83 KB
/
Copy pathRemoteDiff.swift
File metadata and controls
110 lines (86 loc) · 2.83 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import Foundation
/// RemoteDiff model
public struct RemoteDiff: Codable {
/// Revision id from the content has been changed
public var fromRevisionId: Int
/// Current revision id
public var toRevisionId: Int
/// Model for the diff values
public var values: RemoteDiffValues
/// Mapping keys
private enum CodingKeys: String, CodingKey {
case fromRevisionId = "from"
case toRevisionId = "to"
case values = "diff"
}
// MARK: - Decode protocol
public init(from decoder: Decoder) throws {
let data = try decoder.container(keyedBy: CodingKeys.self)
fromRevisionId = (try? data.decode(Int.self, forKey: .fromRevisionId)) ?? 0
toRevisionId = (try? data.decode(Int.self, forKey: .toRevisionId)) ?? 0
values = try data.decode(RemoteDiffValues.self, forKey: .values)
}
}
/// RemoteDiffValues model
public struct RemoteDiffValues: Codable {
/// Model for the diff totals operations
public var totals: RemoteDiffTotals?
/// Title diffs array
public var titleDiffs: [RemoteDiffValue]
/// Content diffs array
public var contentDiffs: [RemoteDiffValue]
/// Mapping keys
private enum CodingKeys: String, CodingKey {
case titleDiffs = "post_title"
case contentDiffs = "post_content"
case totals
}
}
/// RemoteDiffTotals model
public struct RemoteDiffTotals: Codable {
/// Total of additional operations
public var totalAdditions: Int
/// Total of deletions operations
public var totalDeletions: Int
/// Mapping keys
private enum CodingKeys: String, CodingKey {
case totalAdditions = "add"
case totalDeletions = "del"
}
// MARK: - Decode protocol
public init(from decoder: Decoder) throws {
let data = try decoder.container(keyedBy: CodingKeys.self)
totalAdditions = (try? data.decode(Int.self, forKey: .totalAdditions)) ?? 0
totalDeletions = (try? data.decode(Int.self, forKey: .totalDeletions)) ?? 0
}
}
/// RemoteDiffOperation enumeration
///
/// - add: Addition
/// - copy: Copy
/// - del: Deletion
/// - unknown: Default value
public enum RemoteDiffOperation: String, Codable {
case add
case copy
case del
case unknown
}
/// DiffValue
public struct RemoteDiffValue: Codable {
/// Diff operation
public var operation: RemoteDiffOperation
/// Diff value
public var value: String?
/// Mapping keys
private enum CodingKeys: String, CodingKey {
case operation = "op"
case value
}
// MARK: - Decode protocol
public init(from decoder: Decoder) throws {
let data = try decoder.container(keyedBy: CodingKeys.self)
operation = (try? data.decode(RemoteDiffOperation.self, forKey: .operation)) ?? .unknown
value = try? data.decode(String.self, forKey: .value)
}
}