Skip to content

Commit

Permalink
Pass null to converter functions
Browse files Browse the repository at this point in the history
  • Loading branch information
seesharper committed May 31, 2023
1 parent 1ddab8f commit 6a22c0d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
"type": "process",
"args": [
"script",
"${workspaceFolder}/../build/build.csx",
"${workspaceFolder}/build/build.csx",
"testcoverage"
],
"problemMatcher": "$msCompile",
Expand Down
27 changes: 26 additions & 1 deletion src/DbReader.Tests/ArgumentParserMethodBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,23 @@ public void ShouldHandleEnumWithConverterFunction()
[Fact]
public void ShouldHandleNullableEnumWithoutConverterFunctionPassingNull()
{
var args = new { Status = new EnumParameterWithoutConverterFunction?() };
//var args = new { EnumParameterWithoutConverterFunctionStatus = (EnumParameterWithoutConverterFunction?)null };
var args = new Args() { Status = null };
var method = argumentParserMethodBuilder.CreateMethod("@Status", args.GetType(), Array.Empty<IDataParameter>());
var result = method("@Status", args, () => new TestDataParameter());
result.Parameters[0].Value.ShouldBe(DBNull.Value);
}

[Fact]
public void ShouldHandleNullableEnumWithConverterFunctionPassingNull()
{
DbReaderOptions.WhenPassing<EnumParameterWithConverterFunctionPassingNull?>().Use((parameter, value) => parameter.Value = (int)EnumParameterWithConverterFunctionPassingNull.Value3);
var args = new { Status = (EnumParameterWithConverterFunctionPassingNull?)null };
var method = argumentParserMethodBuilder.CreateMethod("@Status", args.GetType(), Array.Empty<IDataParameter>());
var result = method("@Status", args, () => new TestDataParameter());
result.Parameters[0].Value.ShouldBe(3);
}

[Fact]
public void ShouldHandleNullableEnumWithoutConverterFunction()
{
Expand Down Expand Up @@ -118,6 +129,15 @@ public class ThrowingDataParameter : IDataParameter
public DataRowVersion SourceVersion { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
public object Value { get => throw new NotImplementedException(); set => throw new ArgumentException(); }
}

public enum EnumParameterWithConverterFunctionPassingNull
{
Value1 = 1,
Value2 = 2,
Value3 = 3
}


public enum EnumParameterWithoutConverterFunction
{
Value1 = 1,
Expand All @@ -131,7 +151,12 @@ public enum EnumParameterWithConverterFunction
Value2 = 2,
Value3 = 3
}
public class Args
{
public EnumParameterWithoutConverterFunction? Status { get; set; }
}
}



}
4 changes: 2 additions & 2 deletions src/DbReader/ArgumentProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ static ArgumentProcessor()
ProcessDelegates.TryAdd(typeof(byte), (parameter, value) => AssignParameterValue(parameter, (byte)value));
ProcessDelegates.TryAdd(typeof(short), (parameter, value) => AssignParameterValue(parameter, (short)value));
ProcessDelegates.TryAdd(typeof(ushort), (parameter, value) => AssignParameterValue(parameter, (ushort)value));
ProcessDelegates.TryAdd(typeof(ushort), (parameter, value) => AssignParameterValue(parameter, (ushort)value));
ProcessDelegates.TryAdd(typeof(ushort), (parameter, value) => AssignParameterValue(parameter, (ushort)value));
}


Expand Down Expand Up @@ -75,7 +75,7 @@ public static void RegisterProcessDelegate<TArgument>(Action<IDataParameter, TAr

public static void Process(Type argumentType, IDataParameter dataParameter, object argument)
{
if (argument == null)
if (argument == null && !ProcessDelegates.ContainsKey(argumentType))
{
dataParameter.Value = DBNull.Value;
return;
Expand Down

0 comments on commit 6a22c0d

Please sign in to comment.