10
10
//===----------------------------------------------------------------------===//
11
11
12
12
fileprivate protocol ParsableArgumentsValidator {
13
- static func validate( _ type: ParsableArguments . Type ) -> ParsableArgumentsValidatorError ?
13
+ static func validate( _ type: ParsableArguments . Type , parent : InputKey . Parent ) -> ParsableArgumentsValidatorError ?
14
14
}
15
15
16
16
enum ValidatorErrorKind {
@@ -37,15 +37,15 @@ struct ParsableArgumentsValidationError: Error, CustomStringConvertible {
37
37
}
38
38
39
39
extension ParsableArguments {
40
- static func _validate( ) throws {
40
+ static func _validate( parent : InputKey . Parent ) throws {
41
41
let validators : [ ParsableArgumentsValidator . Type ] = [
42
42
PositionalArgumentsValidator . self,
43
43
ParsableArgumentsCodingKeyValidator . self,
44
44
ParsableArgumentsUniqueNamesValidator . self,
45
45
NonsenseFlagsValidator . self,
46
46
]
47
47
let errors = validators. compactMap { validator in
48
- validator. validate ( self )
48
+ validator. validate ( self , parent : parent )
49
49
}
50
50
if errors. count > 0 {
51
51
throw ParsableArgumentsValidationError ( parsableArgumentsType: self , underlayingErrors: errors)
@@ -68,7 +68,6 @@ fileprivate extension ArgumentSet {
68
68
/// in the argument list. Any other configuration leads to ambiguity in
69
69
/// parsing the arguments.
70
70
struct PositionalArgumentsValidator : ParsableArgumentsValidator {
71
-
72
71
struct Error : ParsableArgumentsValidatorError , CustomStringConvertible {
73
72
let repeatedPositionalArgument : String
74
73
@@ -81,19 +80,16 @@ struct PositionalArgumentsValidator: ParsableArgumentsValidator {
81
80
var kind : ValidatorErrorKind { . failure }
82
81
}
83
82
84
- static func validate( _ type: ParsableArguments . Type ) -> ParsableArgumentsValidatorError ? {
83
+ static func validate( _ type: ParsableArguments . Type , parent : InputKey . Parent ) -> ParsableArgumentsValidatorError ? {
85
84
let sets : [ ArgumentSet ] = Mirror ( reflecting: type. init ( ) )
86
85
. children
87
86
. compactMap { child in
88
87
guard
89
- var codingKey = child. label,
88
+ let codingKey = child. label,
90
89
let parsed = child. value as? ArgumentSetProvider
91
90
else { return nil }
92
91
93
- // Property wrappers have underscore-prefixed names
94
- codingKey = String ( codingKey. first == " _ " ? codingKey. dropFirst ( 1 ) : codingKey. dropFirst ( 0 ) )
95
-
96
- let key = InputKey ( rawValue: codingKey)
92
+ let key = InputKey ( name: codingKey, parent: parent)
97
93
return parsed. argumentSet ( for: key)
98
94
}
99
95
@@ -107,20 +103,20 @@ struct PositionalArgumentsValidator: ParsableArgumentsValidator {
107
103
let firstRepeatedPositionalArgument : ArgumentDefinition = sets [ repeatedPositional] . firstRepeatedPositionalArgument!
108
104
let positionalFollowingRepeatedArgument : ArgumentDefinition = positionalFollowingRepeated. firstPositionalArgument!
109
105
return Error (
110
- repeatedPositionalArgument: firstRepeatedPositionalArgument. help. keys. first!. rawValue ,
111
- positionalArgumentFollowingRepeated: positionalFollowingRepeatedArgument. help. keys. first!. rawValue )
106
+ repeatedPositionalArgument: firstRepeatedPositionalArgument. help. keys. first!. name ,
107
+ positionalArgumentFollowingRepeated: positionalFollowingRepeatedArgument. help. keys. first!. name )
112
108
}
113
109
}
114
110
115
111
/// Ensure that all arguments have corresponding coding keys
116
112
struct ParsableArgumentsCodingKeyValidator : ParsableArgumentsValidator {
117
113
118
114
private struct Validator : Decoder {
119
- let argumentKeys : [ String ]
115
+ let argumentKeys : [ InputKey ]
120
116
121
117
enum ValidationResult : Swift . Error {
122
118
case success
123
- case missingCodingKeys( [ String ] )
119
+ case missingCodingKeys( [ InputKey ] )
124
120
}
125
121
126
122
let codingPath : [ CodingKey ] = [ ]
@@ -135,7 +131,7 @@ struct ParsableArgumentsCodingKeyValidator: ParsableArgumentsValidator {
135
131
}
136
132
137
133
func container< Key> ( keyedBy type: Key . Type ) throws -> KeyedDecodingContainer < Key > where Key : CodingKey {
138
- let missingKeys = argumentKeys. filter { Key ( stringValue: $0) == nil }
134
+ let missingKeys = argumentKeys. filter { Key ( stringValue: $0. name ) == nil }
139
135
if missingKeys. isEmpty {
140
136
throw ValidationResult . success
141
137
} else {
@@ -147,7 +143,7 @@ struct ParsableArgumentsCodingKeyValidator: ParsableArgumentsValidator {
147
143
/// This error indicates that an option, a flag, or an argument of
148
144
/// a `ParsableArguments` is defined without a corresponding `CodingKey`.
149
145
struct MissingKeysError : ParsableArgumentsValidatorError , CustomStringConvertible {
150
- let missingCodingKeys : [ String ]
146
+ let missingCodingKeys : [ InputKey ]
151
147
152
148
var description : String {
153
149
let resolution = """
@@ -194,8 +190,8 @@ struct ParsableArgumentsCodingKeyValidator: ParsableArgumentsValidator {
194
190
}
195
191
}
196
192
197
- static func validate( _ type: ParsableArguments . Type ) -> ParsableArgumentsValidatorError ? {
198
- let argumentKeys : [ String ] = Mirror ( reflecting: type. init ( ) )
193
+ static func validate( _ type: ParsableArguments . Type , parent : InputKey . Parent ) -> ParsableArgumentsValidatorError ? {
194
+ let argumentKeys : [ InputKey ] = Mirror ( reflecting: type. init ( ) )
199
195
. children
200
196
. compactMap { child in
201
197
guard
@@ -204,7 +200,7 @@ struct ParsableArgumentsCodingKeyValidator: ParsableArgumentsValidator {
204
200
else { return nil }
205
201
206
202
// Property wrappers have underscore-prefixed names
207
- return String ( codingKey . first == " _ " ? codingKey. dropFirst ( 1 ) : codingKey . dropFirst ( 0 ) )
203
+ return InputKey ( name : codingKey, parent : parent )
208
204
}
209
205
guard argumentKeys. count > 0 else {
210
206
return nil
@@ -239,19 +235,16 @@ struct ParsableArgumentsUniqueNamesValidator: ParsableArgumentsValidator {
239
235
var kind : ValidatorErrorKind { . failure }
240
236
}
241
237
242
- static func validate( _ type: ParsableArguments . Type ) -> ParsableArgumentsValidatorError ? {
238
+ static func validate( _ type: ParsableArguments . Type , parent : InputKey . Parent ) -> ParsableArgumentsValidatorError ? {
243
239
let argSets : [ ArgumentSet ] = Mirror ( reflecting: type. init ( ) )
244
240
. children
245
241
. compactMap { child in
246
242
guard
247
- var codingKey = child. label,
243
+ let codingKey = child. label,
248
244
let parsed = child. value as? ArgumentSetProvider
249
245
else { return nil }
250
246
251
- // Property wrappers have underscore-prefixed names
252
- codingKey = String ( codingKey. first == " _ " ? codingKey. dropFirst ( 1 ) : codingKey. dropFirst ( 0 ) )
253
-
254
- let key = InputKey ( rawValue: codingKey)
247
+ let key = InputKey ( name: codingKey, parent: parent)
255
248
return parsed. argumentSet ( for: key)
256
249
}
257
250
@@ -290,19 +283,16 @@ struct NonsenseFlagsValidator: ParsableArgumentsValidator {
290
283
var kind : ValidatorErrorKind { . warning }
291
284
}
292
285
293
- static func validate( _ type: ParsableArguments . Type ) -> ParsableArgumentsValidatorError ? {
286
+ static func validate( _ type: ParsableArguments . Type , parent : InputKey . Parent ) -> ParsableArgumentsValidatorError ? {
294
287
let argSets : [ ArgumentSet ] = Mirror ( reflecting: type. init ( ) )
295
288
. children
296
289
. compactMap { child in
297
290
guard
298
- var codingKey = child. label,
291
+ let codingKey = child. label,
299
292
let parsed = child. value as? ArgumentSetProvider
300
293
else { return nil }
301
294
302
- // Property wrappers have underscore-prefixed names
303
- codingKey = String ( codingKey. first == " _ " ? codingKey. dropFirst ( 1 ) : codingKey. dropFirst ( 0 ) )
304
-
305
- let key = InputKey ( rawValue: codingKey)
295
+ let key = InputKey ( name: codingKey, parent: parent)
306
296
return parsed. argumentSet ( for: key)
307
297
}
308
298
0 commit comments