Skip to content

Commit 1827b5c

Browse files
Round trip flow style for PSCustomObject
Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>
1 parent 51eb481 commit 1827b5c

5 files changed

Lines changed: 29 additions & 4 deletions

File tree

0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.

powershell-yaml.psd1

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ New Features:
4343
* Enhanced PSCustomObject mode with metadata preservation
4444
* Validation for non-YamlBase nested classes with clear error messages
4545
* Automatic property name conversion (PascalCase -> hyphenated-case)
46+
* Added -Depth flag to ConvertTo-Yaml
47+
* Added -EmitTags to ConvertTo-Yaml
48+
* Added option to emit yaml using explicit block style (override the round-trip style from the document)
4649
4750
Usage:
4851
# PSCustomObject mode with metadata
@@ -52,6 +55,13 @@ Usage:
5255
class MyConfig : YamlBase { [string]$Name }
5356
$config = $yaml | ConvertFrom-Yaml -As ([MyConfig])
5457
58+
# Convert flow style doc to block style doc
59+
$yaml = '{hello: world, goodbye: world}'
60+
ConvertFrom-Yaml $yaml -As ([pscustomobject]) | ConvertTo-Yaml -Options UseBlockStyle
61+
hello: world
62+
goodbye: world
63+
64+
5565
See examples/ for detailed usage patterns.
5666
5767
# 0.4.12

powershell-yaml.psm1

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -612,12 +612,16 @@ function ConvertTo-Yaml {
612612
} elseif ($script:PSObjectMetadataExtensions::IsEnhancedPSCustomObject($d)) {
613613
# Use metadata-aware serializer
614614
$MetadataAwareSerializer = $script:typedModuleAssembly.GetType('PowerShellYaml.Module.MetadataAwareSerializer')
615-
# Extract indentedSequences option if using Options parameter
615+
# Extract style options if using Options parameter
616616
$indentedSequences = $false
617+
$useFlowStyle = $false
618+
$useBlockStyle = $false
617619
if ($PSCmdlet.ParameterSetName -eq 'Options') {
618620
$indentedSequences = $Options.HasFlag([SerializationOptions]::WithIndentedSequences)
621+
$useFlowStyle = $Options.HasFlag([SerializationOptions]::UseFlowStyle)
622+
$useBlockStyle = $Options.HasFlag([SerializationOptions]::UseBlockStyle)
619623
}
620-
$yaml = $MetadataAwareSerializer::Serialize($d, $indentedSequences, $EmitTags.IsPresent, $Depth)
624+
$yaml = $MetadataAwareSerializer::Serialize($d, $indentedSequences, $EmitTags.IsPresent, $Depth, $useFlowStyle, $useBlockStyle)
621625
} else {
622626
$wrt = New-Object 'System.IO.StringWriter'
623627
$norm = Convert-PSObjectToGenericObject $d

src/PowerShellYaml.Module/LegacySerializers.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -926,7 +926,7 @@ private static string GetTagFromType(object value) {
926926
};
927927
}
928928

929-
public static string Serialize(PSObject obj, bool indentedSequences = false, bool emitTags = false, int maxDepth = 100) {
929+
public static string Serialize(PSObject obj, bool indentedSequences = false, bool emitTags = false, int maxDepth = 100, bool useFlowStyle = false, bool useBlockStyle = false) {
930930
var metadata = PSObjectMetadataExtensions.GetMetadata(obj);
931931
if (metadata == null) {
932932
throw new InvalidOperationException("Object does not have YAML metadata");
@@ -954,7 +954,18 @@ public static string Serialize(PSObject obj, bool indentedSequences = false, boo
954954
emitter.Emit(new StreamStart());
955955
emitter.Emit(new DocumentStart());
956956

957-
SerializePSObject(obj, metadata, emitter, MappingStyle.Block, emitTags, 0, maxDepth);
957+
// Determine mapping style: explicit options override metadata
958+
MappingStyle mappingStyle;
959+
if (useFlowStyle) {
960+
mappingStyle = MappingStyle.Flow;
961+
} else if (useBlockStyle) {
962+
mappingStyle = MappingStyle.Block;
963+
} else {
964+
// Use the original mapping style from metadata, or default to Block
965+
mappingStyle = metadata.DocumentMappingStyle ?? MappingStyle.Block;
966+
}
967+
968+
SerializePSObject(obj, metadata, emitter, mappingStyle, emitTags, 0, maxDepth);
958969

959970
emitter.Emit(new DocumentEnd(true));
960971
emitter.Emit(new StreamEnd());

0 commit comments

Comments
 (0)