-
Notifications
You must be signed in to change notification settings - Fork 49
Introduce URIEncoder and URIDecoder types #41
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
Changes from 18 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
956d966
[Prototype] URIEncoder
czechboy0 bb9ba79
Merge branch 'main' into hd-uri-encoder
czechboy0 dbbe261
Refactored the URIEncoder a bunch, only support top level values for …
czechboy0 2c99128
Encoder, serializer, parser work, what remains is the decoder
czechboy0 2f710ab
Renaming, prepared the ground for implementing the decoder
czechboy0 569c576
WIP on the Decoder
czechboy0 e6d4b2f
Decoder working as well now
czechboy0 1befb30
Allow caching the parsed result
czechboy0 fcc413b
Update tests
czechboy0 ed6e073
Added rountrip tests
czechboy0 ae171d5
Improve tests, fix a few bugs around empty containers
czechboy0 e36c893
Formatting
czechboy0 3b36c16
Merge branch 'main' into hd-uri-encoder
czechboy0 a24c805
PR feedback
czechboy0 d821442
Add Date support
czechboy0 efb7fc0
WIP on documenting the URI coder
czechboy0 1eec75f
Finished documenting the URI coder
czechboy0 db84422
Fixed missing escaping of formatted dates
czechboy0 c0c0072
Merge branch 'main' into hd-uri-encoder
czechboy0 17eb5df
PR feedback - add a precondition for the first index provided
czechboy0 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
46 changes: 46 additions & 0 deletions
46
Sources/OpenAPIRuntime/URICoder/Common/URICodeCodingKey.swift
This file contains hidden or 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,46 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the SwiftOpenAPIGenerator open source project | ||
// | ||
// Copyright (c) 2023 Apple Inc. and the SwiftOpenAPIGenerator project authors | ||
// Licensed under Apache License v2.0 | ||
// | ||
// See LICENSE.txt for license information | ||
// See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import Foundation | ||
|
||
/// The coding key used by the URI encoder and decoder. | ||
struct URICoderCodingKey { | ||
|
||
/// The string to use in a named collection (e.g. a string-keyed dictionary). | ||
var stringValue: String | ||
|
||
/// The value to use in an integer-indexed collection (e.g. an int-keyed | ||
/// dictionary). | ||
var intValue: Int? | ||
|
||
/// Creates a new key with the same string and int value as the provided key. | ||
/// - Parameter key: The key whose values to copy. | ||
init(_ key: some CodingKey) { | ||
self.stringValue = key.stringValue | ||
self.intValue = key.intValue | ||
} | ||
} | ||
|
||
extension URICoderCodingKey: CodingKey { | ||
|
||
init(stringValue: String) { | ||
self.stringValue = stringValue | ||
self.intValue = nil | ||
} | ||
|
||
init(intValue: Int) { | ||
self.stringValue = "\(intValue)" | ||
self.intValue = intValue | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
Sources/OpenAPIRuntime/URICoder/Common/URICoderConfiguration.swift
This file contains hidden or 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,52 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the SwiftOpenAPIGenerator open source project | ||
// | ||
// Copyright (c) 2023 Apple Inc. and the SwiftOpenAPIGenerator project authors | ||
// Licensed under Apache License v2.0 | ||
// | ||
// See LICENSE.txt for license information | ||
// See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import Foundation | ||
|
||
/// A bag of configuration values used by the URI encoder and decoder. | ||
struct URICoderConfiguration { | ||
|
||
/// A variable expansion style as described by RFC 6570 and OpenAPI 3.0.3. | ||
enum Style { | ||
|
||
/// A style for simple string variable expansion. | ||
case simple | ||
|
||
/// A style for form-based URI expansion. | ||
case form | ||
} | ||
|
||
/// A character used to escape the space character. | ||
enum SpaceEscapingCharacter: String { | ||
|
||
/// A percent encoded value for the space character. | ||
case percentEncoded = "%20" | ||
|
||
/// The plus character. | ||
case plus = "+" | ||
} | ||
|
||
/// The variable expansion style. | ||
var style: Style | ||
|
||
/// A Boolean value indicating whether the key should be repeated with | ||
/// each value, as described by RFC 6570 and OpenAPI 3.0.3. | ||
var explode: Bool | ||
|
||
/// The character used to escape the space character. | ||
var spaceEscapingCharacter: SpaceEscapingCharacter | ||
|
||
/// The coder used for serializing the Date type. | ||
var dateTranscoder: any DateTranscoder | ||
} |
145 changes: 145 additions & 0 deletions
145
Sources/OpenAPIRuntime/URICoder/Common/URIEncodedNode.swift
This file contains hidden or 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,145 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the SwiftOpenAPIGenerator open source project | ||
// | ||
// Copyright (c) 2023 Apple Inc. and the SwiftOpenAPIGenerator project authors | ||
// Licensed under Apache License v2.0 | ||
// | ||
// See LICENSE.txt for license information | ||
// See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import Foundation | ||
|
||
/// A node produced by `URIValueToNodeEncoder`. | ||
enum URIEncodedNode: Equatable { | ||
|
||
/// No value. | ||
case unset | ||
|
||
/// A single primitive value. | ||
case primitive(Primitive) | ||
|
||
/// An array of nodes. | ||
case array([Self]) | ||
|
||
/// A dictionary with node values. | ||
case dictionary([String: Self]) | ||
|
||
/// A primitive value. | ||
enum Primitive: Equatable { | ||
|
||
/// A boolean value. | ||
case bool(Bool) | ||
|
||
/// A string value. | ||
case string(String) | ||
|
||
/// An integer value. | ||
case integer(Int) | ||
|
||
/// A floating-point value. | ||
case double(Double) | ||
|
||
/// A date value. | ||
case date(Date) | ||
} | ||
} | ||
|
||
extension URIEncodedNode { | ||
|
||
/// An error thrown by the methods modifying `URIEncodedNode`. | ||
enum InsertionError: Swift.Error { | ||
|
||
/// The encoder encoded a second primitive value. | ||
case settingPrimitiveValueAgain | ||
|
||
/// The encoder set a single value on a container. | ||
case settingValueOnAContainer | ||
|
||
/// The encoder appended to a node that wasn't an array. | ||
case appendingToNonArrayContainer | ||
|
||
/// The encoder inserted a value for key into a node that wasn't | ||
/// a dictionary. | ||
case insertingChildValueIntoNonContainer | ||
|
||
/// The encoder added a value to an array, but the key was not a valid | ||
/// integer key. | ||
case insertingChildValueIntoArrayUsingNonIntValueKey | ||
} | ||
|
||
/// Sets the node to be a primitive node with the provided value. | ||
/// - Parameter value: The primitive value to set into the node. | ||
/// - Throws: If the node is already set. | ||
mutating func set(_ value: Primitive) throws { | ||
switch self { | ||
case .unset: | ||
self = .primitive(value) | ||
case .primitive: | ||
throw InsertionError.settingPrimitiveValueAgain | ||
case .array, .dictionary: | ||
throw InsertionError.settingValueOnAContainer | ||
} | ||
} | ||
|
||
/// Inserts a value for a key into the node, which is interpreted as a | ||
/// dictionary. | ||
/// - Parameters: | ||
/// - childValue: The value to save under the provided key. | ||
/// - key: The key to save the value for into the dictionary. | ||
/// - Throws: If the node is already set to be anything else but a | ||
/// dictionary. | ||
mutating func insert<Key: CodingKey>( | ||
_ childValue: Self, | ||
atKey key: Key | ||
) throws { | ||
switch self { | ||
case .dictionary(var dictionary): | ||
self = .unset | ||
dictionary[key.stringValue] = childValue | ||
self = .dictionary(dictionary) | ||
case .array(var array): | ||
// Check that this is a valid key for an unkeyed container, | ||
// but don't actually extract the index, we only support appending | ||
// here. | ||
guard let intValue = key.intValue else { | ||
throw InsertionError.insertingChildValueIntoArrayUsingNonIntValueKey | ||
} | ||
precondition( | ||
intValue == array.count, | ||
"Unkeyed container inserting at an incorrect index" | ||
) | ||
self = .unset | ||
array.append(childValue) | ||
self = .array(array) | ||
case .unset: | ||
if let _ = key.intValue { | ||
self = .array([childValue]) | ||
} else { | ||
self = .dictionary([key.stringValue: childValue]) | ||
} | ||
default: | ||
throw InsertionError.insertingChildValueIntoNonContainer | ||
} | ||
} | ||
|
||
/// Appends a value to the array node. | ||
/// - Parameter childValue: The node to append to the underlying array. | ||
/// - Throws: If the node is already set to be anything else but an array. | ||
mutating func append(_ childValue: Self) throws { | ||
switch self { | ||
case .array(var items): | ||
self = .unset | ||
items.append(childValue) | ||
self = .array(items) | ||
case .unset: | ||
self = .array([childValue]) | ||
default: | ||
throw InsertionError.appendingToNonArrayContainer | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
Sources/OpenAPIRuntime/URICoder/Common/URIParsedNode.swift
This file contains hidden or 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,27 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the SwiftOpenAPIGenerator open source project | ||
// | ||
// Copyright (c) 2023 Apple Inc. and the SwiftOpenAPIGenerator project authors | ||
// Licensed under Apache License v2.0 | ||
// | ||
// See LICENSE.txt for license information | ||
// See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import Foundation | ||
|
||
/// The type used for keys by `URIParser`. | ||
typealias URIParsedKey = String.SubSequence | ||
|
||
/// The type used for values by `URIParser`. | ||
typealias URIParsedValue = String.SubSequence | ||
|
||
/// The type used for an array of values by `URIParser`. | ||
typealias URIParsedValueArray = [URIParsedValue] | ||
|
||
/// The type used for a node and a dictionary by `URIParser`. | ||
typealias URIParsedNode = [URIParsedKey: URIParsedValueArray] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.