-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexporter_metadata.swift
41 lines (38 loc) · 1.42 KB
/
exporter_metadata.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
import Foundation
import OpenFeature
// Define a Codable enum that can represent any type of JSON value
public enum ExporterMetadataValue: Codable, Equatable {
case string(String)
case integer(Int64)
case double(Double)
case bool(Bool)
// Decode the JSON based on its type
public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
if let stringValue = try? container.decode(String.self) {
self = .string(stringValue)
} else if let intValue = try? container.decode(Int64.self) {
self = .integer(intValue)
} else if let doubleValue = try? container.decode(Double.self) {
self = .double(doubleValue)
} else if let boolValue = try? container.decode(Bool.self) {
self = .bool(boolValue)
} else {
throw DecodingError.dataCorruptedError(in: container, debugDescription: "Cannot decode JSONValue")
}
}
// Encode the JSON based on its type
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
switch self {
case .string(let value):
try container.encode(value)
case .integer(let value):
try container.encode(value)
case .double(let value):
try container.encode(value)
case .bool(let value):
try container.encode(value)
}
}
}