generated from dankinsoid/iOSLibraryTemplate
-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
SchemeEncoder.swift
173 lines (156 loc) · 4.81 KB
/
SchemeEncoder.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import Foundation
struct SchemeEncoder {
var extractReferences = true
var dateFormat: DateEncodingFormat
var keyEncodingStrategy: KeyEncodingStrategy
@discardableResult
func encode(
_ value: Encodable,
into schemas: inout ComponentsMap<SchemaObject>
) throws -> ReferenceOr<SchemaObject> {
try parse(
value: TypeRevision().describeType(of: value),
type: Swift.type(of: value),
into: &schemas
)
}
@discardableResult
func decode(
_ type: Decodable.Type,
into schemas: inout ComponentsMap<SchemaObject>
) throws -> ReferenceOr<SchemaObject> {
try parse(
value: TypeRevision().describe(type: type),
type: type,
into: &schemas
)
}
func parse(
value: TypeInfo,
type: Any.Type,
into schemas: inout ComponentsMap<SchemaObject>
) throws -> ReferenceOr<SchemaObject> {
var needReparse = false
var cache: [ObjectIdentifier: ReferenceOr<SchemaObject>] = [:]
var result = try parse(
value: value,
type: type,
into: &schemas,
cache: &cache,
needReparse: &needReparse
)
if needReparse {
result = try parse(
value: value,
type: type,
into: &schemas,
cache: &cache,
needReparse: &needReparse
)
}
return result
}
func parse(
value: TypeInfo,
type: Any.Type,
into schemas: inout ComponentsMap<SchemaObject>,
cache: inout [ObjectIdentifier: ReferenceOr<SchemaObject>],
needReparse: inout Bool
) throws -> ReferenceOr<SchemaObject> {
let name = String.typeName(type)
var result: ReferenceOr<SchemaObject>
let typeInfo = value
let typeID = ObjectIdentifier(typeInfo.type)
switch type {
case is Date.Type:
result = .value(dateFormat.schema)
case let openAPI as OpenAPIType.Type:
result = .value(openAPI.openAPISchema)
default:
switch typeInfo.container {
case let .single(codableValues):
let (dataType, format) = try parse(value: codableValues)
let schema: SchemaObject
if let iterable = type as? any CaseIterable.Type {
let allCases = iterable.allCases as any Collection
schema = .enum(of: dataType, cases: allCases.map { .string(caseValue(for: $0)) })
} else {
schema = SchemaObject(format: format, context: SchemaContexts(dataType))
}
result = .value(schema)
case let .keyed(keyedInfo):
switch keyedInfo.isFixed {
case true:
let schema = try SchemaObject.object(
properties: keyedInfo.fields.mapKeys {
keyEncodingStrategy.encode($0)
}.mapValues {
try parse(value: $0, type: $0.type, into: &schemas, cache: &cache, needReparse: &needReparse)
},
required: Set(keyedInfo.fields.unorderedHash.filter { !$0.value.isOptional }.keys)
)
result = .value(schema)
case false:
let schema = try SchemaObject.dictionary(
of: (keyedInfo.fields.first?.value).map {
try parse(value: $0, type: $0.type, into: &schemas, cache: &cache, needReparse: &needReparse)
} ?? .any
)
result = .value(schema)
}
case let .unkeyed(itemInfo):
let schema = try SchemaObject.array(
of: parse(value: itemInfo, type: itemInfo.type, into: &schemas, cache: &cache, needReparse: &needReparse)
)
result = .value(schema)
case .recursive:
needReparse = true
if let cached = cache[typeID] {
result = cached
} else {
result = .ref(components: \.schemas, name)
}
}
}
if let descriptable = type as? OpenAPIDescriptable.Type {
result = result.with(description: descriptable.openAPIDescription)
}
if extractReferences, result.isReferenceable {
result.object?.nullable = nil
schemas[name] = result
cache[typeID] = .ref(components: \.schemas, name)
return .ref(components: \.schemas, name)
} else {
if typeInfo.isOptional, result.object?.enum == nil {
result.object?.nullable = true
}
cache[typeID] = result
return result
}
}
private func parse(
value: CodableValues
) throws -> (PrimitiveDataType, DataFormat?) {
switch value {
case .int8, .int16, .int32, .uint8, .uint16, .uint32:
return (.integer, .int32)
case .int, .int64, .uint, .uint64:
return (.integer, .int64)
case .double:
return (.number, .double)
case .float:
return (.number, .float)
case .bool:
return (.boolean, nil)
case .string, .null:
return (.string, nil)
}
}
private func caseValue(for value: Any) -> String {
if let anyRaw = value as? any RawRepresentable {
return "\(anyRaw.rawValue)"
} else {
return "\(value)"
}
}
}