forked from apple/swift-openapi-runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathURIDecoder.swift
97 lines (90 loc) · 4.45 KB
/
URIDecoder.swift
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
//===----------------------------------------------------------------------===//
//
// 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 type that decodes a `Decodable` value from an URI-encoded string
/// using the rules from RFC 6570, RFC 1866, and OpenAPI 3.0.4, depending on
/// the configuration.
///
/// [RFC 6570 - Form-style query expansion.](https://datatracker.ietf.org/doc/html/rfc6570#section-3.2.8)
///
/// | Example Template | Expansion |
/// | ---------------- | ----------------------------------|
/// | `{?who}` | `?who=fred` |
/// | `{?half}` | `?half=50%25` |
/// | `{?x,y}` | `?x=1024&y=768` |
/// | `{?x,y,empty}` | `?x=1024&y=768&empty=` |
/// | `{?x,y,undef}` | `?x=1024&y=768` |
/// | `{?list}` | `?list=red,green,blue` |
/// | `{?list\*}` | `?list=red&list=green&list=blue` |
/// | `{?keys}` | `?keys=semi,%3B,dot,.,comma,%2C` |
/// | `{?keys\*}` | `?semi=%3B&dot=.&comma=%2C` |
///
/// [RFC 6570 - Simple string expansion.](https://datatracker.ietf.org/doc/html/rfc6570#section-3.2.2)
///
/// | Example Template | Expansion |
/// | ---------------- | ----------------------------------|
/// | `{hello}` | `Hello%20World%21` |
/// | `{half}` | `50%25` |
/// | `{x,y}` | `1024,768` |
/// | `{x,empty}` | `1024,` |
/// | `{x,undef}` | `1024` |
/// | `{list}` | `red,green,blue` |
/// | `{list\*}` | `red,green,blue` |
/// | `{keys}` | `semi,%3B,dot,.,comma,%2C` |
/// | `{keys\*}` | `semi=%3B,dot=.,comma=%2C` |
///
/// [OpenAPI 3.0.4 - Deep object expansion.](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.4.md#style-examples)
///
/// | Example Template | Expansion |
/// | ---------------- | ----------------------------------------------------------|
/// | `{?keys\*}` | `?keys%5Bsemi%5D=%3B&keys%5Bdot%5D=.&keys%5Bcomma%5D=%2C` |
///
struct URIDecoder: Sendable {
/// The configuration instructing the decoder how to interpret the raw
/// string.
private let configuration: URICoderConfiguration
/// Creates a new decoder with the provided configuration.
/// - Parameter configuration: The configuration used by the decoder.
init(configuration: URICoderConfiguration) { self.configuration = configuration }
}
extension URIDecoder {
/// Attempt to decode an object from an URI string.
///
/// - Parameters:
/// - type: The type to decode.
/// - key: The key of the decoded value. Only used with certain styles
/// and explode options, ignored otherwise.
/// - data: The URI-encoded string.
/// - Returns: The decoded value.
/// - Throws: An error if decoding fails, for example, due to incompatible data or key.
func decode<T: Decodable>(_ type: T.Type = T.self, forKey key: String = "", from data: Substring) throws -> T {
let decoder = URIValueFromNodeDecoder(data: data, rootKey: key[...], configuration: configuration)
return try decoder.decodeRoot(type)
}
/// Attempt to decode an object from an URI string, if present.
///
/// - Parameters:
/// - type: The type to decode.
/// - key: The key of the decoded value. Only used with certain styles
/// and explode options, ignored otherwise.
/// - data: The URI-encoded string.
/// - Returns: The decoded value.
/// - Throws: An error if decoding fails, for example, due to incompatible data or key.
func decodeIfPresent<T: Decodable>(_ type: T.Type = T.self, forKey key: String = "", from data: Substring) throws
-> T?
{
let decoder = URIValueFromNodeDecoder(data: data, rootKey: key[...], configuration: configuration)
return try decoder.decodeRootIfPresent(type)
}
}