Skip to content

Commit

Permalink
Merge branch 'release/v1.2.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
chriswalpen committed Dec 9, 2021
2 parents a1b3a78 + 0187ffb commit 5a29eff
Show file tree
Hide file tree
Showing 11 changed files with 201 additions and 68 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@

[![NuGet Version](https://img.shields.io/nuget/v/yamlmap.svg?style=for-the-badge&label=Latest)](https://www.nuget.org/packages/yamlmap/)
[![NuGet Version](https://img.shields.io/nuget/vpre/yamlmap.svg?style=for-the-badge&label=RC)](https://www.nuget.org/packages/yamlmap/)


[![Codacy Badge](https://app.codacy.com/project/badge/Grade/ab8916dc1225487a8a19923e6c96d7fe)](https://www.codacy.com/gh/WickedFlame/YamlMap/dashboard?utm_source=github.com&utm_medium=referral&utm_content=WickedFlame/YamlMap&utm_campaign=Badge_Grade)

[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=WickedFlame_Yaml&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=WickedFlame_Yaml)
[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=WickedFlame_Yaml&metric=coverage)](https://sonarcloud.io/summary/new_code?id=WickedFlame_Yaml)

A .NET Yaml Parser.
Map Yaml to .NET objects and vice versa.

Expand Down
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ dotnet_csproj:
patch: true
file: '**\*.csproj'
version: '{version}'
package_version: $(base_version)-RC0{build}
package_version: $(base_version)-RC{build}
assembly_version: $(base_version)
file_version: '{version}'
informational_version: '{version}'
Expand Down
36 changes: 36 additions & 0 deletions src/Tests/YamlMap.Tests/ParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -309,5 +309,41 @@ public void WickeFlame_Yaml_Parser_InvalidProperty()
Assert.That(result.Count == 1);
Assert.AreEqual("true", ((ValueToken)result[0]).Value);
}

[Test]
public void YamlMap_Parser_EmptyItem()
{
var lines = new[]
{
"First: one",
"",
"Comment: true"
};
var scanner = new Scanner(lines);
var parser = new Parser(scanner);

var result = parser.Parse();

Assert.That(result.Count == 2);
Assert.AreEqual("true", ((ValueToken)result[1]).Value);
}

[Test]
public void YamlMap_Parser_Comment()
{
var lines = new[]
{
"First: one",
"# invalid",
"Comment: true"
};
var scanner = new Scanner(lines);
var parser = new Parser(scanner);

var result = parser.Parse();

Assert.That(result.Count == 2);
Assert.AreEqual("true", ((ValueToken)result[1]).Value);
}
}
}
35 changes: 35 additions & 0 deletions src/Tests/YamlMap.Tests/Reader/YamlReaderCommentsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using Polaroider;

namespace YamlMap.Tests.Reader
{
public class YamlReaderCommentsTests
{
[Test]
public void YamlReader_Comments_InList()
{
var lines = new[]
{
"Items:",
"# - first",
" - second",
" # - third",
" - fourth",
"Id: test"
};

var reader = new YamlReader();
reader.Read<CommentsList>(lines).MatchSnapshot();
}

public class CommentsList
{
public List<string> Items { get; set; }

public string Id{ get; set; }
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---data
Id: test
Items:
second
fourth
18 changes: 9 additions & 9 deletions src/YamlMap/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,19 @@ public IToken Parse()

while (line != null)
{
while (token.Parent != null && line.Indentation <= token.Indentation)
var function = ParserFunctionFactory.GetFunction(line);
if (function != null)
{
if (token.Parent == null)
while (token.Parent != null && line.Indentation <= token.Indentation)
{
break;
}
if (token.Parent == null)
{
break;
}

token = token.Parent;
}
token = token.Parent;
}

var function = ParserFunctionFactory.GetFunction(line);
if(function != null)
{
token = function(token, line);
}

Expand Down
29 changes: 29 additions & 0 deletions src/YamlMap/Serialization/SerializerExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System.Linq;

namespace YamlMap.Serialization
{
internal static class SerializerExtensions
{
private static readonly char[] SpecialChars = new[] { ':', '[', ']' };

/// <summary>
/// Convert a object to a string that is used for serialization
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static string ToSerializeableString(this object value)
{
if (value == null)
{
return null;
}

if (value is string s && SpecialChars.Any(c => s.Contains(c)))
{
value = $"'{s}'";
}

return value.ToString();
}
}
}
21 changes: 19 additions & 2 deletions src/YamlMap/Serialization/TokenDeserializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,37 @@

namespace YamlMap.Serialization
{
/// <summary>
/// Deserializer
/// </summary>
public class TokenDeserializer : ITokenDeserializer
{
//private readonly PropertyMapper _mapper;
private readonly object _item;
private readonly IToken _token;
private readonly Type _type;

/// <summary>
/// Creates a new deserializer
/// </summary>
/// <param name="type"></param>
/// <param name="token"></param>
public TokenDeserializer(Type type, IToken token)
{
//_mapper = new PropertyMapper(type);
_item = type.CreateInstance(token);
_token = token;
_type = type;
}

/// <summary>
/// Gets the instance that is created
/// </summary>
public object Node => _item;

/// <summary>
/// Deserialize a <see cref="IToken"/>
/// </summary>
/// <param name="token"></param>
/// <exception cref="InvalidConfigurationException"></exception>
public void Deserialize(IToken token)
{
var mapper = MapperFactory.GetObjectMapper(Node, _type);
Expand Down Expand Up @@ -54,6 +68,9 @@ public void Deserialize(IToken token)
property.SetValue(Node, child.Node, null);
}

/// <summary>
/// Deserialize all child tokens
/// </summary>
public void DeserializeChildren()
{
// refactor the line to be parsed as property
Expand Down
31 changes: 9 additions & 22 deletions src/YamlMap/Serialization/TokenSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,17 @@

namespace YamlMap.Serialization
{
/// <summary>
/// Serializer that creates tokens of objects
/// </summary>
public class TokenSerializer
{


/// <summary>
/// Serialize the item to a string
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="item"></param>
/// <returns></returns>
public string Serialize<T>(T item)
{
var sb = new StringBuilder();
Expand Down Expand Up @@ -87,24 +94,4 @@ private void SerializeNode<T>(T item, StringBuilder sb, int indentation)
}
}
}

internal static class SerializerExtensions
{
private static char[] SpecialChars = new[] { ':', '[', ']' };

public static string ToSerializeableString(this object value)
{
if (value == null)
{
return null;
}

if (value is string s && SpecialChars.Any(c => s.Contains(c)))
{
value = $"'{s}'";
}

return value.ToString();
}
}
}
17 changes: 16 additions & 1 deletion src/YamlMap/Serialization/TypeConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,18 @@

namespace YamlMap.Serialization
{
public class TypeConverter
/// <summary>
/// Converter that converts a string value to a defined type
/// </summary>
public static class TypeConverter
{
/// <summary>
/// Convert the string to a given type
/// </summary>
/// <param name="type"></param>
/// <param name="value"></param>
/// <returns></returns>
/// <exception cref="FormatException"></exception>
public static object Convert(Type type, string value)
{
if (type == typeof(string))
Expand Down Expand Up @@ -115,6 +125,11 @@ public static object Convert(Type type, string value)
return null;
}

/// <summary>
/// Parser a string to a boolean
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static bool ParseBoolean(object value)
{
if (value == null || value == DBNull.Value)
Expand Down
68 changes: 36 additions & 32 deletions src/YamlMap/YamlMap.csproj
Original file line number Diff line number Diff line change
@@ -1,37 +1,41 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.1</TargetFrameworks>
<TargetFrameworks Condition="'$(OS)' != 'Windows_NT'">netcoreapp2.1</TargetFrameworks>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>../YamlMap.snk</AssemblyOriginatorKeyFile>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<Authors>Christian Walpen</Authors>
<Company>WickedFlame</Company>
<Description>
A .NET Yaml Parser.
Map Yaml to .NET objects and vice versa.
</Description>
<Copyright>@WickedFlame 2019</Copyright>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<PackageTags>YAML Parser</PackageTags>
<Title>YamlMap</Title>
<Summary>A Yaml Parser for .NET</Summary>
<PackageProjectUrl>http://wickedflame.github.io/</PackageProjectUrl>
<NeutralLanguage>en</NeutralLanguage>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>1.0.0.0</Version>
<AssemblyVersion>1.0.0</AssemblyVersion>
<FileVersion>1.0.0.0</FileVersion>
<PackageVersion>1.0.0</PackageVersion>
</PropertyGroup>
<PropertyGroup>
<TargetFrameworks>netstandard2.1</TargetFrameworks>
<TargetFrameworks Condition="'$(OS)' != 'Windows_NT'">netcoreapp2.1</TargetFrameworks>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>../YamlMap.snk</AssemblyOriginatorKeyFile>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<Authors>Christian Walpen</Authors>
<Company>WickedFlame</Company>
<Description>
A .NET Yaml Parser.
Map Yaml to .NET objects and vice versa.
</Description>
<Copyright>@WickedFlame 2019</Copyright>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<PackageTags>YAML Parser</PackageTags>
<Title>YamlMap</Title>
<Summary>A Yaml Parser for .NET</Summary>
<PackageProjectUrl>http://wickedflame.github.io/</PackageProjectUrl>
<NeutralLanguage>en</NeutralLanguage>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>1.0.0.0</Version>
<AssemblyVersion>1.0.0</AssemblyVersion>
<FileVersion>1.0.0.0</FileVersion>
<PackageVersion>1.0.0</PackageVersion>
</PropertyGroup>

<ItemGroup>
<None Include="..\..\LICENSE">
<Pack>True</Pack>
<PackagePath></PackagePath>
</None>
</ItemGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<DocumentationFile>bin\Release\netstandard2.0\YamlMap.xml</DocumentationFile>
</PropertyGroup>

<ItemGroup>
<None Include="..\..\LICENSE">
<Pack>True</Pack>
<PackagePath></PackagePath>
</None>
</ItemGroup>

</Project>

0 comments on commit 5a29eff

Please sign in to comment.