Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions glean-core/ios/Glean/Metrics/ObjectMetric.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public final class ObjectMetricType<K: ObjectSerialize>: Sendable {
public func testGetValue(_ pingName: String? = nil) -> K? {
guard let data = self.inner.testGetValue(pingName) else { return nil }
let jsonDecoder = JSONDecoder()
jsonDecoder.keyDecodingStrategy = .convertFromSnakeCase
return try! jsonDecoder.decode(K.self, from: Data(data.utf8))
}

Expand Down
34 changes: 34 additions & 0 deletions glean-core/ios/GleanTests/Metrics/ObjectMetricTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import XCTest
struct BalloonsObjectItem: Decodable, Equatable, ObjectSerialize {
var colour: String?
var diameter: Int64?
var anotherValue: Bool?

func intoSerializedObject() -> String {
var data: [String] = []
Expand All @@ -21,6 +22,11 @@ struct BalloonsObjectItem: Decodable, Equatable, ObjectSerialize {
elem.append(val.intoSerializedObject())
data.append(elem)
}
if let val = self.anotherValue {
var elem = "\"another_value\":"
elem.append(val.intoSerializedObject())
data.append(elem)
}
let obj = data.joined(separator: ",")
let json = "{" + obj + "}"
return json
Expand Down Expand Up @@ -132,4 +138,32 @@ class ObjectMetricTypeTests: XCTestCase {
XCTAssertEqual(2, snapshot.count)
XCTAssertEqual(expected, snapshot)
}

func testObjectDecodesFromSnakeCase() {
let metric = ObjectMetricType<BalloonsObject>(CommonMetricData(
category: "test",
name: "balloon",
sendInPings: ["store1"],
lifetime: .ping,
disabled: false
))

XCTAssertNil(metric.testGetValue())

var balloons: BalloonsObject = []
balloons.append(BalloonsObjectItem(colour: "red", diameter: 5, anotherValue: true))
balloons.append(BalloonsObjectItem(colour: "green", anotherValue: false))
metric.set(balloons)

let snapshot = metric.testGetValue()!
XCTAssertNotNil(snapshot)
XCTAssertEqual(2, snapshot.count)

XCTAssertEqual(snapshot[0].colour, "red")
XCTAssertEqual(snapshot[0].diameter, 5)
XCTAssertEqual(snapshot[0].anotherValue, true)
XCTAssertEqual(snapshot[1].colour, "green")
XCTAssertNil(snapshot[1].diameter)
XCTAssertEqual(snapshot[1].anotherValue, false)
}
}