@@ -254,17 +254,17 @@ public static List<MiniYamlNode> FromString(string text, string fileName = "<no
254
254
return FromLines ( text . Split ( new [ ] { "\r \n " , "\n " } , StringSplitOptions . RemoveEmptyEntries ) , fileName ) ;
255
255
}
256
256
257
- public static List < MiniYamlNode > MergeLiberal ( List < MiniYamlNode > a , List < MiniYamlNode > b )
257
+ public static List < MiniYamlNode > MergeStrict ( List < MiniYamlNode > a , List < MiniYamlNode > b )
258
258
{
259
259
return Merge ( a , b , false ) ;
260
260
}
261
261
262
- public static List < MiniYamlNode > MergeStrict ( List < MiniYamlNode > a , List < MiniYamlNode > b )
262
+ public static List < MiniYamlNode > MergeLiberal ( List < MiniYamlNode > a , List < MiniYamlNode > b )
263
263
{
264
264
return Merge ( a , b , true ) ;
265
265
}
266
266
267
- static List < MiniYamlNode > Merge ( List < MiniYamlNode > a , List < MiniYamlNode > b , bool throwErrors )
267
+ static List < MiniYamlNode > Merge ( List < MiniYamlNode > a , List < MiniYamlNode > b , bool allowUnresolvedRemoves = false )
268
268
{
269
269
if ( a . Count == 0 )
270
270
return b ;
@@ -275,58 +275,67 @@ static List<MiniYamlNode> Merge(List<MiniYamlNode> a, List<MiniYamlNode> b, bool
275
275
276
276
var dictA = a . ToDictionaryWithConflictLog ( x => x . Key , "MiniYaml.Merge" , null , x => "{0} (at {1})" . F ( x . Key , x . Location ) ) ;
277
277
var dictB = b . ToDictionaryWithConflictLog ( x => x . Key , "MiniYaml.Merge" , null , x => "{0} (at {1})" . F ( x . Key , x . Location ) ) ;
278
- var keys = dictA . Keys . Union ( dictB . Keys ) . ToList ( ) ;
278
+ var allKeys = dictA . Keys . Union ( dictB . Keys ) ;
279
279
280
- var noInherit = keys . Where ( x => x . Length > 0 && x [ 0 ] == '-' )
281
- . ToDictionary ( x => x . Substring ( 1 ) , x => false ) ;
280
+ var keys = allKeys . Where ( x => x . Length == 0 || x [ 0 ] != '-' ) . ToList ( ) ;
281
+ var removeKeys = allKeys . Where ( x => x . Length > 0 && x [ 0 ] == '-' )
282
+ . Select ( k => k . Substring ( 1 ) ) . ToHashSet ( ) ;
282
283
283
284
foreach ( var key in keys )
284
285
{
285
286
MiniYamlNode aa , bb ;
286
287
dictA . TryGetValue ( key , out aa ) ;
287
288
dictB . TryGetValue ( key , out bb ) ;
288
289
289
- if ( noInherit . ContainsKey ( key ) )
290
- {
291
- if ( ! throwErrors )
292
- if ( aa != null )
293
- ret . Add ( aa ) ;
294
-
295
- noInherit [ key ] = true ;
296
- }
290
+ if ( removeKeys . Contains ( key ) )
291
+ removeKeys . Remove ( key ) ;
297
292
else
298
293
{
299
294
var loc = aa == null ? default ( MiniYamlNode . SourceLocation ) : aa . Location ;
300
- var merged = ( aa == null || bb == null ) ? aa ?? bb : new MiniYamlNode ( key , Merge ( aa . Value , bb . Value , throwErrors ) , loc ) ;
295
+ var merged = ( aa == null || bb == null ) ? aa ?? bb : new MiniYamlNode ( key , Merge ( aa . Value , bb . Value , allowUnresolvedRemoves ) , loc ) ;
301
296
ret . Add ( merged ) ;
302
297
}
303
298
}
304
299
305
- if ( throwErrors && noInherit . ContainsValue ( false ) )
306
- throw new YamlException ( "Bogus yaml removals: {0}" . F (
307
- noInherit . Where ( x => ! x . Value ) . JoinWith ( ", " ) ) ) ;
300
+ if ( removeKeys . Any ( ) )
301
+ {
302
+ if ( allowUnresolvedRemoves )
303
+ {
304
+ // Add the removal nodes back for the next pass to deal with
305
+ foreach ( var k in removeKeys )
306
+ {
307
+ var key = "-" + k ;
308
+ MiniYamlNode rem ;
309
+ if ( ! dictA . TryGetValue ( key , out rem ) )
310
+ rem = dictB [ key ] ;
311
+ ret . Add ( rem ) ;
312
+ }
313
+ }
314
+ else
315
+ throw new YamlException ( "Bogus yaml removals: {0}" . F ( removeKeys . JoinWith ( ", " ) ) ) ;
316
+ }
308
317
309
318
return ret ;
310
319
}
311
320
312
321
public static MiniYaml MergeLiberal ( MiniYaml a , MiniYaml b )
313
322
{
314
- return Merge ( a , b , false ) ;
323
+ return Merge ( a , b , true ) ;
315
324
}
316
325
317
326
public static MiniYaml MergeStrict ( MiniYaml a , MiniYaml b )
318
327
{
319
- return Merge ( a , b , true ) ;
328
+ return Merge ( a , b , false ) ;
320
329
}
321
330
322
- static MiniYaml Merge ( MiniYaml a , MiniYaml b , bool throwErrors )
331
+ static MiniYaml Merge ( MiniYaml a , MiniYaml b , bool allowUnresolvedRemoves )
323
332
{
324
333
if ( a == null )
325
334
return b ;
326
335
if ( b == null )
327
336
return a ;
328
337
329
- return new MiniYaml ( a . Value ?? b . Value , Merge ( a . Nodes , b . Nodes , throwErrors ) ) ;
338
+ return new MiniYaml ( a . Value ?? b . Value , Merge ( a . Nodes , b . Nodes , allowUnresolvedRemoves ) ) ;
330
339
}
331
340
332
341
public IEnumerable < string > ToLines ( string name )
0 commit comments