-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathJSONDynamicReference.swift
125 lines (107 loc) · 4.14 KB
/
JSONDynamicReference.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
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
//
// JSONDynamicReference.swift
//
//
// Created by Mathew Polzin.
//
import OpenAPIKitCore
import Foundation
@dynamicMemberLookup
public struct JSONDynamicReference: Equatable, Hashable {
public let jsonReference: JSONReference<JSONSchema>
public init(
_ reference: JSONReference<JSONSchema>
) {
self.jsonReference = reference
}
public subscript<T>(dynamicMember path: KeyPath<JSONReference<JSONSchema>, T>) -> T {
return jsonReference[keyPath: path]
}
/// Reference a component of type `ReferenceType` in the
/// Components Object.
///
/// Example:
///
/// JSONDynamicReference.component(named: "greetings")
/// // encoded string: "#/components/schemas/greetings"
/// // Swift: `document.components.schemas["greetings"]`
public static func component(named name: String) -> Self {
return .init(.internal(.component(name: name)))
}
/// Reference a dynamic anchor local to this file.
///
/// - Important: The anchor does not contain a leading '#'.
public static func anchor(_ anchor: String) -> Self {
return .init(.internal(.anchor(anchor)))
}
/// Reference an external URL.
public static func external(_ url: URL) -> Self {
return .init(.external(url))
}
/// `true` for internal references, `false` for
/// external references (i.e. to another file).
public var isInternal: Bool {
return jsonReference.isInternal
}
/// `true` for external references, `false` for
/// internal references.
public var isExternal: Bool {
return jsonReference.isExternal
}
/// Get the name of the referenced object. This method returns optional
/// because a reference to an external file might not have any path if the
/// file itself is the referenced component.
public var name: String? {
return jsonReference.name
}
/// The absolute value of an external reference's
/// URL or the path fragment string for a local
/// reference as defined in [RFC 3986](https://tools.ietf.org/html/rfc3986).
public var absoluteString: String {
return jsonReference.absoluteString
}
}
extension JSONDynamicReference {
private enum CodingKeys: String, CodingKey {
case dynamicRef = "$dynamicRef"
}
}
extension JSONDynamicReference: Encodable {
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
switch self.jsonReference {
case .internal(let reference):
try container.encode(reference.rawValue, forKey: .dynamicRef)
case .external(uri: let url):
try container.encode(url.absoluteString, forKey: .dynamicRef)
}
}
}
extension JSONDynamicReference: Decodable {
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let referenceString = try container.decode(String.self, forKey: .dynamicRef)
guard referenceString.count > 0 else {
throw DecodingError.dataCorruptedError(forKey: .dynamicRef, in: container, debugDescription: "Expected a reference string, but found an empty string instead.")
}
if referenceString.first == "#" {
guard let internalReference = JSONReference<JSONSchema>.InternalReference(rawValue: referenceString) else {
throw InconsistencyError(
subjectName: "JSON Dynamic Reference",
details: "Failed to parse a JSON Dynamic Reference from '\(referenceString)'",
codingPath: container.codingPath
)
}
self = .init(.internal(internalReference))
} else {
guard let externalReference = URL(string: referenceString) else {
throw InconsistencyError(
subjectName: "JSON Dynamic Reference",
details: "Failed to parse a valid URI for a JSON Dynamic Reference from '\(referenceString)'",
codingPath: container.codingPath
)
}
self = .init(.external(externalReference))
}
}
}