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(lines).MatchSnapshot(); + } + + public class CommentsList + { + public List 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[] { ':', '[', ']' }; + + /// + /// Convert a object to a string that is used for serialization + /// + /// + /// + 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 { + /// + /// Deserializer + /// public class TokenDeserializer : ITokenDeserializer { - //private readonly PropertyMapper _mapper; private readonly object _item; private readonly IToken _token; private readonly Type _type; + /// + /// Creates a new deserializer + /// + /// + /// public TokenDeserializer(Type type, IToken token) { - //_mapper = new PropertyMapper(type); _item = type.CreateInstance(token); _token = token; _type = type; } + /// + /// Gets the instance that is created + /// public object Node => _item; + /// + /// Deserialize a + /// + /// + /// 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); } + /// + /// Deserialize all child tokens + /// 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 { + /// + /// Serializer that creates tokens of objects + /// public class TokenSerializer { - - + /// + /// Serialize the item to a string + /// + /// + /// + /// public string Serialize(T item) { var sb = new StringBuilder(); @@ -87,24 +94,4 @@ private void SerializeNode(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 + /// + /// Converter that converts a string value to a defined type + /// + public static class TypeConverter { + /// + /// Convert the string to a given type + /// + /// + /// + /// + /// 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; } + /// + /// Parser a string to a boolean + /// + /// + /// 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 @@  - - netstandard2.1 - netcoreapp2.1 - true - ../YamlMap.snk - true - snupkg - Christian Walpen - WickedFlame - - A .NET Yaml Parser. - Map Yaml to .NET objects and vice versa. - - @WickedFlame 2019 - LICENSE - YAML Parser - YamlMap - A Yaml Parser for .NET - http://wickedflame.github.io/ - en - true - 1.0.0.0 - 1.0.0 - 1.0.0.0 - 1.0.0 - + + netstandard2.1 + netcoreapp2.1 + true + ../YamlMap.snk + true + snupkg + Christian Walpen + WickedFlame + + A .NET Yaml Parser. + Map Yaml to .NET objects and vice versa. + + @WickedFlame 2019 + LICENSE + YAML Parser + YamlMap + A Yaml Parser for .NET + http://wickedflame.github.io/ + en + true + 1.0.0.0 + 1.0.0 + 1.0.0.0 + 1.0.0 + - - - True - - - + + bin\Release\netstandard2.0\YamlMap.xml + + + + + True + + +