Skip to content

Commit f2c4262

Browse files
committed
[becnhmark][Gardening] Extracted checked function
1 parent 743c7ef commit f2c4262

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

benchmark/utils/DriverUtils.swift

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,16 @@ struct TestConfig {
8181

8282
filters = benchArgs.positionalArgs
8383

84+
func checked<T>(
85+
_ parse: (String) throws -> T?,
86+
_ value: String,
87+
argument: String? = nil
88+
) throws -> T {
89+
if let t = try parse(value) { return t }
90+
throw ArgumentError.invalidType(
91+
value: value, type: "\(T.self)", argument: argument)
92+
}
93+
8494
func optionalArg<T>(
8595
_ name: String,
8696
_ property: WritableKeyPath<TestConfig, T>,
@@ -90,28 +100,20 @@ struct TestConfig {
90100
if let value = benchArgs.optionalArgsMap[name] {
91101
guard !value.isEmpty || defaultValue != nil
92102
else { throw ArgumentError.missingValue(name) }
93-
guard let typedValue = (value.isEmpty) ? defaultValue
94-
: try parse(value) else {
95-
throw ArgumentError.invalidType(
96-
value: value, type: String(describing: T.self), argument: name)
97-
}
98-
self[keyPath: property] = typedValue
99-
}
100-
}
101103

102-
func tag(tag: String) throws -> BenchmarkCategory {
103-
guard let category = BenchmarkCategory(rawValue: tag) else {
104-
throw ArgumentError.invalidType(
105-
value: tag, type: "BenchmarkCategory", argument: nil)
104+
self[keyPath: property] = (value.isEmpty)
105+
? defaultValue!
106+
: try checked(parse, value, argument:name)
106107
}
107-
return category
108108
}
109+
109110
func tags(tags: String) throws -> Set<BenchmarkCategory> {
110111
// We support specifying multiple tags by splitting on comma, i.e.:
111112
// --tags=Array,Dictionary
112113
// --skip-tags=Array,Set,unstable,skip
113114
return Set(
114-
try tags.split(separator: ",").map(String.init).map(tag))
115+
try tags.split(separator: ",").map(String.init).map {
116+
try checked({ BenchmarkCategory(rawValue: $0) }, $0) })
115117
}
116118

117119
try optionalArg("--iter-scale", \.iterationScale) { Int($0) }

0 commit comments

Comments
 (0)