-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6ff98fa
commit 0d5da64
Showing
2 changed files
with
79 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
Sources/Benchmark/BenchmarkExportConfigurations/BenchmarkExportConfiguration.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/// A configuration used or expected by a particular result exporter | ||
public protocol BenchmarkExportConfiguration: Codable {} | ||
|
||
public struct BenchmarkExportConfigurationKey: Hashable, Codable { | ||
private let value: String | ||
} | ||
|
||
/// The set of export configurations for a particular benchmark | ||
public struct BenchmarkExportConfigurations: Codable { | ||
let configs: [BenchmarkExportConfigurationKey: any BenchmarkExportConfiguration] | ||
|
||
public init(configs: [BenchmarkExportConfigurationKey: any BenchmarkExportConfiguration]) { | ||
self.configs = configs | ||
} | ||
|
||
public subscript(_ key: BenchmarkExportConfigurationKey) -> (any BenchmarkExportConfiguration)? { | ||
configs[key] | ||
} | ||
} | ||
|
||
extension BenchmarkExportConfigurations: ExpressibleByDictionaryLiteral { | ||
public init(dictionaryLiteral elements: (BenchmarkExportConfigurationKey, any BenchmarkExportConfiguration)...) { | ||
configs = Dictionary(elements, uniquingKeysWith: { $1 }) | ||
} | ||
} | ||
|
||
// N.B. We are clever with the codability implementation below | ||
// since the value type in `BenchmarkExportConfigurations` is | ||
// an existential type. | ||
// The key mechanism is `BenchmarkExportConfigurationKey.resolveConfigType` | ||
// that enables us to determine the appropriate concrete type to | ||
// attempt to decode based on the key names located in the | ||
// data container. | ||
|
||
extension BenchmarkExportConfigurationKey { | ||
/// This is used to determine the concrete type to attempt | ||
/// to decode for a particular ``BenchmarkExportConfigurationKey`` | ||
static func resolveConfigType(from key: Self) -> BenchmarkExportConfiguration.Type? { | ||
switch key { | ||
// Add a case here when adding a new exporter config | ||
default: nil | ||
} | ||
} | ||
} | ||
|
||
public extension BenchmarkExportConfigurations { | ||
init(from decoder: any Decoder) throws { | ||
let container = try decoder.container(keyedBy: BenchmarkExportConfigurationKey.self) | ||
self.configs = try container.allKeys.reduce( | ||
into: [BenchmarkExportConfigurationKey: any BenchmarkExportConfiguration]() | ||
) { configs, key in | ||
if let configType = type(of: key).resolveConfigType(from: key) { | ||
configs[key] = try container.decode(configType.self, forKey: key) | ||
} | ||
} | ||
} | ||
|
||
func encode(to encoder: any Encoder) throws { | ||
var encoder = encoder.container(keyedBy: BenchmarkExportConfigurationKey.self) | ||
for (key, config) in configs { | ||
try encoder.encode(config, forKey: key) | ||
} | ||
} | ||
} | ||
|
||
extension BenchmarkExportConfigurationKey: CodingKey { | ||
public var stringValue: String { value } | ||
|
||
public init?(stringValue: String) { self.init(value: stringValue) } | ||
|
||
public var intValue: Int? { nil } | ||
|
||
public init?(intValue: Int) { nil } | ||
} |