Skip to content

Commit af81da8

Browse files
committed
Merge branch 'release/v1.0.3'
2 parents 73268c0 + e77faa3 commit af81da8

16 files changed

Lines changed: 281 additions & 70 deletions

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# LogSplit
2-
[![Build Status](https://travis-ci.org/WickedFlame/LogSplit.svg?branch=master)](https://travis-ci.org/WickedFlame/LogSplit)
3-
[![Build status](https://ci.appveyor.com/api/projects/status/5xsg81nvy8xwval0?svg=true)](https://ci.appveyor.com/project/chriswalpen/logsplit)
2+
[![Build status](https://img.shields.io/appveyor/build/chriswalpen/LogSplit/master?label=Master&logo=appveyor&style=for-the-badge)](https://ci.appveyor.com/project/chriswalpen/LogSplit/branch/master)
3+
[![Build status](https://img.shields.io/appveyor/build/chriswalpen/LogSplit/dev?label=Dev&logo=appveyor&style=for-the-badge)](https://ci.appveyor.com/project/chriswalpen/LogSplit/branch/dev)
4+
[![NuGet Version](https://img.shields.io/nuget/v/LogSplit.svg?style=for-the-badge&label=Latest)](https://www.nuget.org/packages/LogSplit/)
5+
[![NuGet Version](https://img.shields.io/nuget/vpre/LogSplit.svg?style=for-the-badge&label=RC)](https://www.nuget.org/packages/LogSplit/)
46

57
Split string lines into objects
68

appveyor.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33

44
# version format
5-
version: 1.0.2.{build}
5+
version: 1.0.3.{build}
66

77
environment:
8-
base_version: 1.0.2
8+
base_version: 1.0.3
99

1010
# you can use {branch} name in version format too
1111
# version: 1.0.{build}-{branch}
@@ -24,6 +24,7 @@ branches:
2424
# whitelist
2525
only:
2626
- master
27+
- dev
2728
# - somebranch
2829

2930
# Do not build on tags (GitHub only)

src/LogSplit/LogSplit.csproj

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,30 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

3-
<PropertyGroup>
4-
<TargetFrameworks>netstandard2.0;net48</TargetFrameworks>
5-
<TargetFrameworks Condition="'$(OS)' != 'Windows_NT'">netcoreapp2.0</TargetFrameworks>
6-
<Authors>Christian Walpen</Authors>
7-
<Company>WickedFlame</Company>
8-
<Title>LogSplit</Title>
9-
<Version>1.0.0</Version>
10-
<AssemblyVersion>1.0.0.0</AssemblyVersion>
11-
<FileVersion>1.0.0.0</FileVersion>
12-
<PackageVersion>1.0.0-RC01</PackageVersion>
13-
<NeutralLanguage>en</NeutralLanguage>
14-
<Description>Parse strings into objects</Description>
15-
<Copyright>@WickedFlame 2020</Copyright>
16-
<PackageTags>Parser, Logging, String</PackageTags>
17-
<PackageLicenseExpression>MIT</PackageLicenseExpression>
18-
<RepositoryUrl>https://github.com/WickedFlame/LogSplit</RepositoryUrl>
19-
<PackageProjectUrl>https://github.com/WickedFlame/LogSplit</PackageProjectUrl>
20-
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
3+
<PropertyGroup>
4+
<TargetFrameworks>netstandard2.0;net48</TargetFrameworks>
5+
<TargetFrameworks Condition="'$(OS)' != 'Windows_NT'">netcoreapp2.0</TargetFrameworks>
6+
<Authors>Christian Walpen</Authors>
7+
<Company>WickedFlame</Company>
8+
<Title>LogSplit</Title>
9+
<Version>1.0.0</Version>
10+
<AssemblyVersion>1.0.0.0</AssemblyVersion>
11+
<FileVersion>1.0.0.0</FileVersion>
12+
<PackageVersion>1.0.0-RC01</PackageVersion>
13+
<NeutralLanguage>en</NeutralLanguage>
14+
<Description>Parse strings into objects</Description>
15+
<Copyright>@WickedFlame 2020</Copyright>
16+
<PackageTags>Parser, Logging, String</PackageTags>
17+
<PackageLicenseExpression>MIT</PackageLicenseExpression>
18+
<RepositoryUrl>https://github.com/WickedFlame/LogSplit</RepositoryUrl>
19+
<PackageProjectUrl>https://github.com/WickedFlame/LogSplit</PackageProjectUrl>
20+
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
2121

22-
<!--<IncludeBuildOutput>false</IncludeBuildOutput>-->
23-
<!--<ContentTargetFolders>content\any\any\App_Packages\LogSplit;contentFiles\cs\netstandard2.0\App_Packages\LogSplit</ContentTargetFolders>-->
24-
</PropertyGroup>
22+
<!--<IncludeBuildOutput>false</IncludeBuildOutput>-->
23+
<!--<ContentTargetFolders>content\any\any\App_Packages\LogSplit;contentFiles\cs\netstandard2.0\App_Packages\LogSplit</ContentTargetFolders>-->
24+
</PropertyGroup>
2525

26-
<!--<ItemGroup>
27-
<Compile Update="Parser.cs" pack="true" buildAction="compile" />
28-
<None Include="Parser.cs.pp" pack="true" buildAction="compile"/>
29-
</ItemGroup>-->
26+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
27+
<DocumentationFile>bin\Release\netstandard2.0\LogSplit.xml</DocumentationFile>
28+
</PropertyGroup>
3029

3130
</Project>

src/LogSplit/Map/ConvertedValue.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System;
2+
3+
namespace LogSplit.Map
4+
{
5+
/// <summary>
6+
/// A value that is converted to the concrete type
7+
/// </summary>
8+
public class ConvertedValue
9+
{
10+
/// <summary>
11+
/// Creates a default value that represents a value that could not be converted
12+
/// </summary>
13+
public ConvertedValue()
14+
{
15+
Converted = false;
16+
}
17+
18+
/// <summary>
19+
/// Creates a oject containing a converted value and the given type
20+
/// </summary>
21+
/// <param name="value"></param>
22+
/// <param name="type"></param>
23+
public ConvertedValue(object value, Type type)
24+
{
25+
Value = value;
26+
Type = type;
27+
Converted = true;
28+
}
29+
30+
/// <summary>
31+
/// Gets if the value could be converted
32+
/// </summary>
33+
public bool Converted { get; }
34+
35+
/// <summary>
36+
/// Gets the converted value
37+
/// </summary>
38+
public object Value { get; }
39+
40+
/// <summary>
41+
/// Gets the type that the object was converted to
42+
/// </summary>
43+
public Type Type { get; }
44+
}
45+
}

src/LogSplit/Map/Mapper.cs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Linq;
1+
using System.Collections.Generic;
2+
using System.Linq;
23

34
namespace LogSplit.Map
45
{
@@ -10,6 +11,8 @@ public static T Map<T>(ParserResult result)
1011
{
1112
var instance = typeof(T).CreateInstance();
1213

14+
var unconverted = new List<string>();
15+
1316
foreach (var item in result)
1417
{
1518
var properties = typeof(T).GetProperties();
@@ -19,13 +22,23 @@ public static T Map<T>(ParserResult result)
1922
continue;
2023
}
2124

22-
var converted = PrimitiveTypeConverter.Convert(propertyInfo.PropertyType, item.Value.ToString());
23-
if (converted == null)
25+
var value = unconverted.Any() ? $"{string.Join(string.Empty, unconverted.Select(v => v))}{item.Raw}" : item.Value;
26+
var converted = PrimitiveTypeConverter.Convert(propertyInfo.PropertyType, value);
27+
if (!converted.Converted || converted.Value == null)
2428
{
25-
continue;
29+
//try without unconverted values
30+
converted = PrimitiveTypeConverter.Convert(propertyInfo.PropertyType, item.Value);
31+
if (!converted.Converted || converted.Value == null)
32+
{
33+
unconverted.Add(item.Raw);
34+
continue;
35+
}
36+
37+
// conversion was successful so delete the list with unconverted
38+
unconverted.Clear();
2639
}
2740

28-
propertyInfo.SetValue(instance, converted, null);
41+
propertyInfo.SetValue(instance, converted.Value, null);
2942
}
3043

3144
return (T)instance;

src/LogSplit/Map/PrimitiveTypeConverter.cs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,63 +5,63 @@ namespace LogSplit.Map
55
{
66
public class PrimitiveTypeConverter
77
{
8-
public static object Convert(Type type, string value)
8+
public static ConvertedValue Convert(Type type, string value)
99
{
1010
if (type == typeof(string))
1111
{
12-
return value.Trim();
12+
return new ConvertedValue(value.Trim(), type);
1313
}
1414

1515
if (type == typeof(bool) || type == typeof(bool?))
1616
{
1717
if (value != null)
1818
{
19-
return ParseBoolean(value);
19+
return new ConvertedValue(ParseBoolean(value), type);
2020
}
2121

22-
return null;
22+
return new ConvertedValue();
2323
}
2424

2525
if (type == typeof(long) || type == typeof(long?))
2626
{
2727
if (long.TryParse(value, out var l))
2828
{
29-
return l;
29+
return new ConvertedValue(l, type);
3030
}
3131

32-
return null;
32+
return new ConvertedValue();
3333
}
3434

3535
if (type == typeof(int) || type == typeof(int?))
3636
{
3737
if (int.TryParse(value, out var i))
3838
{
39-
return i;
39+
return new ConvertedValue(i, type);
4040
}
4141

42-
return null;
42+
return new ConvertedValue();
4343
}
4444

4545
if (type == typeof(decimal))
4646
{
47-
return decimal.Parse(value);
47+
return new ConvertedValue(decimal.Parse(value), type);
4848
}
4949

5050
if (type == typeof(double) || type == typeof(double?))
5151
{
5252
if (double.TryParse(value, out var b))
5353
{
54-
return b;
54+
return new ConvertedValue(b, type);
5555
}
5656

57-
return null;
57+
return new ConvertedValue();
5858
}
5959

6060
if (type == typeof(DateTime) || type == typeof(DateTime?))
6161
{
6262
if (DateTime.TryParse(value, out var date))
6363
{
64-
return date;
64+
return new ConvertedValue(date, type);
6565
}
6666

6767
var formats = new string[]
@@ -74,45 +74,45 @@ public static object Convert(Type type, string value)
7474

7575
if (DateTime.TryParseExact(value, formats, CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal, out date))
7676
{
77-
return date;
77+
return new ConvertedValue(date, type);
7878
}
7979

80-
throw new FormatException($"Value {value} could not be parsed to {type.FullName}. DateTimes have to be in the ISO-8601 format eg. yyyy-MM-dd");
80+
return new ConvertedValue();
8181
}
8282

8383
if (type == typeof(Guid))
8484
{
8585
if (Guid.TryParse(value, out var guid))
8686
{
87-
return guid;
87+
return new ConvertedValue(guid, type);
8888
}
8989

9090
if (Guid.TryParseExact(value, "B", out guid))
9191
{
92-
return guid;
92+
return new ConvertedValue(guid, type);
9393
}
9494

95-
return null;
95+
return new ConvertedValue();
9696
}
9797

9898
if (type == typeof(Type))
9999
{
100-
return Type.GetType(value);
100+
return new ConvertedValue(Type.GetType(value), type);
101101
}
102102

103103
if (type.IsEnum)
104104
{
105105
try
106106
{
107-
return Enum.Parse(type, value, true);
107+
return new ConvertedValue(Enum.Parse(type, value, true), type);
108108
}
109109
catch (ArgumentException)
110110
{
111111
// returns null anyway so do nothing
112112
}
113113
}
114114

115-
return null;
115+
return new ConvertedValue();
116116
}
117117

118118
public static bool ParseBoolean(object value)

src/LogSplit/Map/TypeExtensions.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,17 @@
22

33
namespace LogSplit.Map
44
{
5+
/// <summary>
6+
/// Extensions for <see cref="Type"/>
7+
/// </summary>
58
public static class TypeExtensions
69
{
10+
/// <summary>
11+
/// Create a instance of the given type
12+
/// </summary>
13+
/// <param name="type"></param>
14+
/// <returns></returns>
15+
/// <exception cref="ArgumentException"></exception>
716
public static object CreateInstance(this Type type)
817
{
918
try

src/LogSplit/Parser.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,29 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Linq;
43

54
namespace LogSplit
65
{
6+
/// <summary>
7+
/// A parser that separates a string based on the pattern provided
8+
/// </summary>
79
public class Parser
810
{
911
private readonly string _pattern;
1012

13+
/// <summary>
14+
/// Creates a new parser that uses the pattern to split a string
15+
/// </summary>
16+
/// <param name="pattern"></param>
1117
public Parser(string pattern)
1218
{
1319
_pattern = pattern;
1420
}
1521

22+
/// <summary>
23+
/// Parse the string based on the pattern
24+
/// </summary>
25+
/// <param name="value"></param>
26+
/// <returns></returns>
1627
public ParserResult Parse(string value)
1728
{
1829
var splits = SplitPattern(_pattern);
@@ -46,13 +57,12 @@ public ParserResult Parse(string value)
4657

4758
if(nextSplit <= 0)
4859
{
49-
//errors.Add($"Could not split value due to invalid pattern\n - Value: \"{value}\"\n - Invalid delimeter: \"{scan.Separator}\".\n - Preceding pattern: \"{scan.Pattern.Pattern}\"");
5060
continue;
5161
}
5262

5363
}
5464

55-
items.Add(new Token(key, value.Substring(0, nextSplit - length).Trim()));
65+
items.Add(new Token(key, value.Substring(0, nextSplit == value.Length ? nextSplit : nextSplit - length), value.Substring(0, nextSplit)));
5666
}
5767

5868

0 commit comments

Comments
 (0)