Skip to content

Commit

Permalink
Merge pull request #67 from seesharper/support-camelhumps-for-simple-…
Browse files Browse the repository at this point in the history
…properties

Added support for "camel humps" for simple properties
  • Loading branch information
seesharper committed Sep 12, 2023
2 parents d5dd958 + 1f5784b commit df0338e
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 9 deletions.
23 changes: 23 additions & 0 deletions src/DbReader.Tests/InstanceReaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,29 @@ public void ShouldHandleNullValuesInNavigationChain()
result.Children.ShouldBeEmpty();
}

[Fact]
public void Test()
{
DbReaderOptions.WhenReading<IEnumerable<ClassWithId>>().Use((dr, ordinal) =>
{
return Array.Empty<ClassWithId>();
});

var dataRecord = new { Id = 42, Children = "1,2,3" }.ToDataRecord();
var reader = GetReader<ClassWithEnumerablePropertySpecifiedInWhenReading>();
var instance = reader.Read(dataRecord, string.Empty);
}

public class ClassWithEnumerablePropertySpecifiedInWhenReading
{
public int Id { get; set; }

public IEnumerable<ClassWithId> Children { get; set; }
}




public enum EnumWithoutConverterFunctionForIntegralType : ushort
{
Value1 = 1,
Expand Down
13 changes: 10 additions & 3 deletions src/DbReader.Tests/IntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,16 @@ public void ShouldReadCustomers()
}
}

[Fact]
public void ShouldReadCustomerWithCamelHumpFieldName()
{
using (var connection = CreateConnection())
{
var customers = connection.Read<Customer>("SELECT CustomerID, CompanyName as CN FROM Customers WHERE CustomerID = 'ALFKI'");
customers.Single().CompanyName.ShouldBe("Alfreds Futterkiste");
}
}

[Fact]
public void ShouldReturnEmptyListWithoutNavigationProperties()
{
Expand Down Expand Up @@ -592,9 +602,6 @@ public async Task ShouldNotCacheCollection()
}
}




public class CustomerId
{
private string Value { get; set; }
Expand Down
10 changes: 10 additions & 0 deletions src/DbReader.Tests/PropertyMapperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ public void ShouldMapMatchingField()
result[0].ColumnInfo.Ordinal.ShouldBe(0);
}

[Fact]
public void ShouldMapMatchingFieldUsingUpperCaseLetters()
{
var dataRecord = new { I32P = 42 }.ToDataRecord();

var result = propertyMapper.Execute(typeof(SampleClass), dataRecord, string.Empty);

result[0].ColumnInfo.Ordinal.ShouldBe(0);
}

[Fact]
public void ShouldNotMapUnknownField()
{
Expand Down
17 changes: 11 additions & 6 deletions src/DbReader/Mapping/PropertyMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace DbReader.Mapping
using System.Data;
using System.Linq;
using System.Reflection;
using DbReader.Extensions;
using Selectors;

/// <summary>
Expand All @@ -14,7 +15,7 @@ namespace DbReader.Mapping
public class PropertyMapper : IPropertyMapper
{
private readonly IPropertySelector simplePropertySelector;
private readonly IFieldSelector fieldSelector;
private readonly IFieldSelector fieldSelector;

/// <summary>
/// Initializes a new instance of the <see cref="PropertyMapper"/> class.
Expand All @@ -40,7 +41,7 @@ public MappingInfo[] Execute(Type type, IDataRecord dataRecord, string prefix)
{
IReadOnlyDictionary<string, ColumnInfo> fieldOrdinals = fieldSelector.Execute(dataRecord);
var simpleProperties = simplePropertySelector.Execute(type);

return
simpleProperties.Select(
property => new MappingInfo(property, TryMapProperty(property, prefix, fieldOrdinals))).ToArray();
Expand All @@ -52,7 +53,7 @@ private static string GetFieldName(PropertyInfo property, string prefix)
{
return property.Name;
}

return prefix + "_" + property.Name;
}

Expand All @@ -62,7 +63,11 @@ private static ColumnInfo GetColumnInfo(PropertyInfo property, IReadOnlyDictiona
ColumnInfo columnInfo;
if (!fields.TryGetValue(fieldName, out columnInfo))
{
columnInfo = new ColumnInfo(-1, null, null);
fieldName = fieldName.GetUpperCaseLetters();
if (!fields.TryGetValue(fieldName, out columnInfo))
{
columnInfo = new ColumnInfo(-1, null, null);
}
}

return columnInfo;
Expand All @@ -73,8 +78,8 @@ private static ColumnInfo GetColumnInfo(PropertyInfo property, IReadOnlyDictiona
string prefix,
IReadOnlyDictionary<string, ColumnInfo> fields)
{
ColumnInfo columnInfo = GetColumnInfo(property, fields, prefix);
ColumnInfo columnInfo = GetColumnInfo(property, fields, prefix);
return columnInfo;
}
}
}
}

0 comments on commit df0338e

Please sign in to comment.