Skip to content

Commit a86e5b9

Browse files
Add a new UseFlowStyle option
This option allows the output of sequences and mapping using the Flow style instead of the Block style. Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>
1 parent bd9c4f3 commit a86e5b9

4 files changed

Lines changed: 26 additions & 4 deletions

File tree

512 Bytes
Binary file not shown.
512 Bytes
Binary file not shown.

powershell-yaml.psm1

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ enum SerializationOptions {
2323
DefaultToStaticType = 16
2424
WithIndentedSequences = 32
2525
OmitNullValues = 64
26+
UseFlowStyle = 128
2627
}
2728
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
2829
$infinityRegex = [regex]::new('^[-+]?(\.inf|\.Inf|\.INF)$', "Compiled, CultureInvariant");
@@ -413,9 +414,10 @@ function Get-Serializer {
413414
}
414415

415416
$omitNull = $Options.HasFlag([SerializationOptions]::OmitNullValues)
417+
$useFlowStyle = $Options.HasFlag([SerializationOptions]::UseFlowStyle)
416418

417-
$stringQuoted = $stringQuotedAssembly.GetType("StringQuotingEmitter")
418-
$builder = $stringQuoted::Add($builder, $omitNull)
419+
$stringQuoted = $stringQuotedAssembly.GetType("BuilderUtils")
420+
$builder = $stringQuoted::BuildSerializer($builder, $omitNull, $useFlowStyle)
419421

420422
return $builder.Build()
421423
}
@@ -433,6 +435,7 @@ function ConvertTo-Yaml {
433435

434436
[Parameter(ParameterSetName = 'NoOptions')]
435437
[switch]$JsonCompatible,
438+
[switch]$UseFlowStyle,
436439

437440
[switch]$KeepArray,
438441

src/PowerShellYamlSerializer.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,24 @@ public override void Emit(ScalarEventInfo eventInfo, IEmitter emitter) {
117117

118118
base.Emit(eventInfo, emitter);
119119
}
120-
// objectGraphVisitor, w => w.OnTop()
121-
public static SerializerBuilder Add(SerializerBuilder builder, bool omitNullValues = false) {
120+
}
121+
122+
public class FlowStyleEmitter: ChainedEventEmitter {
123+
public FlowStyleEmitter(IEventEmitter next): base(next) {}
124+
125+
public override void Emit(MappingStartEventInfo eventInfo, IEmitter emitter) {
126+
eventInfo.Style = MappingStyle.Flow;
127+
base.Emit(eventInfo, emitter);
128+
}
129+
130+
public override void Emit(SequenceStartEventInfo eventInfo, IEmitter emitter){
131+
eventInfo.Style = SequenceStyle.Flow;
132+
nextEmitter.Emit(eventInfo, emitter);
133+
}
134+
}
135+
136+
class BuilderUtils {
137+
public static SerializerBuilder BuildSerializer(SerializerBuilder builder, bool omitNullValues = false, bool useFlowStyle = false) {
122138
builder = builder
123139
.WithEventEmitter(next => new StringQuotingEmitter(next))
124140
.WithTypeConverter(new BigIntegerTypeConverter())
@@ -127,6 +143,9 @@ public static SerializerBuilder Add(SerializerBuilder builder, bool omitNullValu
127143
builder = builder
128144
.WithEmissionPhaseObjectGraphVisitor(args => new NullValueGraphVisitor(args.InnerVisitor));
129145
}
146+
if (useFlowStyle == true) {
147+
builder = builder.WithEventEmitter(next => new FlowStyleEmitter(next));
148+
}
130149
return builder;
131150
}
132151
}

0 commit comments

Comments
 (0)