@@ -174,6 +174,28 @@ public enum NodeResult {
174174 return . node( array [ key] )
175175 }
176176 }
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+ }
177199}
178200
179201/**
@@ -183,15 +205,17 @@ public protocol Node {
183205 var kind : Kind { get }
184206 var loc : Location ? { get }
185207 func get( key: String ) -> NodeResult ?
186- func set( value: Node ? , key: String )
208+ func set( value: NodeResult ? , key: String )
187209}
188210
189211public extension Node {
190212 func get( key _: String ) -> NodeResult ? {
191213 return nil
192214 }
193215
194- func set( value _: Node ? , key _: String ) { }
216+ func set( value: NodeResult ? , key: String ) {
217+ print ( " TODO: Should be implemented on each type! " )
218+ }
195219}
196220
197221extension Name : Node { }
@@ -252,7 +276,7 @@ extension Name: Equatable {
252276public final class Document {
253277 public let kind : Kind = . document
254278 public let loc : Location ?
255- public let definitions : [ Definition ]
279+ public var definitions : [ Definition ]
256280
257281 init ( loc: Location ? = nil , definitions: [ Definition ] ) {
258282 self . loc = loc
@@ -270,6 +294,24 @@ public final class Document {
270294 return nil
271295 }
272296 }
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+ }
273315}
274316
275317extension Document : Equatable {
@@ -323,10 +365,10 @@ public final class OperationDefinition {
323365 public let kind : Kind = . operationDefinition
324366 public let loc : Location ?
325367 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
330372
331373 init (
332374 loc: Location ? = nil ,
@@ -364,6 +406,48 @@ public final class OperationDefinition {
364406 return nil
365407 }
366408 }
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+ }
367451}
368452
369453extension OperationDefinition : Hashable {
0 commit comments