@@ -174,6 +174,28 @@ public enum NodeResult {
174
174
return . node( array [ key] )
175
175
}
176
176
}
177
+
178
+ func set( value: NodeResult , key: IndexPathElement ) -> Self ? {
179
+ switch self {
180
+ case let . node( node) :
181
+ guard let key = key. keyValue else {
182
+ return nil
183
+ }
184
+ node. set ( value: value, key: key)
185
+ return . node( node)
186
+ case var . array( array) :
187
+ switch value {
188
+ case let . node( value) :
189
+ guard let key = key. indexValue else {
190
+ return nil
191
+ }
192
+ array [ key] = value
193
+ return . array( array)
194
+ case let . array( value) :
195
+ return . array( value)
196
+ }
197
+ }
198
+ }
177
199
}
178
200
179
201
/**
@@ -183,15 +205,17 @@ public protocol Node {
183
205
var kind : Kind { get }
184
206
var loc : Location ? { get }
185
207
func get( key: String ) -> NodeResult ?
186
- func set( value: Node ? , key: String )
208
+ func set( value: NodeResult ? , key: String )
187
209
}
188
210
189
211
public extension Node {
190
212
func get( key _: String ) -> NodeResult ? {
191
213
return nil
192
214
}
193
215
194
- func set( value _: Node ? , key _: String ) { }
216
+ func set( value: NodeResult ? , key: String ) {
217
+ print ( " TODO: Should be implemented on each type! " )
218
+ }
195
219
}
196
220
197
221
extension Name : Node { }
@@ -252,7 +276,7 @@ extension Name: Equatable {
252
276
public final class Document {
253
277
public let kind : Kind = . document
254
278
public let loc : Location ?
255
- public let definitions : [ Definition ]
279
+ public var definitions : [ Definition ]
256
280
257
281
init ( loc: Location ? = nil , definitions: [ Definition ] ) {
258
282
self . loc = loc
@@ -270,6 +294,24 @@ public final class Document {
270
294
return nil
271
295
}
272
296
}
297
+
298
+ public func set( value: NodeResult ? , key: String ) {
299
+ guard let value = value else {
300
+ return
301
+ }
302
+ switch key {
303
+ case " definitions " :
304
+ guard
305
+ case let . array( values) = value,
306
+ let definitions = values as? [ Definition ]
307
+ else {
308
+ return
309
+ }
310
+ self . definitions = definitions
311
+ default :
312
+ return
313
+ }
314
+ }
273
315
}
274
316
275
317
extension Document : Equatable {
@@ -323,10 +365,10 @@ public final class OperationDefinition {
323
365
public let kind : Kind = . operationDefinition
324
366
public let loc : Location ?
325
367
public let operation : OperationType
326
- public let name : Name ?
327
- public let variableDefinitions : [ VariableDefinition ]
328
- public let directives : [ Directive ]
329
- public let selectionSet : SelectionSet
368
+ public var name : Name ?
369
+ public var variableDefinitions : [ VariableDefinition ]
370
+ public var directives : [ Directive ]
371
+ public var selectionSet : SelectionSet
330
372
331
373
init (
332
374
loc: Location ? = nil ,
@@ -364,6 +406,48 @@ public final class OperationDefinition {
364
406
return nil
365
407
}
366
408
}
409
+
410
+ public func set( value: NodeResult ? , key: String ) {
411
+ guard let value = value else {
412
+ return
413
+ }
414
+ switch key {
415
+ case " name " :
416
+ guard
417
+ case let . node( node) = value,
418
+ let name = node as? Name
419
+ else {
420
+ return
421
+ }
422
+ self . name = name
423
+ case " variableDefinitions " :
424
+ guard
425
+ case let . array( values) = value,
426
+ let variableDefinitions = values as? [ VariableDefinition ]
427
+ else {
428
+ return
429
+ }
430
+ self . variableDefinitions = variableDefinitions
431
+ case " directives " :
432
+ guard
433
+ case let . array( values) = value,
434
+ let directives = values as? [ Directive ]
435
+ else {
436
+ return
437
+ }
438
+ self . directives = directives
439
+ case " selectionSet " :
440
+ guard
441
+ case let . node( value) = value,
442
+ let selectionSet = value as? SelectionSet
443
+ else {
444
+ return
445
+ }
446
+ self . selectionSet = selectionSet
447
+ default :
448
+ return
449
+ }
450
+ }
367
451
}
368
452
369
453
extension OperationDefinition : Hashable {
0 commit comments