|
15 | 15 | /// Visitor pattern. Conform your type to this protocol and pass |
16 | 16 | /// it to ``FlagPole/walk(visitor:)`` or any container using |
17 | 17 | /// ``FlagContainer/walk(visitor:)``. |
| 18 | +/// |
| 19 | +/// Walking always starts at a Container, and then walks the children of that container. |
| 20 | +/// When one of the children is a group, a call to ``beginGroup(keyPath:wigwag:)`` is made before |
| 21 | +/// descending into the group's container. That container will then call ``beginContainer(keyPath:container:)`` |
| 22 | +/// itself. You can use this to differentiate between the operations you are looking for. |
| 23 | +/// |
| 24 | +/// # Example |
| 25 | +/// |
| 26 | +/// Given the following flag hierarchy: |
| 27 | +/// |
| 28 | +/// ```swift |
| 29 | +/// @FlagContainer |
| 30 | +/// struct TestFlags { |
| 31 | +/// |
| 32 | +/// @Flag(...) |
| 33 | +/// var topLevelFlag: Bool |
| 34 | +/// |
| 35 | +/// @FlagGroup(...) |
| 36 | +/// var subgroup: SubgroupFlags |
| 37 | +/// |
| 38 | +/// } |
| 39 | +/// |
| 40 | +/// @FlagContainer |
| 41 | +/// struct SubgroupFlags { |
| 42 | +/// |
| 43 | +/// @FlagGroup(...) |
| 44 | +/// var doubleSubgroup: DoubleSubgroupFlags |
| 45 | +/// |
| 46 | +/// } |
| 47 | +/// |
| 48 | +/// @FlagContainer |
| 49 | +/// struct DoubleSubgroupFlags { |
| 50 | +/// |
| 51 | +/// @Flag(...) |
| 52 | +/// var thirdLevelFlag: Bool |
| 53 | +/// |
| 54 | +/// } |
| 55 | +/// ``` |
| 56 | +/// |
| 57 | +/// You should expect to see the following callbacks: |
| 58 | +/// |
| 59 | +/// ```swift |
| 60 | +/// visitor.beginContainer("") // root |
| 61 | +/// visitor.visitFlag("top-level-flag") |
| 62 | +/// visitor.beginGroup("subgroup") |
| 63 | +/// |
| 64 | +/// visitor.beginContainer("subgroup") |
| 65 | +/// visitor.beginGroup("subgroup.double-subgroup") |
| 66 | +/// |
| 67 | +/// visitor.beginContainer("subgroup.double-subgroup") |
| 68 | +/// visitor.visitFlag("subgroup.double-subgroup.third-level-flag") |
| 69 | +/// visitor.endContainer("subgroup.double-subgroup") |
| 70 | +/// |
| 71 | +/// visitor.endGroup("subgroup.double-subgroup") |
| 72 | +/// visitor.endContainer("subgroup") |
| 73 | +/// |
| 74 | +/// visitor.endGroup("subgroup") |
| 75 | +/// visitor.endContainer("") // root |
| 76 | +/// ``` |
| 77 | +/// |
18 | 78 | public protocol FlagVisitor { |
19 | 79 |
|
20 | | - /// Called when beginning to visit a new ``FlagGroup`` |
21 | | - func beginGroup(keyPath: FlagKeyPath) |
| 80 | + /// Called when beginning to walk within a ``FlagContainer`` |
| 81 | + func beginContainer(keyPath: FlagKeyPath, containerType: Any.Type) |
| 82 | + |
| 83 | + /// Called when finished visiting a ``FlagContainer``. |
| 84 | + func endContainer(keyPath: FlagKeyPath) |
| 85 | + |
| 86 | + /// Called when about to descend into a new ``FlagGroup`` |
| 87 | + func beginGroup(keyPath: FlagKeyPath, wigwag: () -> FlagGroupWigwag<some FlagContainer>) |
22 | 88 |
|
23 | 89 | /// Called when finished visiting a ``FlagGroup`` |
24 | 90 | func endGroup(keyPath: FlagKeyPath) |
@@ -49,7 +115,15 @@ public protocol FlagVisitor { |
49 | 115 |
|
50 | 116 | public extension FlagVisitor { |
51 | 117 |
|
52 | | - func beginGroup(keyPath: FlagKeyPath) { |
| 118 | + func beginContainer(keyPath: FlagKeyPath, containerType: Any.Type) { |
| 119 | + // Intentionally left blank |
| 120 | + } |
| 121 | + |
| 122 | + func endContainer(keyPath: FlagKeyPath) { |
| 123 | + // Intentionally left blank |
| 124 | + } |
| 125 | + |
| 126 | + func beginGroup(keyPath: FlagKeyPath, wigwag: () -> FlagGroupWigwag<some FlagContainer>) { |
53 | 127 | // Intentionally left blank |
54 | 128 | } |
55 | 129 |
|
|
0 commit comments