Fix byte array encoding to yaml add a test to validate. (#485)
This commit is contained in:
@@ -18,6 +18,42 @@ namespace k8s
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public static class Yaml
|
public static class Yaml
|
||||||
{
|
{
|
||||||
|
public class ByteArrayStringYamlConverter : IYamlTypeConverter
|
||||||
|
{
|
||||||
|
public bool Accepts(Type type)
|
||||||
|
{
|
||||||
|
return type == typeof(System.Byte[]);
|
||||||
|
}
|
||||||
|
|
||||||
|
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 Encoding.UTF8.GetBytes(scalar.Value);
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
parser.MoveNext();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new InvalidOperationException(parser.Current?.ToString());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void WriteYaml(IEmitter emitter, object value, Type type)
|
||||||
|
{
|
||||||
|
var obj = (System.Byte[])value;
|
||||||
|
emitter.Emit(new YamlDotNet.Core.Events.Scalar(Encoding.UTF8.GetString(obj)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Load a collection of objects from a stream asynchronously
|
/// Load a collection of objects from a stream asynchronously
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -65,6 +101,7 @@ namespace k8s
|
|||||||
.WithNamingConvention(new CamelCaseNamingConvention())
|
.WithNamingConvention(new CamelCaseNamingConvention())
|
||||||
.WithTypeInspector(ti => new AutoRestTypeInspector(ti))
|
.WithTypeInspector(ti => new AutoRestTypeInspector(ti))
|
||||||
.WithTypeConverter(new IntOrStringYamlConverter())
|
.WithTypeConverter(new IntOrStringYamlConverter())
|
||||||
|
.WithTypeConverter(new ByteArrayStringYamlConverter())
|
||||||
.IgnoreUnmatchedProperties()
|
.IgnoreUnmatchedProperties()
|
||||||
.Build();
|
.Build();
|
||||||
var types = new List<Type>();
|
var types = new List<Type>();
|
||||||
@@ -81,6 +118,7 @@ namespace k8s
|
|||||||
.WithNamingConvention(new CamelCaseNamingConvention())
|
.WithNamingConvention(new CamelCaseNamingConvention())
|
||||||
.WithTypeInspector(ti => new AutoRestTypeInspector(ti))
|
.WithTypeInspector(ti => new AutoRestTypeInspector(ti))
|
||||||
.WithTypeConverter(new IntOrStringYamlConverter())
|
.WithTypeConverter(new IntOrStringYamlConverter())
|
||||||
|
.WithTypeConverter(new ByteArrayStringYamlConverter())
|
||||||
.Build();
|
.Build();
|
||||||
parser = new Parser(new StringReader(content));
|
parser = new Parser(new StringReader(content));
|
||||||
parser.Expect<StreamStart>();
|
parser.Expect<StreamStart>();
|
||||||
@@ -118,6 +156,7 @@ namespace k8s
|
|||||||
.WithNamingConvention(new CamelCaseNamingConvention())
|
.WithNamingConvention(new CamelCaseNamingConvention())
|
||||||
.WithTypeInspector(ti => new AutoRestTypeInspector(ti))
|
.WithTypeInspector(ti => new AutoRestTypeInspector(ti))
|
||||||
.WithTypeConverter(new IntOrStringYamlConverter())
|
.WithTypeConverter(new IntOrStringYamlConverter())
|
||||||
|
.WithTypeConverter(new ByteArrayStringYamlConverter())
|
||||||
.Build();
|
.Build();
|
||||||
var obj = deserializer.Deserialize<T>(content);
|
var obj = deserializer.Deserialize<T>(content);
|
||||||
return obj;
|
return obj;
|
||||||
@@ -135,6 +174,7 @@ namespace k8s
|
|||||||
.WithNamingConvention(new CamelCaseNamingConvention())
|
.WithNamingConvention(new CamelCaseNamingConvention())
|
||||||
.WithTypeInspector(ti => new AutoRestTypeInspector(ti))
|
.WithTypeInspector(ti => new AutoRestTypeInspector(ti))
|
||||||
.WithTypeConverter(new IntOrStringYamlConverter())
|
.WithTypeConverter(new IntOrStringYamlConverter())
|
||||||
|
.WithTypeConverter(new ByteArrayStringYamlConverter())
|
||||||
.WithEventEmitter(e => new StringQuotingEmitter(e))
|
.WithEventEmitter(e => new StringQuotingEmitter(e))
|
||||||
.BuildValueSerializer();
|
.BuildValueSerializer();
|
||||||
emitter.Emit(new StreamStart());
|
emitter.Emit(new StreamStart());
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ using System;
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using k8s;
|
using System.Text;
|
||||||
using k8s.Models;
|
using k8s.Models;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
@@ -321,5 +321,23 @@ spec:
|
|||||||
var objStr = Yaml.SaveToString(obj);
|
var objStr = Yaml.SaveToString(obj);
|
||||||
Assert.Equal(content, objStr);
|
Assert.Equal(content, objStr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void LoadSecret()
|
||||||
|
{
|
||||||
|
string kManifest = @"
|
||||||
|
apiVersion: v1
|
||||||
|
kind: Secret
|
||||||
|
metadata:
|
||||||
|
name: test-secret
|
||||||
|
data:
|
||||||
|
username: bXktYXBw
|
||||||
|
password: Mzk1MjgkdmRnN0pi
|
||||||
|
";
|
||||||
|
|
||||||
|
var result = Yaml.LoadFromString<V1Secret>(kManifest);
|
||||||
|
Assert.Equal(Encoding.UTF8.GetString(result.Data["username"]), "bXktYXBw");
|
||||||
|
Assert.Equal(Encoding.UTF8.GetString(result.Data["password"]), "Mzk1MjgkdmRnN0pi");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user