@@ -14,12 +14,12 @@ import JSONAPI
14
14
/// and build a representation of a JSON:API Document that can handle both
15
15
/// Data and Error cases.
16
16
public struct DataDocumentSwiftGen : JSONSchemaSwiftGenerator {
17
- public let structure : JSONSchema
17
+ public let structure : DereferencedJSONSchema
18
18
public let decls : [ Decl ]
19
19
public let swiftTypeName : String
20
20
public let resourceObjectGenerators : Set < ResourceObjectSwiftGen >
21
21
public let exampleGenerator : ExampleSwiftGen ?
22
- public let testExampleFuncs : [ SwiftFunctionGenerator ]
22
+ public let testExampleFuncs : [ TestFunctionGenerator ]
23
23
24
24
/// Generate Swift code not just for this Document's declaration but
25
25
/// also for all declarations required for this Document to compile.
@@ -30,23 +30,29 @@ public struct DataDocumentSwiftGen: JSONSchemaSwiftGenerator {
30
30
. joined ( separator: " \n " )
31
31
}
32
32
33
- public init ( swiftTypeName: String ,
34
- structure: JSONSchema ,
35
- allowPlaceholders: Bool = true ,
36
- example: ExampleSwiftGen ? = nil ,
37
- testExampleFuncs: [ SwiftFunctionGenerator ] = [ ] ) throws {
33
+ public init (
34
+ swiftTypeName: String ,
35
+ structure: DereferencedJSONSchema ,
36
+ allowPlaceholders: Bool = true ,
37
+ example: ExampleSwiftGen ? = nil ,
38
+ testExampleFuncs: [ TestFunctionGenerator ] = [ ]
39
+ ) throws {
38
40
self . swiftTypeName = swiftTypeName
39
41
self . structure = structure
40
42
self . exampleGenerator = example
41
43
self . testExampleFuncs = testExampleFuncs
42
44
43
- ( decls, resourceObjectGenerators) = try DataDocumentSwiftGen . swiftDecls ( from: structure,
44
- swiftTypeName: swiftTypeName,
45
- allowPlaceholders: allowPlaceholders)
45
+ ( decls, resourceObjectGenerators) = try DataDocumentSwiftGen . swiftDecls (
46
+ from: structure,
47
+ swiftTypeName: swiftTypeName,
48
+ allowPlaceholders: allowPlaceholders
49
+ )
46
50
}
47
51
48
- static func swiftDeclsForErrorDocument( from resourceObjectContext: JSONSchema . ObjectContext ,
49
- swiftTypeName: String ) throws -> [ Decl ] {
52
+ static func swiftDeclsForErrorDocument(
53
+ from resourceObjectContext: DereferencedJSONSchema . ObjectContext ,
54
+ swiftTypeName: String
55
+ ) throws -> [ Decl ] {
50
56
guard let errorsSchema = resourceObjectContext. properties [ " errors " ] ,
51
57
case . array( _, let arrayContext) = errorsSchema,
52
58
let errorsItems = arrayContext. items else {
@@ -58,35 +64,53 @@ public struct DataDocumentSwiftGen: JSONSchemaSwiftGenerator {
58
64
59
65
let errorsItemsDecls : [ Decl ]
60
66
do { //GenericJSONAPIError<ErrorPayload>
61
- let errorTypealias = Typealias ( alias: . def( . init( name: errorTypeName) ) ,
62
- existingType: . def( . init( name: " GenericJSONAPIError " ,
63
- specializationReps: [ . def( . init( name: errorPayloadTypeName) ) ] ) ) )
67
+ let errorTypealias = Typealias (
68
+ alias: . def(
69
+ . init( name: errorTypeName)
70
+ ) ,
71
+ existingType: . def(
72
+ . init(
73
+ name: " GenericJSONAPIError " ,
74
+ specializationReps: [ . def( . init( name: errorPayloadTypeName) ) ]
75
+ )
76
+ )
77
+ )
64
78
65
- errorsItemsDecls = try StructureSwiftGen ( swiftTypeName: errorPayloadTypeName,
66
- structure: errorsItems,
67
- cascadingConformances: [ " Codable " , " Equatable " ] ) . decls
79
+ errorsItemsDecls = try StructureSwiftGen (
80
+ swiftTypeName: errorPayloadTypeName,
81
+ structure: errorsItems,
82
+ cascadingConformances: [ " Codable " , " Equatable " ]
83
+ ) . decls
68
84
+ [ errorTypealias]
69
85
} catch let error {
70
86
throw Error . failedToCreateErrorsStructure ( underlyingError: error)
71
87
}
72
88
73
- let documentTypealiasDecl = Typealias ( alias: . def( . init( name: swiftTypeName) ) ,
74
- existingType: . def( . init( name: " JSONAPI.Document " ,
75
- specializationReps: [
76
- . init( NoResourceBody . self) ,
77
- . init( NoMetadata . self) ,
78
- . init( NoLinks . self) ,
79
- . init( NoIncludes . self) ,
80
- . init( NoAPIDescription . self) ,
81
- . def( . init( name: errorTypeName) )
82
- ] ) ) )
89
+ let documentTypealiasDecl = Typealias (
90
+ alias: . def( . init( name: swiftTypeName) ) ,
91
+ existingType: . def(
92
+ . init(
93
+ name: " JSONAPI.Document " ,
94
+ specializationReps: [
95
+ . init( NoResourceBody . self) ,
96
+ . init( NoMetadata . self) ,
97
+ . init( NoLinks . self) ,
98
+ . init( NoIncludes . self) ,
99
+ . init( NoAPIDescription . self) ,
100
+ . def( . init( name: errorTypeName) )
101
+ ]
102
+ )
103
+ )
104
+ )
83
105
84
106
return errorsItemsDecls + [ documentTypealiasDecl]
85
107
}
86
108
87
- static func swiftDecls( from structure: JSONSchema ,
88
- swiftTypeName: String ,
89
- allowPlaceholders: Bool ) throws -> ( [ Decl ] , Set < ResourceObjectSwiftGen > ) {
109
+ static func swiftDecls(
110
+ from structure: DereferencedJSONSchema ,
111
+ swiftTypeName: String ,
112
+ allowPlaceholders: Bool
113
+ ) throws -> ( [ Decl ] , Set < ResourceObjectSwiftGen > ) {
90
114
guard case let . object( _, resourceObjectContextB) = structure else {
91
115
throw Error . rootNotJSONObject
92
116
}
@@ -115,19 +139,29 @@ public struct DataDocumentSwiftGen: JSONSchemaSwiftGenerator {
115
139
let primaryResourceTypeName : String
116
140
switch data {
117
141
case . object:
118
- let resourceObject = try ResourceObjectSwiftGen ( structure: data,
119
- allowPlaceholders: allowPlaceholders)
142
+ let resourceObject = try ResourceObjectSwiftGen (
143
+ structure: data,
144
+ allowPlaceholders: allowPlaceholders
145
+ )
120
146
primaryResourceTypeName = resourceObject. resourceTypeName
121
147
122
148
let isNullablePrimaryResource = data. nullable
123
149
124
150
// SingleResourceBody<PrimaryResource>
125
- primaryResourceBodyType = . def( . init( name: " SingleResourceBody " ,
126
- specializationReps: [
127
- . def( . init( name: primaryResourceTypeName,
128
- specializationReps: [ ] ,
129
- optional: isNullablePrimaryResource) )
130
- ] ) )
151
+ primaryResourceBodyType = . def(
152
+ . init(
153
+ name: " SingleResourceBody " ,
154
+ specializationReps: [
155
+ . def(
156
+ . init(
157
+ name: primaryResourceTypeName,
158
+ specializationReps: [ ] ,
159
+ optional: isNullablePrimaryResource
160
+ )
161
+ )
162
+ ]
163
+ )
164
+ )
131
165
132
166
allResourceObjectGenerators. insert ( resourceObject)
133
167
@@ -137,15 +171,25 @@ public struct DataDocumentSwiftGen: JSONSchemaSwiftGenerator {
137
171
throw Error . expectedDataArrayToDefineItems
138
172
}
139
173
140
- let resourceObject = try ResourceObjectSwiftGen ( structure: dataItem,
141
- allowPlaceholders: allowPlaceholders)
174
+ let resourceObject = try ResourceObjectSwiftGen (
175
+ structure: dataItem,
176
+ allowPlaceholders: allowPlaceholders
177
+ )
142
178
primaryResourceTypeName = resourceObject. resourceTypeName
143
179
144
- primaryResourceBodyType = . def( . init( name: " ManyResourceBody " ,
145
- specializationReps: [
146
- . def( . init( name: primaryResourceTypeName,
147
- specializationReps: [ ] ) )
148
- ] ) )
180
+ primaryResourceBodyType = . def(
181
+ . init(
182
+ name: " ManyResourceBody " ,
183
+ specializationReps: [
184
+ . def(
185
+ . init(
186
+ name: primaryResourceTypeName,
187
+ specializationReps: [ ]
188
+ )
189
+ )
190
+ ]
191
+ )
192
+ )
149
193
150
194
allResourceObjectGenerators. insert ( resourceObject)
151
195
@@ -169,35 +213,53 @@ public struct DataDocumentSwiftGen: JSONSchemaSwiftGenerator {
169
213
switch items {
170
214
case . one( of: let resourceTypeSchemas, _) :
171
215
resources = try Array ( Set ( resourceTypeSchemas. map {
172
- try ResourceObjectSwiftGen ( structure: $0,
173
- allowPlaceholders: allowPlaceholders)
216
+ try ResourceObjectSwiftGen (
217
+ structure: $0,
218
+ allowPlaceholders: allowPlaceholders
219
+ )
174
220
} ) ) . sorted { $0. resourceTypeName < $1. resourceTypeName }
175
221
default :
176
- resources = [ try ResourceObjectSwiftGen ( structure: items,
177
- allowPlaceholders: allowPlaceholders) ]
222
+ resources = [
223
+ try ResourceObjectSwiftGen (
224
+ structure: items,
225
+ allowPlaceholders: allowPlaceholders
226
+ )
227
+ ]
178
228
}
179
229
180
230
let resourceTypes = resources. map { SwiftTypeRep . def ( . init( name: $0. resourceTypeName) ) }
181
231
182
- includeType = . def( . init( name: " Include \( resourceTypes. count) " ,
183
- specializationReps: resourceTypes) )
232
+ includeType = . def(
233
+ . init(
234
+ name: " Include \( resourceTypes. count) " ,
235
+ specializationReps: resourceTypes
236
+ )
237
+ )
184
238
185
239
186
240
allResourceObjectGenerators = allResourceObjectGenerators. union ( resources)
187
241
} else {
188
242
includeType = . rep( NoIncludes . self)
189
243
}
190
244
191
- allDecls. append ( Typealias ( alias: . def( . init( name: swiftTypeName) ) ,
192
- existingType: . def( . init( name: " JSONAPI.Document " ,
193
- specializationReps: [
194
- primaryResourceBodyType,
195
- . init( NoMetadata . self) ,
196
- . init( NoLinks . self) ,
197
- includeType,
198
- . init( NoAPIDescription . self) ,
199
- " BasicJSONAPIError<AnyCodable> "
200
- ] ) ) ) )
245
+ allDecls. append (
246
+ Typealias (
247
+ alias: . def( . init( name: swiftTypeName) ) ,
248
+ existingType: . def(
249
+ . init(
250
+ name: " JSONAPI.Document " ,
251
+ specializationReps: [
252
+ primaryResourceBodyType,
253
+ . init( NoMetadata . self) ,
254
+ . init( NoLinks . self) ,
255
+ includeType,
256
+ . init( NoAPIDescription . self) ,
257
+ " BasicJSONAPIError<AnyCodable> "
258
+ ]
259
+ )
260
+ )
261
+ )
262
+ )
201
263
202
264
return ( allDecls, allResourceObjectGenerators)
203
265
}
0 commit comments