|
14 | 14 | -- |
15 | 15 | -- Use yamlLines to transform 'RawYaml' to ['RawYamlLine']. |
16 | 16 | module Stack.YamlUpdate |
17 | | - ( encodeInOrder |
| 17 | + ( compareInOrder |
| 18 | + , encodeInOrder |
18 | 19 | , redress |
19 | 20 | , mkRaw |
20 | 21 | , unmkRaw |
@@ -131,28 +132,37 @@ fetchInRange YamlLines{blanks, wholeLineComments, partLineComments} p = |
131 | 132 | ps = filterLineNumber partLineComments |
132 | 133 | in (ps, L.sortOn commentLineNumber $ ls ++ cs) |
133 | 134 |
|
| 135 | +-- | From an ordered list of keys constructs a comparison respecting that order. |
| 136 | +preservingCompare :: Ord a => Map Text a -> [Text] -> Text -> Text -> Text -> Ordering |
| 137 | +preservingCompare ixMap keysFound k x y = |
| 138 | + -- If updating then preserve order but if inserting then put last. |
| 139 | + if | k `L.elem` keysFound -> Map.lookup x ixMap `compare` Map.lookup y ixMap |
| 140 | + | k == x, k == y -> EQ |
| 141 | + | k == x -> GT |
| 142 | + | k == y -> LT |
| 143 | + | otherwise -> Map.lookup x ixMap `compare` Map.lookup y ixMap |
| 144 | + |
| 145 | +-- | From an ordered list of YAML lines constructs a comparison respecting that order. |
| 146 | +compareInOrder :: [RawYamlLine] |
| 147 | + -> [YamlKey] |
| 148 | + -> YamlKey |
| 149 | + -> (Text -> Text -> Ordering) |
| 150 | +compareInOrder rawLines keysFound (YamlKey k) = |
| 151 | + let keyLine = findKeyLine rawLines |
| 152 | + ixMap = Map.fromList $ (\yk@(YamlKey x) -> (x, keyLine yk)) <$> keysFound |
| 153 | + |
| 154 | + in preservingCompare ixMap (coerce <$> keysFound) k |
| 155 | + |
134 | 156 | -- | Uses the order of the keys in the original to preserve the order in the |
135 | 157 | -- update except that inserting a key orders it last. |
136 | 158 | encodeInOrder :: [RawYamlLine] |
137 | 159 | -> [YamlKey] |
138 | 160 | -> YamlKey |
139 | 161 | -> Yaml.Object |
140 | 162 | -> Either UnicodeException RawYaml |
141 | | -encodeInOrder rawLines keysFound upsertKey@(YamlKey k) yObject = |
142 | | - let keyLine = findKeyLine rawLines |
143 | | - ixMap = Map.fromList $ (\yk@(YamlKey x) -> (x, keyLine yk)) <$> keysFound |
144 | | - preservingCompare x y = |
145 | | - -- If updating then preserve order but if inserting then put last. |
146 | | - if | upsertKey `L.elem` keysFound -> |
147 | | - Map.lookup x ixMap `compare` Map.lookup y ixMap |
148 | | - | k == x, k == y -> EQ |
149 | | - | k == x -> GT |
150 | | - | k == y -> LT |
151 | | - | otherwise -> Map.lookup x ixMap `compare` Map.lookup y ixMap |
152 | | - |
153 | | - keyCmp = Yaml.setConfCompare preservingCompare Yaml.defConfig |
154 | | - |
155 | | - in RawYaml <$> decodeUtf8' (Yaml.encodePretty keyCmp yObject) |
| 163 | +encodeInOrder rawLines keysFound key yObject = |
| 164 | + let keyCmp = Yaml.setConfCompare (compareInOrder rawLines keysFound key) Yaml.defConfig |
| 165 | + in RawYaml <$> decodeUtf8' (Yaml.encodePretty keyCmp yObject) |
156 | 166 |
|
157 | 167 | endSentinel :: Text |
158 | 168 | endSentinel = |
|
0 commit comments