Skip to content

Commit d435914

Browse files
OdonnoEricSites
authored andcommitted
feat: directly use MemberInfo for naming convention (dahomey-technologies#140)
1 parent 2d89bb3 commit d435914

File tree

11 files changed

+69
-43
lines changed

11 files changed

+69
-43
lines changed

src/Dahomey.Cbor.Tests/Issues/Issue0021.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Reflection;
23
using Dahomey.Cbor.Serialization;
34
using Dahomey.Cbor.Serialization.Conventions;
45
using Dahomey.Cbor.Serialization.Converters.Mappings;
@@ -13,9 +14,9 @@ public class Issue0021
1314
{
1415
public class LowerCaseNamingConvention : INamingConvention
1516
{
16-
public string GetPropertyName(string name)
17+
public string GetPropertyName(MemberInfo memberInfo)
1718
{
18-
return name.ToLowerInvariant();
19+
return memberInfo.Name.ToLowerInvariant();
1920
}
2021
}
2122

src/Dahomey.Cbor.Tests/NamingConventionTests.cs

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,30 @@
1-
using Dahomey.Cbor.Serialization.Conventions;
1+
using System.Linq;
2+
using Dahomey.Cbor.Serialization.Conventions;
23
using Xunit;
34

45
namespace Dahomey.Cbor.Tests;
56

67
public class NamingConventionTests
78
{
9+
private class TestObject
10+
{
11+
public string FooBar { get; set; }
12+
public string fooBar { get; set; }
13+
public string FooBAR { get; set; }
14+
public string FooBARFoo { get; set; }
15+
public string Foo { get; set; }
16+
public string foo { get; set; }
17+
}
18+
819
[Theory]
920
[InlineData("FooBar", "fooBar")]
1021
[InlineData("fooBar", "fooBar")]
1122
[InlineData("Foo", "foo")]
1223
[InlineData("foo", "foo")]
13-
[InlineData("", "")]
1424
public void CamelCase(string srcName, string expectedName)
1525
{
16-
string actualName = new CamelCaseNamingConvention().GetPropertyName(srcName);
26+
var memberInfo = typeof(TestObject).GetMember(srcName).Single();
27+
string actualName = new CamelCaseNamingConvention().GetPropertyName(memberInfo);
1728
Assert.Equal(expectedName, actualName);
1829
}
1930

@@ -24,10 +35,10 @@ public void CamelCase(string srcName, string expectedName)
2435
[InlineData("FooBARFoo", "foo_bar_foo")]
2536
[InlineData("Foo", "foo")]
2637
[InlineData("foo", "foo")]
27-
[InlineData("", "")]
2838
public void SnakeCase(string srcName, string expectedName)
2939
{
30-
string actualName = new SnakeCaseNamingConvention().GetPropertyName(srcName);
40+
var memberInfo = typeof(TestObject).GetMember(srcName).Single();
41+
string actualName = new SnakeCaseNamingConvention().GetPropertyName(memberInfo);
3142
Assert.Equal(expectedName, actualName);
3243
}
3344

@@ -38,10 +49,10 @@ public void SnakeCase(string srcName, string expectedName)
3849
[InlineData("FooBARFoo", "FOO_BAR_FOO")]
3950
[InlineData("Foo", "FOO")]
4051
[InlineData("foo", "FOO")]
41-
[InlineData("", "")]
4252
public void UpperSnakeCase(string srcName, string expectedName)
4353
{
44-
string actualName = new UpperSnakeCaseNamingConvention().GetPropertyName(srcName);
54+
var memberInfo = typeof(TestObject).GetMember(srcName).Single();
55+
string actualName = new UpperSnakeCaseNamingConvention().GetPropertyName(memberInfo);
4556
Assert.Equal(expectedName, actualName);
4657
}
4758

@@ -52,10 +63,10 @@ public void UpperSnakeCase(string srcName, string expectedName)
5263
[InlineData("FooBARFoo", "foo-bar-foo")]
5364
[InlineData("Foo", "foo")]
5465
[InlineData("foo", "foo")]
55-
[InlineData("", "")]
5666
public void KebabCase(string srcName, string expectedName)
5767
{
58-
string actualName = new KebabCaseNamingConvention().GetPropertyName(srcName);
68+
var memberInfo = typeof(TestObject).GetMember(srcName).Single();
69+
string actualName = new KebabCaseNamingConvention().GetPropertyName(memberInfo);
5970
Assert.Equal(expectedName, actualName);
6071
}
6172

@@ -66,10 +77,10 @@ public void KebabCase(string srcName, string expectedName)
6677
[InlineData("FooBARFoo", "FOO-BAR-FOO")]
6778
[InlineData("Foo", "FOO")]
6879
[InlineData("foo", "FOO")]
69-
[InlineData("", "")]
7080
public void UpperKebabCase(string srcName, string expectedName)
7181
{
72-
string actualName = new UpperKebabCaseNamingConvention().GetPropertyName(srcName);
82+
var memberInfo = typeof(TestObject).GetMember(srcName).Single();
83+
string actualName = new UpperKebabCaseNamingConvention().GetPropertyName(memberInfo);
7384
Assert.Equal(expectedName, actualName);
7485
}
7586

@@ -80,10 +91,10 @@ public void UpperKebabCase(string srcName, string expectedName)
8091
[InlineData("FooBARFoo", "foobarfoo")]
8192
[InlineData("Foo", "foo")]
8293
[InlineData("foo", "foo")]
83-
[InlineData("", "")]
8494
public void LowerCase(string srcName, string expectedName)
8595
{
86-
string actualName = new LowerCaseNamingConvention().GetPropertyName(srcName);
96+
var memberInfo = typeof(TestObject).GetMember(srcName).Single();
97+
string actualName = new LowerCaseNamingConvention().GetPropertyName(memberInfo);
8798
Assert.Equal(expectedName, actualName);
8899
}
89100

@@ -94,10 +105,10 @@ public void LowerCase(string srcName, string expectedName)
94105
[InlineData("FooBARFoo", "FOOBARFOO")]
95106
[InlineData("Foo", "FOO")]
96107
[InlineData("foo", "FOO")]
97-
[InlineData("", "")]
98108
public void UpperCase(string srcName, string expectedName)
99109
{
100-
string actualName = new UpperCaseNamingConvention().GetPropertyName(srcName);
110+
var memberInfo = typeof(TestObject).GetMember(srcName).Single();
111+
string actualName = new UpperCaseNamingConvention().GetPropertyName(memberInfo);
101112
Assert.Equal(expectedName, actualName);
102113
}
103114
}

src/Dahomey.Cbor/Serialization/Conventions/CamelCaseNamingConvention.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
using System;
2-
using System.Text;
1+
using System.Reflection;
32

43
namespace Dahomey.Cbor.Serialization.Conventions
54
{
65
public class CamelCaseNamingConvention : INamingConvention
76
{
8-
public string GetPropertyName(string reflectionName)
7+
public string GetPropertyName(MemberInfo member)
98
{
9+
string reflectionName = member.Name;
1010
if (string.IsNullOrEmpty(reflectionName) || char.IsLower(reflectionName[0]))
1111
{
1212
return reflectionName;
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
namespace Dahomey.Cbor.Serialization.Conventions
1+
using System.Reflection;
2+
3+
namespace Dahomey.Cbor.Serialization.Conventions
24
{
35
public interface INamingConvention
46
{
5-
string GetPropertyName(string name);
7+
string GetPropertyName(MemberInfo member);
68
}
79
}
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
namespace Dahomey.Cbor.Serialization.Conventions;
1+
using System.Reflection;
2+
3+
namespace Dahomey.Cbor.Serialization.Conventions;
24

35
public class KebabCaseNamingConvention : INamingConvention
46
{
5-
public string GetPropertyName(string name)
7+
public string GetPropertyName(MemberInfo member)
68
{
7-
return NamingConventionExtensions.GetPropertyName(name, (byte)'-');
9+
return NamingConventionExtensions.GetPropertyName(member.Name, (byte)'-');
810
}
911
}
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace Dahomey.Cbor.Serialization.Conventions
1+
using System.Reflection;
2+
3+
namespace Dahomey.Cbor.Serialization.Conventions
24
{
35
/// <summary>
46
/// Convert all names to lower case when serializing
@@ -9,11 +11,11 @@ public sealed class LowerCaseNamingConvention : INamingConvention
911
/// <summary>
1012
/// Get property name according convention
1113
/// </summary>
12-
/// <param name="name"> Property raw-name</param>
14+
/// <param name="member">Property member info</param>
1315
/// <returns>Property name according convention</returns>
14-
public string GetPropertyName(string name)
16+
public string GetPropertyName(MemberInfo member)
1517
{
16-
return name.ToLower();
18+
return member.Name.ToLower();
1719
}
1820
}
1921
}
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
namespace Dahomey.Cbor.Serialization.Conventions;
1+
using System.Reflection;
2+
3+
namespace Dahomey.Cbor.Serialization.Conventions;
24

35
public class SnakeCaseNamingConvention : INamingConvention
46
{
5-
public string GetPropertyName(string name)
7+
public string GetPropertyName(MemberInfo member)
68
{
7-
return NamingConventionExtensions.GetPropertyName(name, (byte)'_');
9+
return NamingConventionExtensions.GetPropertyName(member.Name, (byte)'_');
810
}
911
}
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace Dahomey.Cbor.Serialization.Conventions
1+
using System.Reflection;
2+
3+
namespace Dahomey.Cbor.Serialization.Conventions
24
{
35
/// <summary>
46
/// Convert all names to upper case when serializing
@@ -9,11 +11,11 @@ public sealed class UpperCaseNamingConvention : INamingConvention
911
/// <summary>
1012
/// Get property name according convention
1113
/// </summary>
12-
/// <param name="name"> Property raw-name</param>
14+
/// <param name="member">Property member info</param>
1315
/// <returns>Property name according convention</returns>
14-
public string GetPropertyName(string name)
16+
public string GetPropertyName(MemberInfo member)
1517
{
16-
return name.ToUpper();
18+
return member.Name.ToUpper();
1719
}
1820
}
1921
}
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
namespace Dahomey.Cbor.Serialization.Conventions;
1+
using System.Reflection;
2+
3+
namespace Dahomey.Cbor.Serialization.Conventions;
24

35
public class UpperKebabCaseNamingConvention : INamingConvention
46
{
5-
public string GetPropertyName(string name)
7+
public string GetPropertyName(MemberInfo member)
68
{
7-
return NamingConventionExtensions.GetPropertyName(name, (byte)'-', true);
9+
return NamingConventionExtensions.GetPropertyName(member.Name, (byte)'-', true);
810
}
911
}
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
namespace Dahomey.Cbor.Serialization.Conventions;
1+
using System.Reflection;
2+
3+
namespace Dahomey.Cbor.Serialization.Conventions;
24

35
public class UpperSnakeCaseNamingConvention : INamingConvention
46
{
5-
public string GetPropertyName(string name)
7+
public string GetPropertyName(MemberInfo member)
68
{
7-
return NamingConventionExtensions.GetPropertyName(name, (byte)'_', true);
9+
return NamingConventionExtensions.GetPropertyName(member.Name, (byte)'_', true);
810
}
911
}

0 commit comments

Comments
 (0)