From 7f002312c9b66168e5febf521728d9b4c11b1216 Mon Sep 17 00:00:00 2001 From: Jan Trejbal Date: Tue, 10 Dec 2024 23:34:29 +0100 Subject: [PATCH] Use serializer in the PublishedPortConverter (#143) --- .../Program.cs | 9 +++++++++ .../Converters/PublishedPortConverter.cs | 5 ++--- .../ForceQuotedStringValuesEventEmitter.cs | 16 +++++++--------- ...YamlIEnumerableSkipEmptyObjectGraphVisitor.cs | 8 ++++++-- 4 files changed, 24 insertions(+), 14 deletions(-) diff --git a/src/DockerComposeBuilder.Examples.Complex/Program.cs b/src/DockerComposeBuilder.Examples.Complex/Program.cs index 08457d0..16c46f3 100644 --- a/src/DockerComposeBuilder.Examples.Complex/Program.cs +++ b/src/DockerComposeBuilder.Examples.Complex/Program.cs @@ -48,10 +48,19 @@ { Target = 80, Published = "8000", + Protocol = "tcp", }, new Port { Target = 81, Published = 8001, + Protocol = "udp", + }, new Port + { + Target = 82, + Published = 8002, + }, new Port + { + Target = 83, }) .WithEnvironment(mb => { diff --git a/src/DockerComposeBuilder/Converters/PublishedPortConverter.cs b/src/DockerComposeBuilder/Converters/PublishedPortConverter.cs index 09d377e..5df3a02 100644 --- a/src/DockerComposeBuilder/Converters/PublishedPortConverter.cs +++ b/src/DockerComposeBuilder/Converters/PublishedPortConverter.cs @@ -1,7 +1,6 @@ using DockerComposeBuilder.Model.Services; using System; using YamlDotNet.Core; -using YamlDotNet.Core.Events; using YamlDotNet.Serialization; namespace DockerComposeBuilder.Converters; @@ -18,11 +17,11 @@ public void WriteYaml(IEmitter emitter, object? value, Type type, ObjectSerializ { if (publishedPort is { PortAsInt: { } portAsInt }) { - emitter.Emit(new Scalar(portAsInt.ToString())); + serializer(portAsInt, typeof(int)); } else if (publishedPort is { PortAsString: { } portAsString }) { - emitter.Emit(new Scalar(AnchorName.Empty, TagName.Empty, portAsString, ScalarStyle.DoubleQuoted, true, false)); + serializer(portAsString, typeof(string)); } } } diff --git a/src/DockerComposeBuilder/Emitters/ForceQuotedStringValuesEventEmitter.cs b/src/DockerComposeBuilder/Emitters/ForceQuotedStringValuesEventEmitter.cs index 2b0356e..7ab9941 100644 --- a/src/DockerComposeBuilder/Emitters/ForceQuotedStringValuesEventEmitter.cs +++ b/src/DockerComposeBuilder/Emitters/ForceQuotedStringValuesEventEmitter.cs @@ -10,8 +10,9 @@ public class ForceQuotedStringValuesEventEmitter : ChainedEventEmitter { private readonly Stack _state = new(); - public ForceQuotedStringValuesEventEmitter(IEventEmitter nextEmitter) - : base(nextEmitter) + public ForceQuotedStringValuesEventEmitter( + IEventEmitter nextEmitter + ) : base(nextEmitter) { _state.Push(new EmitterState(EmitterState.EventType.Root)); } @@ -68,17 +69,14 @@ public override void Emit(SequenceEndEventInfo eventInfo, IEmitter emitter) base.Emit(eventInfo, emitter); } - private class EmitterState + private sealed class EmitterState( + EmitterState.EventType eventType + ) { - public EventType Type { get; } + public EventType Type { get; } = eventType; private int _currentIndex; - public EmitterState(EventType eventType) - { - Type = eventType; - } - public void Move() { _currentIndex++; diff --git a/src/DockerComposeBuilder/Emitters/YamlIEnumerableSkipEmptyObjectGraphVisitor.cs b/src/DockerComposeBuilder/Emitters/YamlIEnumerableSkipEmptyObjectGraphVisitor.cs index e350f40..8bdf919 100644 --- a/src/DockerComposeBuilder/Emitters/YamlIEnumerableSkipEmptyObjectGraphVisitor.cs +++ b/src/DockerComposeBuilder/Emitters/YamlIEnumerableSkipEmptyObjectGraphVisitor.cs @@ -10,8 +10,12 @@ public sealed class YamlIEnumerableSkipEmptyObjectGraphVisitor( IObjectGraphVisitor nextVisitor ) : ChainedObjectGraphVisitor(nextVisitor) { - public override bool EnterMapping(IPropertyDescriptor key, IObjectDescriptor value, IEmitter context, - ObjectSerializer serializer) + public override bool EnterMapping( + IPropertyDescriptor key, + IObjectDescriptor value, + IEmitter context, + ObjectSerializer serializer + ) { var retVal = false;