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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
|
//===----------------------------------------------------------*- swift -*-===//
//
// This source file is part of the Swift Argument Parser open source project
//
// Copyright (c) 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//
/// A previously decoded parsable arguments type.
///
/// Because arguments are consumed and decoded the first time they're
/// encountered, we save the decoded instances for using later in the
/// command/subcommand hierarchy.
struct DecodedArguments {
var type: ParsableArguments.Type
var value: ParsableArguments
var commandType: ParsableCommand.Type? {
type as? ParsableCommand.Type
}
var command: ParsableCommand? {
value as? ParsableCommand
}
}
/// A decoder that decodes from parsed command-line arguments.
final class ArgumentDecoder: Decoder {
init(values: ParsedValues, previouslyDecoded: [DecodedArguments] = []) {
self.values = values
self.previouslyDecoded = previouslyDecoded
self.usedOrigins = InputOrigin()
}
let values: ParsedValues
var usedOrigins: InputOrigin
var nextCommandIndex = 0
var previouslyDecoded: [DecodedArguments] = []
var codingPath: [CodingKey] = []
var userInfo: [CodingUserInfoKey : Any] = [:]
func container<K>(keyedBy type: K.Type) throws -> KeyedDecodingContainer<K> where K: CodingKey {
let container = ParsedArgumentsContainer(for: self, keyType: K.self, codingPath: codingPath)
return KeyedDecodingContainer(container)
}
func unkeyedContainer() throws -> UnkeyedDecodingContainer {
throw Error.topLevelHasNoUnkeyedContainer
}
func singleValueContainer() throws -> SingleValueDecodingContainer {
throw Error.topLevelHasNoSingleValueContainer
}
}
extension ArgumentDecoder {
fileprivate func element(forKey key: InputKey) -> ParsedValues.Element? {
guard let element = values.element(forKey: key) else { return nil }
usedOrigins.formUnion(element.inputOrigin)
return element
}
}
extension ArgumentDecoder {
enum Error: Swift.Error {
case topLevelHasNoUnkeyedContainer
case topLevelHasNoSingleValueContainer
case singleValueDecoderHasNoContainer
case wrongKeyType(CodingKey.Type, CodingKey.Type)
}
}
final class ParsedArgumentsContainer<K>: KeyedDecodingContainerProtocol where K : CodingKey {
var codingPath: [CodingKey]
let decoder: ArgumentDecoder
init(for decoder: ArgumentDecoder, keyType: K.Type, codingPath: [CodingKey]) {
self.codingPath = codingPath
self.decoder = decoder
}
var allKeys: [K] {
fatalError()
}
fileprivate func element(forKey key: K) -> ParsedValues.Element? {
let k = InputKey(codingKey: key, path: codingPath)
return decoder.element(forKey: k)
}
func contains(_ key: K) -> Bool {
return element(forKey: key) != nil
}
func decodeNil(forKey key: K) throws -> Bool {
return element(forKey: key)?.value == nil
}
func decode<T>(_ type: T.Type, forKey key: K) throws -> T where T : Decodable {
let parsedElement = element(forKey: key)
if parsedElement?.inputOrigin.isDefaultValue ?? false, let rawValue = parsedElement?.value {
guard let value = rawValue as? T else {
throw InternalParseError.wrongType(rawValue, forKey: parsedElement!.key)
}
return value
}
let subDecoder = SingleValueDecoder(userInfo: decoder.userInfo, underlying: decoder, codingPath: codingPath + [key], key: InputKey(codingKey: key, path: codingPath), parsedElement: element(forKey: key))
return try type.init(from: subDecoder)
}
func decodeIfPresent<T>(_ type: T.Type, forKey key: KeyedDecodingContainer<K>.Key) throws -> T? where T : Decodable {
let parsedElement = element(forKey: key)
if let parsedElement = parsedElement, parsedElement.inputOrigin.isDefaultValue {
return parsedElement.value as? T
}
let subDecoder = SingleValueDecoder(userInfo: decoder.userInfo, underlying: decoder, codingPath: codingPath + [key], key: InputKey(codingKey: key, path: codingPath), parsedElement: parsedElement)
do {
return try type.init(from: subDecoder)
} catch let error as ParserError {
if case .noValue = error {
return nil
} else {
throw error
}
}
}
func nestedContainer<NestedKey>(keyedBy type: NestedKey.Type, forKey key: K) throws -> KeyedDecodingContainer<NestedKey> where NestedKey : CodingKey {
fatalError()
}
func nestedUnkeyedContainer(forKey key: K) throws -> UnkeyedDecodingContainer {
fatalError()
}
func superDecoder() throws -> Decoder {
fatalError()
}
func superDecoder(forKey key: K) throws -> Decoder {
fatalError()
}
}
struct SingleValueDecoder: Decoder {
var userInfo: [CodingUserInfoKey : Any]
var underlying: ArgumentDecoder
var codingPath: [CodingKey]
var key: InputKey
var parsedElement: ParsedValues.Element?
func container<K>(keyedBy type: K.Type) throws -> KeyedDecodingContainer<K> where K: CodingKey {
return KeyedDecodingContainer(ParsedArgumentsContainer(for: underlying, keyType: type, codingPath: codingPath))
}
func unkeyedContainer() throws -> UnkeyedDecodingContainer {
guard let e = parsedElement else {
var errorPath = codingPath
let last = errorPath.popLast()!
throw ParserError.noValue(forKey: InputKey(codingKey: last, path: errorPath))
}
guard let a = e.value as? [Any] else {
throw ParserError.invalidState
}
return UnkeyedContainer(codingPath: codingPath, parsedElement: e, array: ArrayWrapper(a))
}
func singleValueContainer() throws -> SingleValueDecodingContainer {
return SingleValueContainer(underlying: self, codingPath: codingPath, parsedElement: parsedElement)
}
func previousValue<T>(_ type: T.Type) throws -> T {
guard let previous = underlying.previouslyDecoded.first(where: { type == $0.type })
else { throw ParserError.invalidState }
return previous.value as! T
}
func saveValue<T: ParsableArguments>(_ value: T, type: T.Type = T.self) {
underlying.previouslyDecoded.append(DecodedArguments(type: type, value: value))
}
struct SingleValueContainer: SingleValueDecodingContainer {
var underlying: SingleValueDecoder
var codingPath: [CodingKey]
var parsedElement: ParsedValues.Element?
func decodeNil() -> Bool {
return parsedElement == nil
}
func decode<T>(_ type: T.Type) throws -> T where T : Decodable {
guard let e = parsedElement else {
var errorPath = codingPath
let last = errorPath.popLast()!
throw ParserError.noValue(forKey: InputKey(codingKey: last, path: errorPath))
}
guard let s = e.value as? T else {
throw InternalParseError.wrongType(e.value, forKey: e.key)
}
return s
}
}
struct UnkeyedContainer: UnkeyedDecodingContainer {
var codingPath: [CodingKey]
var parsedElement: ParsedValues.Element
var array: ArrayWrapperProtocol
var count: Int? {
return array.count
}
var isAtEnd: Bool {
return array.isAtEnd
}
var currentIndex: Int {
return array.currentIndex
}
mutating func decodeNil() throws -> Bool {
return false
}
mutating func decode<T>(_ type: T.Type) throws -> T where T : Decodable {
guard let next = array.getNext() else { fatalError() }
guard let t = next as? T else {
throw InternalParseError.wrongType(next, forKey: parsedElement.key)
}
return t
}
mutating func nestedContainer<NestedKey>(keyedBy type: NestedKey.Type) throws -> KeyedDecodingContainer<NestedKey> where NestedKey : CodingKey {
fatalError()
}
mutating func nestedUnkeyedContainer() throws -> UnkeyedDecodingContainer {
fatalError()
}
mutating func superDecoder() throws -> Decoder {
fatalError()
}
}
}
/// A type-erasing wrapper for consuming elements of an array.
protocol ArrayWrapperProtocol {
var count: Int? { get }
var isAtEnd: Bool { get }
var currentIndex: Int { get }
mutating func getNext() -> Any?
}
struct ArrayWrapper<A>: ArrayWrapperProtocol {
var base: [A]
var currentIndex: Int
init(_ a: [A]) {
self.base = a
self.currentIndex = a.startIndex
}
var count: Int? {
return base.count
}
var isAtEnd: Bool {
return base.endIndex <= currentIndex
}
mutating func getNext() -> Any? {
guard currentIndex < base.endIndex else { return nil }
let next = base[currentIndex]
currentIndex += 1
return next
}
}
|