|
5 | 5 | @testable import Glean |
6 | 6 | import XCTest |
7 | 7 |
|
8 | | -struct BalloonsObjectItem: Decodable, Equatable, ObjectSerialize { |
| 8 | +struct BalloonsObjectItem: Codable, Equatable { |
9 | 9 | var colour: String? |
10 | 10 | var diameter: Int64? |
| 11 | + var anotherValue: Bool? |
11 | 12 |
|
12 | | - func intoSerializedObject() -> String { |
13 | | - var data: [String] = [] |
14 | | - if let val = self.colour { |
15 | | - var elem = "\"colour\":" |
16 | | - elem.append(val.intoSerializedObject()) |
17 | | - data.append(elem) |
| 13 | + enum CodingKeys: String, CodingKey { |
| 14 | + case colour = "colour" |
| 15 | + case diameter = "diameter" |
| 16 | + case anotherValue = "another_value" |
| 17 | + } |
| 18 | + |
| 19 | + func encode(to encoder: Encoder) throws { |
| 20 | + var container = encoder.container(keyedBy: CodingKeys.self) |
| 21 | + if let colour = self.colour { |
| 22 | + try container.encode(colour, forKey: .colour) |
18 | 23 | } |
19 | | - if let val = self.diameter { |
20 | | - var elem = "\"diameter\":" |
21 | | - elem.append(val.intoSerializedObject()) |
22 | | - data.append(elem) |
| 24 | + if let diameter = self.diameter { |
| 25 | + try container.encode(diameter, forKey: .diameter) |
| 26 | + } |
| 27 | + if let anotherValue = self.anotherValue { |
| 28 | + try container.encode(anotherValue, forKey: .anotherValue) |
23 | 29 | } |
24 | | - let obj = data.joined(separator: ",") |
25 | | - let json = "{" + obj + "}" |
26 | | - return json |
27 | 30 | } |
28 | 31 | } |
29 | | - |
30 | 32 | typealias BalloonsObject = [BalloonsObjectItem] |
31 | 33 |
|
| 34 | +// generated from |
| 35 | +// |
| 36 | +// ``` |
| 37 | +// structure: |
| 38 | +// type: object |
| 39 | +// properties: |
| 40 | +// key1: |
| 41 | +// type: string |
| 42 | +// another_value: |
| 43 | +// type: number |
| 44 | +// sub_array: |
| 45 | +// type: array |
| 46 | +// items: |
| 47 | +// type: number |
| 48 | +// ``` |
| 49 | +struct ToplevelObjectObject: Codable, Equatable, ObjectSerialize { |
| 50 | + var key1: String? |
| 51 | + var anotherValue: Int64? |
| 52 | + var subArray: ToplevelObjectObjectSubArray = [] |
| 53 | + |
| 54 | + enum CodingKeys: String, CodingKey { |
| 55 | + case key1 = "key1" |
| 56 | + case anotherValue = "another_value" |
| 57 | + case subArray = "sub_array" |
| 58 | + } |
| 59 | + |
| 60 | + func encode(to encoder: Encoder) throws { |
| 61 | + var container = encoder.container(keyedBy: CodingKeys.self) |
| 62 | + if let key1 = self.key1 { |
| 63 | + try container.encode(key1, forKey: .key1) |
| 64 | + } |
| 65 | + if let anotherValue = self.anotherValue { |
| 66 | + try container.encode(anotherValue, forKey: .anotherValue) |
| 67 | + } |
| 68 | + if subArray.count > 0 { |
| 69 | + let subArray = self.subArray |
| 70 | + try container.encode(subArray, forKey: .subArray) |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + func intoSerializedObject() -> String { |
| 75 | + let jsonEncoder = JSONEncoder() |
| 76 | + let jsonData = try! jsonEncoder.encode(self) |
| 77 | + let json = String(data: jsonData, encoding: String.Encoding.utf8)! |
| 78 | + return json |
| 79 | + } |
| 80 | +} |
| 81 | +typealias ToplevelObjectObjectSubArray = [ToplevelObjectObjectSubArrayItem] |
| 82 | +typealias ToplevelObjectObjectSubArrayItem = Int64 |
| 83 | + |
32 | 84 | class ObjectMetricTypeTests: XCTestCase { |
33 | 85 | override func setUp() { |
34 | 86 | resetGleanDiscardingInitialPings(testCase: self, tag: "ObjectMetricTypeTests") |
@@ -132,4 +184,54 @@ class ObjectMetricTypeTests: XCTestCase { |
132 | 184 | XCTAssertEqual(2, snapshot.count) |
133 | 185 | XCTAssertEqual(expected, snapshot) |
134 | 186 | } |
| 187 | + |
| 188 | + func testObjectDecodesFromSnakeCase() { |
| 189 | + let metric = ObjectMetricType<BalloonsObject>(CommonMetricData( |
| 190 | + category: "test", |
| 191 | + name: "balloon", |
| 192 | + sendInPings: ["store1"], |
| 193 | + lifetime: .ping, |
| 194 | + disabled: false |
| 195 | + )) |
| 196 | + |
| 197 | + XCTAssertNil(metric.testGetValue()) |
| 198 | + |
| 199 | + var balloons: BalloonsObject = [] |
| 200 | + balloons.append(BalloonsObjectItem(colour: "red", diameter: 5, anotherValue: true)) |
| 201 | + balloons.append(BalloonsObjectItem(colour: "green", anotherValue: false)) |
| 202 | + metric.set(balloons) |
| 203 | + |
| 204 | + let snapshot = metric.testGetValue()! |
| 205 | + XCTAssertNotNil(snapshot) |
| 206 | + XCTAssertEqual(2, snapshot.count) |
| 207 | + |
| 208 | + XCTAssertEqual(snapshot[0].colour, "red") |
| 209 | + XCTAssertEqual(snapshot[0].diameter, 5) |
| 210 | + XCTAssertEqual(snapshot[0].anotherValue, true) |
| 211 | + XCTAssertEqual(snapshot[1].colour, "green") |
| 212 | + XCTAssertNil(snapshot[1].diameter) |
| 213 | + XCTAssertEqual(snapshot[1].anotherValue, false) |
| 214 | + } |
| 215 | + |
| 216 | + func testObjectWithStructureOnToplevel() { |
| 217 | + let metric = ObjectMetricType<ToplevelObjectObject>(CommonMetricData( |
| 218 | + category: "test", |
| 219 | + name: "toplevel_object", |
| 220 | + sendInPings: ["store1"], |
| 221 | + lifetime: .ping, |
| 222 | + disabled: false |
| 223 | + )) |
| 224 | + |
| 225 | + XCTAssertNil(metric.testGetValue()) |
| 226 | + |
| 227 | + let obj = ToplevelObjectObject(key1: "test", anotherValue: 3, subArray: [1, 2, 3]) |
| 228 | + metric.set(obj) |
| 229 | + |
| 230 | + let snapshot = metric.testGetValue()! |
| 231 | + XCTAssertNotNil(snapshot) |
| 232 | + |
| 233 | + XCTAssertEqual("test", snapshot.key1) |
| 234 | + XCTAssertEqual(3, snapshot.anotherValue) |
| 235 | + XCTAssertEqual([1, 2, 3], snapshot.subArray) |
| 236 | + } |
135 | 237 | } |
0 commit comments