-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor URIDecoder/URIParser to improve handling of the deepObject s…
…tyle (#127) ### Motivation As part of apple/swift-openapi-generator#259, adding deepObject parameter style support, the initial PR wasn't complete. And once we dug more into it, turns out the original implementation of the URIDecoder/URIParser didn't really lend themselves well for handling deepObject, and the recent additions of supporting arrays within dictionaries (#120) further confused the implementation. ### Modifications Refactored URIParser/URIDecoder with a clearer understanding of the current requirements. It's now much easier to follow and embraces the fact that each of the 7 variants of URI coding we support (form exploded, form unexploded, simple exploded, simple unexploded, form data exploded, form data unexploded, and now deepObject exploded) are similar, but still different in subtle ways. This new implementation doesn't try as hard to share code between the implementations, so might at first sight appear to duplicate code. The original implementation had many methods with many configuration parameters and utility methods with a high cyclomatic complexity, which made it very hard to reason about. We did away with that. While there, I also made some minor improvements to the serialization path, which allows cleaner round-tripping tests. ### Result A more maintainable and more correct URI decoder/parser implementation. ### Test Plan Added many more unit tests that test the full matrix of supported styles and inputs. --------- Co-authored-by: Si Beaumont <simonjbeaumont@gmail.com>
- Loading branch information
1 parent
3d5d957
commit 5e119a3
Showing
21 changed files
with
1,475 additions
and
725 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
27 changes: 0 additions & 27 deletions
27
Sources/OpenAPIRuntime/URICoder/Common/URIParsedNode.swift
This file was deleted.
Oops, something went wrong.
63 changes: 63 additions & 0 deletions
63
Sources/OpenAPIRuntime/URICoder/Common/URIParsedTypes.swift
This file contains 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,63 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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 component of a `URIParsedKey`. | ||
typealias URIParsedKeyComponent = String.SubSequence | ||
|
||
/// A parsed key for a parsed value. | ||
/// | ||
/// For example, `foo=bar` in a `form` string would parse the key as `foo` (single component). | ||
/// In an unexploded `form` string `root=foo,bar`, the key would be `root/foo` (two components). | ||
/// In a `simple` string `bar`, the key would be empty (0 components). | ||
struct URIParsedKey: Hashable { | ||
|
||
/// The individual string components. | ||
let components: [URIParsedKeyComponent] | ||
|
||
/// Creates a new parsed key. | ||
/// - Parameter components: The key components. | ||
init(_ components: [URIParsedKeyComponent]) { self.components = components } | ||
|
||
/// A new empty key. | ||
static var empty: Self { .init([]) } | ||
} | ||
|
||
/// A primitive value produced by `URIParser`. | ||
typealias URIParsedValue = String.SubSequence | ||
|
||
/// A key-value produced by `URIParser`. | ||
struct URIParsedPair: Equatable { | ||
|
||
/// The key of the pair. | ||
/// | ||
/// In `foo=bar`, `foo` is the key. | ||
var key: URIParsedKey | ||
|
||
/// The value of the pair. | ||
/// | ||
/// In `foo=bar`, `bar` is the value. | ||
var value: URIParsedValue | ||
} | ||
|
||
// MARK: - Extensions | ||
|
||
extension URIParsedKey: CustomStringConvertible { | ||
// swift-format-ignore: AllPublicDeclarationsHaveDocumentation | ||
var description: String { | ||
if components.isEmpty { return "<empty>" } | ||
return components.joined(separator: "/") | ||
} | ||
} |
This file contains 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
This file contains 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
Oops, something went wrong.