Skip to content

Commit

Permalink
Use serializer in the PublishedPortConverter (#143)
Browse files Browse the repository at this point in the history
  • Loading branch information
trejjam authored Dec 10, 2024
1 parent feedbbf commit 7f00231
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 14 deletions.
9 changes: 9 additions & 0 deletions src/DockerComposeBuilder.Examples.Complex/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 =>
{
Expand Down
5 changes: 2 additions & 3 deletions src/DockerComposeBuilder/Converters/PublishedPortConverter.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using DockerComposeBuilder.Model.Services;
using System;
using YamlDotNet.Core;
using YamlDotNet.Core.Events;
using YamlDotNet.Serialization;

namespace DockerComposeBuilder.Converters;
Expand All @@ -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));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ public class ForceQuotedStringValuesEventEmitter : ChainedEventEmitter
{
private readonly Stack<EmitterState> _state = new();

public ForceQuotedStringValuesEventEmitter(IEventEmitter nextEmitter)
: base(nextEmitter)
public ForceQuotedStringValuesEventEmitter(
IEventEmitter nextEmitter
) : base(nextEmitter)
{
_state.Push(new EmitterState(EmitterState.EventType.Root));
}
Expand Down Expand Up @@ -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++;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@ public sealed class YamlIEnumerableSkipEmptyObjectGraphVisitor(
IObjectGraphVisitor<IEmitter> 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;

Expand Down

0 comments on commit 7f00231

Please sign in to comment.