Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Do not serialize empty collections (#30)
This is a temporary workaround before aaubry/YamlDotNet#608 gets merged. This makes the YAML not contain bunch of rows like this: ``` variables: [] ```
- Loading branch information
Showing
3 changed files
with
51 additions
and
1 deletion.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
src/Sharpliner/GraphTraversal/SharplinerGraphTraversalStrategy.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using YamlDotNet.Serialization; | ||
using System.Collections.Generic; | ||
using System.Collections; | ||
using YamlDotNet.Serialization.ObjectGraphTraversalStrategies; | ||
|
||
namespace Sharpliner.GraphTraversal | ||
{ | ||
/// <summary> | ||
/// This is a workaround that skips empty collections and doesn't serialize them at all. | ||
/// The proper way to do this is in a PR here: https://github.com/aaubry/YamlDotNet/pull/608 | ||
/// Before that is fixed, this is how we're going to do things (╯°□°)╯︵ ┻━┻ | ||
/// </summary> | ||
public class SharplinerGraphTraversalStrategy : FullObjectGraphTraversalStrategy | ||
{ | ||
private readonly ITypeInspector _typeDescriptor; | ||
|
||
public SharplinerGraphTraversalStrategy(ITypeInspector typeDescriptor, ITypeResolver typeResolver, int maxRecursion, INamingConvention namingConvention) | ||
: base(typeDescriptor, typeResolver, maxRecursion, namingConvention) | ||
{ | ||
_typeDescriptor = typeDescriptor; | ||
} | ||
|
||
protected override void TraverseProperties<TContext>(IObjectDescriptor value, IObjectGraphVisitor<TContext> visitor, TContext context, Stack<ObjectPathSegment> path) | ||
{ | ||
visitor.VisitMappingStart(value, typeof(string), typeof(object), context); | ||
|
||
var source = value.NonNullValue(); | ||
foreach (var propertyDescriptor in _typeDescriptor.GetProperties(value.Type, source)) | ||
{ | ||
var propertyValue = propertyDescriptor.Read(source); | ||
|
||
if (visitor.EnterMapping(propertyDescriptor, propertyValue, context)) | ||
{ | ||
// This is the actual change | ||
if (propertyValue.Value is IEnumerable collection && !collection.GetEnumerator().MoveNext()) | ||
{ | ||
// Skip empty arrays/collections | ||
continue; | ||
} | ||
|
||
Traverse(propertyDescriptor.Name, new ObjectDescriptor(propertyDescriptor.Name, typeof(string), typeof(string)), visitor, context, path); | ||
Traverse(propertyDescriptor.Name, propertyValue, visitor, context, path); | ||
} | ||
} | ||
|
||
visitor.VisitMappingEnd(value, context); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters