diff --git a/README.md b/README.md index 6a5d43a..96e4331 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/appveyor.yml b/appveyor.yml index df4ac10..6a350e2 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -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}' diff --git a/src/Tests/YamlMap.Tests/ParserTests.cs b/src/Tests/YamlMap.Tests/ParserTests.cs index 439a829..273aa96 100644 --- a/src/Tests/YamlMap.Tests/ParserTests.cs +++ b/src/Tests/YamlMap.Tests/ParserTests.cs @@ -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); + } } } diff --git a/src/Tests/YamlMap.Tests/Reader/YamlReaderCommentsTests.cs b/src/Tests/YamlMap.Tests/Reader/YamlReaderCommentsTests.cs new file mode 100644 index 0000000..1882ba4 --- /dev/null +++ b/src/Tests/YamlMap.Tests/Reader/YamlReaderCommentsTests.cs @@ -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; } + } + } +} diff --git a/src/Tests/YamlMap.Tests/Reader/_Snapshots/YamlReaderCommentsTests_YamlReader_Comments_InList.snapshot b/src/Tests/YamlMap.Tests/Reader/_Snapshots/YamlReaderCommentsTests_YamlReader_Comments_InList.snapshot new file mode 100644 index 0000000..fc2a817 --- /dev/null +++ b/src/Tests/YamlMap.Tests/Reader/_Snapshots/YamlReaderCommentsTests_YamlReader_Comments_InList.snapshot @@ -0,0 +1,5 @@ +---data +Id: test +Items: + second + fourth diff --git a/src/YamlMap/Parser.cs b/src/YamlMap/Parser.cs index b97f509..a6c5165 100644 --- a/src/YamlMap/Parser.cs +++ b/src/YamlMap/Parser.cs @@ -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); } diff --git a/src/YamlMap/Serialization/SerializerExtensions.cs b/src/YamlMap/Serialization/SerializerExtensions.cs new file mode 100644 index 0000000..c57a0fd --- /dev/null +++ b/src/YamlMap/Serialization/SerializerExtensions.cs @@ -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(); + } + } +} diff --git a/src/YamlMap/Serialization/TokenDeserializer.cs b/src/YamlMap/Serialization/TokenDeserializer.cs index 4db6dad..9b266e7 100644 --- a/src/YamlMap/Serialization/TokenDeserializer.cs +++ b/src/YamlMap/Serialization/TokenDeserializer.cs @@ -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); @@ -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 diff --git a/src/YamlMap/Serialization/TokenSerializer.cs b/src/YamlMap/Serialization/TokenSerializer.cs index 22355bb..24ec1ed 100644 --- a/src/YamlMap/Serialization/TokenSerializer.cs +++ b/src/YamlMap/Serialization/TokenSerializer.cs @@ -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(); @@ -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(); - } - } } diff --git a/src/YamlMap/Serialization/TypeConverter.cs b/src/YamlMap/Serialization/TypeConverter.cs index 6a95c8b..22fe4bb 100644 --- a/src/YamlMap/Serialization/TypeConverter.cs +++ b/src/YamlMap/Serialization/TypeConverter.cs @@ -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)) @@ -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) diff --git a/src/YamlMap/YamlMap.csproj b/src/YamlMap/YamlMap.csproj index 62b03da..bdf6840 100644 --- a/src/YamlMap/YamlMap.csproj +++ b/src/YamlMap/YamlMap.csproj @@ -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>