Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add DynamicExpression.RegisterConverter #47

Merged
merged 1 commit into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/NeinLinq/DynamicExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,21 @@ public static Expression CreateMemberAccess(Expression target,
return selector.Split('.').Aggregate(target, Expression.PropertyOrField);
}

/// <summary>
/// Registers a custom converter for any type.
/// </summary>
/// <param name="type">Tye type to convert.</param>
/// <param name="converter">The culture-specific converter.</param>
public static void RegisterConverter(Type type, Func<string, IFormatProvider?, object> converter)
{
if (type is null)
throw new ArgumentNullException(nameof(type));
if (converter is null)
throw new ArgumentNullException(nameof(converter));

Cache.Add(type, converter);
}

private static Expression CreateConstant(ParameterExpression target,
Expression selector,
string? value,
Expand Down
14 changes: 14 additions & 0 deletions src/NeinLinq/ObjectCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,20 @@ private readonly Dictionary<TKey, TValue> cache
private readonly ReaderWriterLockSlim cacheLock
= new();

public void Add(TKey key, TValue value)
{
cacheLock.EnterWriteLock();

try
{
cache.Add(key, value);
}
finally
{
cacheLock.ExitWriteLock();
}
}

public TValue GetOrAdd(TKey key, Func<TKey, TValue> valueFactory)
{
cacheLock.EnterReadLock();
Expand Down
43 changes: 43 additions & 0 deletions test/NeinLinq.Tests/DynamicExpressionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,50 @@ public void CreateMemberAccess_NullArgument_Throws()
Assert.Equal("selector", selectorError.ParamName);
}

[Fact]
public void RegisterConverter_NullArgument_Throws()
{
var type = typeof(CustomType);

Func<string, IFormatProvider?, object> converter = (value, _) => CustomType.Create(value);

var typeError = Assert.Throws<ArgumentNullException>(()
=> DynamicExpression.RegisterConverter(null!, converter));
var converterError = Assert.Throws<ArgumentNullException>(()
=> DynamicExpression.RegisterConverter(type, null!));

Assert.Equal("type", typeError.ParamName);
Assert.Equal("converter", converterError.ParamName);
}

[Fact]
public void RegisterConverter_ValidArguments_Works()
{
DynamicExpression.RegisterConverter(typeof(CustomType), (value, _) => CustomType.Create(value));

var predicate = DynamicQuery.CreatePredicate<Model>("Value", DynamicCompare.Equal, "narf");

var actual = Assert.IsType<CustomType>(
Assert.IsAssignableFrom<ConstantExpression>(
Assert.IsAssignableFrom<BinaryExpression>(
predicate.Body)
.Right)
.Value)
.Value;

Assert.Equal("narf", actual);
}

private class CustomType
{
public string Value { get; private set; } = "";

public static CustomType Create(string value)
=> new() { Value = value };
}

private class Model
{
public CustomType? Value { get; set; }
}
}
Loading