Skip to content

Commit 763a353

Browse files
Add UseSequenceFlowStyle option
This option uses the Flow style only for sequences. Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>
1 parent a86e5b9 commit 763a353

4 files changed

Lines changed: 24 additions & 5 deletions

File tree

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

powershell-yaml.psm1

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ enum SerializationOptions {
2424
WithIndentedSequences = 32
2525
OmitNullValues = 64
2626
UseFlowStyle = 128
27+
UseSequenceFlowStyle = 256
2728
}
2829
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
2930
$infinityRegex = [regex]::new('^[-+]?(\.inf|\.Inf|\.INF)$', "Compiled, CultureInvariant");
@@ -415,9 +416,10 @@ function Get-Serializer {
415416

416417
$omitNull = $Options.HasFlag([SerializationOptions]::OmitNullValues)
417418
$useFlowStyle = $Options.HasFlag([SerializationOptions]::UseFlowStyle)
419+
$useSequenceFlowStyle = $Options.HasFlag([SerializationOptions]::UseSequenceFlowStyle)
418420

419421
$stringQuoted = $stringQuotedAssembly.GetType("BuilderUtils")
420-
$builder = $stringQuoted::BuildSerializer($builder, $omitNull, $useFlowStyle)
422+
$builder = $stringQuoted::BuildSerializer($builder, $omitNull, $useFlowStyle, $useSequenceFlowStyle)
421423

422424
return $builder.Build()
423425
}

src/PowerShellYamlSerializer.cs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,8 @@ public override void Emit(ScalarEventInfo eventInfo, IEmitter emitter) {
119119
}
120120
}
121121

122-
public class FlowStyleEmitter: ChainedEventEmitter {
123-
public FlowStyleEmitter(IEventEmitter next): base(next) {}
122+
public class FlowStyleAllEmitter: ChainedEventEmitter {
123+
public FlowStyleAllEmitter(IEventEmitter next): base(next) {}
124124

125125
public override void Emit(MappingStartEventInfo eventInfo, IEmitter emitter) {
126126
eventInfo.Style = MappingStyle.Flow;
@@ -133,8 +133,21 @@ public override void Emit(SequenceStartEventInfo eventInfo, IEmitter emitter){
133133
}
134134
}
135135

136+
public class FlowStyleSequenceEmitter: ChainedEventEmitter {
137+
public FlowStyleSequenceEmitter(IEventEmitter next): base(next) {}
138+
139+
public override void Emit(SequenceStartEventInfo eventInfo, IEmitter emitter){
140+
eventInfo.Style = SequenceStyle.Flow;
141+
nextEmitter.Emit(eventInfo, emitter);
142+
}
143+
}
144+
136145
class BuilderUtils {
137-
public static SerializerBuilder BuildSerializer(SerializerBuilder builder, bool omitNullValues = false, bool useFlowStyle = false) {
146+
public static SerializerBuilder BuildSerializer(
147+
SerializerBuilder builder,
148+
bool omitNullValues = false,
149+
bool useFlowStyle = false,
150+
bool useSequenceFlowStyle = false) {
138151
builder = builder
139152
.WithEventEmitter(next => new StringQuotingEmitter(next))
140153
.WithTypeConverter(new BigIntegerTypeConverter())
@@ -144,8 +157,12 @@ public static SerializerBuilder BuildSerializer(SerializerBuilder builder, bool
144157
.WithEmissionPhaseObjectGraphVisitor(args => new NullValueGraphVisitor(args.InnerVisitor));
145158
}
146159
if (useFlowStyle == true) {
147-
builder = builder.WithEventEmitter(next => new FlowStyleEmitter(next));
160+
builder = builder.WithEventEmitter(next => new FlowStyleAllEmitter(next));
148161
}
162+
if (useSequenceFlowStyle == true) {
163+
builder = builder.WithEventEmitter(next => new FlowStyleSequenceEmitter(next));
164+
}
165+
149166
return builder;
150167
}
151168
}

0 commit comments

Comments
 (0)