Skip to content

Refactor URIDecoder/URIParser to improve handling of the deepObject style #127

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Nov 21, 2024
Merged
22 changes: 0 additions & 22 deletions Sources/OpenAPIRuntime/URICoder/Common/URIDecodedTypes.swift

This file was deleted.

6 changes: 5 additions & 1 deletion Sources/OpenAPIRuntime/URICoder/Common/URIEncodedNode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ extension URIEncodedNode {
/// The encoder appended to a node that wasn't an array.
case appendingToNonArrayContainer

/// The encoder is trying to mark a container as array, but it's already
/// marked as a container of another type.
case markingExistingNonArrayContainerAsArray

/// The encoder inserted a value for key into a node that wasn't
/// a dictionary.
case insertingChildValueIntoNonContainer
Expand Down Expand Up @@ -136,7 +140,7 @@ extension URIEncodedNode {
// Already an array.
break
case .unset: self = .array([])
default: throw InsertionError.appendingToNonArrayContainer
default: throw InsertionError.markingExistingNonArrayContainerAsArray
}
}

Expand Down
6 changes: 0 additions & 6 deletions Sources/OpenAPIRuntime/URICoder/Common/URIParsedTypes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@ struct URIParsedKey: Hashable {
/// A primitive value produced by `URIParser`.
typealias URIParsedValue = String.SubSequence

/// An array of primitive values produced by `URIParser`.
typealias URIParsedValueArray = [URIParsedValue]

/// A key-value produced by `URIParser`.
struct URIParsedPair: Equatable {

Expand All @@ -55,9 +52,6 @@ struct URIParsedPair: Equatable {
var value: URIParsedValue
}

/// An array of key-value pairs produced by `URIParser`.
typealias URIParsedPairArray = [URIParsedPair]

// MARK: - Extensions

extension URIParsedKey: CustomStringConvertible {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,9 @@ extension URIKeyedDecodingContainer {

extension URIKeyedDecodingContainer: KeyedDecodingContainerProtocol {

var allKeys: [Key] {
do { return try decoder.elementKeysInCurrentDictionary().compactMap { .init(stringValue: $0) } } catch {
return []
}
}
var allKeys: [Key] { decoder.elementKeysInCurrentDictionary().compactMap { .init(stringValue: $0) } }

func contains(_ key: Key) -> Bool {
do { return try decoder.containsElementInCurrentDictionary(forKey: key.stringValue) } catch { return false }
}
func contains(_ key: Key) -> Bool { decoder.containsElementInCurrentDictionary(forKey: key.stringValue) }

var codingPath: [any CodingKey] { decoder.codingPath }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ struct URISingleValueDecodingContainer {
extension URISingleValueDecodingContainer {

/// The underlying value as a single value.
///
/// Can be nil if the underlying URI is valid, just doesn't contain any value.
///
/// For example, an empty string input into an exploded form decoder (expecting pairs in the form `key=value`)
/// would result in a nil returned value.
var value: URIParsedValue? { get throws { try decoder.currentElementAsSingleValue() } }

/// Returns the value found in the underlying node converted to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@ struct URIUnkeyedDecodingContainer {
let decoder: URIValueFromNodeDecoder

/// The index of the next item to be decoded.
private var index: URIParsedValueArray.Index = 0
private(set) var currentIndex: Int

/// Creates a new unkeyed container ready to decode the first key.
/// - Parameter decoder: The underlying decoder.
init(decoder: URIValueFromNodeDecoder) { self.decoder = decoder }
init(decoder: URIValueFromNodeDecoder) {
self.decoder = decoder
self.currentIndex = 0
}
}

extension URIUnkeyedDecodingContainer {
Expand All @@ -36,7 +39,7 @@ extension URIUnkeyedDecodingContainer {
/// - Throws: An error if the container ran out of items.
private mutating func _decodingNext<R>(in work: () throws -> R) throws -> R {
guard !isAtEnd else { throw URIValueFromNodeDecoder.GeneralError.reachedEndOfUnkeyedContainer }
defer { index += 1 }
defer { currentIndex += 1 }
return try work()
}

Expand All @@ -45,7 +48,7 @@ extension URIUnkeyedDecodingContainer {
/// - Returns: The next value found.
/// - Throws: An error if the container ran out of items.
private mutating func _decodeNext() throws -> URIParsedValue {
try _decodingNext { [decoder, index] in try decoder.nestedElementInCurrentArray(atIndex: index) }
try _decodingNext { [decoder, currentIndex] in try decoder.nestedElementInCurrentArray(atIndex: currentIndex) }
}

/// Returns the next value converted to the provided type.
Expand Down Expand Up @@ -103,9 +106,7 @@ extension URIUnkeyedDecodingContainer: UnkeyedDecodingContainer {

var count: Int? { try? decoder.countOfCurrentArray() }

var isAtEnd: Bool { index == count }

var currentIndex: Int { index }
var isAtEnd: Bool { currentIndex == count }

var codingPath: [any CodingKey] { decoder.codingPath }

Expand Down
Loading