Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix YAML serialization for IntOrString values. #310

Merged
merged 1 commit into from
Sep 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/KubernetesClient/IntstrIntOrString.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,43 @@
using System;
using Newtonsoft.Json;
using YamlDotNet.Core;
using YamlDotNet.Core.Events;
using YamlDotNet.Core.Tokens;
using YamlDotNet.Serialization;

namespace k8s.Models
{
public class IntOrStringYamlConverter: IYamlTypeConverter
{
public bool Accepts(Type type)
{
return type == typeof(IntstrIntOrString);
}

public object ReadYaml(IParser parser, Type type)
{
if (parser.Current is YamlDotNet.Core.Events.Scalar scalar)
{
try {
if (string.IsNullOrEmpty(scalar.Value))
{
return null;
}
return new IntstrIntOrString(scalar.Value);
} finally {
parser.MoveNext();
}
}
throw new InvalidOperationException(parser.Current?.ToString());
}

public void WriteYaml(IEmitter emitter, object value, Type type)
{
var obj = (IntstrIntOrString) value;
emitter.Emit(new YamlDotNet.Core.Events.Scalar(obj.Value));
}
}

internal class IntOrStringConverter : JsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
Expand Down
4 changes: 4 additions & 0 deletions src/KubernetesClient/Yaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using YamlDotNet.Core.Events;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;
using k8s.Models;

namespace k8s
{
Expand All @@ -34,6 +35,7 @@ public static T LoadFromString<T>(string content) {
new DeserializerBuilder()
.WithNamingConvention(new CamelCaseNamingConvention())
.WithTypeInspector(ti => new AutoRestTypeInspector(ti))
.WithTypeConverter(new IntOrStringYamlConverter())
.Build();
var obj = deserializer.Deserialize<T>(content);
return obj;
Expand All @@ -47,8 +49,10 @@ public static string SaveToString<T>(T value)

var serializer =
new SerializerBuilder()
.DisableAliases()
.WithNamingConvention(new CamelCaseNamingConvention())
.WithTypeInspector(ti => new AutoRestTypeInspector(ti))
.WithTypeConverter(new IntOrStringYamlConverter())
.BuildValueSerializer();
emitter.Emit(new StreamStart());
emitter.Emit(new DocumentStart());
Expand Down
62 changes: 60 additions & 2 deletions tests/KubernetesClient.Tests/YamlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

namespace k8s.Tests
{
public class YamlTests {
public class YamlTests
{
[Fact]
public void LoadFromString()
{
Expand Down Expand Up @@ -184,7 +185,7 @@ private static IEnumerable<string> ToLines(string s)
{
using (var reader = new StringReader(s))
{
for (;;)
for (; ; )
{
var line = reader.ReadLine();
if (line == null)
Expand Down Expand Up @@ -236,5 +237,62 @@ public void CpuRequestAndLimitFromString()
Assert.Equal("cpu", cpuRequest.Key);
Assert.Equal("500m", cpuRequest.Value.ToString());
}

[Fact]
public void LoadIntOrString()
{
var content = @"apiVersion: v1
kind: Service
spec:
ports:
- port: 3000
targetPort: 3000
";

var obj = Yaml.LoadFromString<V1Service>(content);

Assert.Equal(3000, obj.Spec.Ports[0].Port);
Assert.Equal(3000, (int)obj.Spec.Ports[0].TargetPort);
}

[Fact]
public void SerializeIntOrString()
{
var content = @"apiVersion: v1
kind: Service
metadata:
labels:
app: test
name: test-svc
spec:
ports:
- port: 3000
targetPort: 3000";

Dictionary<string, string> labels = new Dictionary<string, string>
{
{"app", "test"}
};
var obj = new V1Service
{
Kind = "Service",
Metadata = new V1ObjectMeta(labels: labels, name: "test-svc"),
ApiVersion = "v1",
Spec = new V1ServiceSpec
{
Ports = new List<V1ServicePort>
{
new V1ServicePort
{
Port = 3000,
TargetPort = 3000
}
}
}
};

var output = Yaml.SaveToString<V1Service>(obj);
Assert.Equal(output, content);
}
}
}