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

Fix value types in anonymous arguments #61

Merged
merged 1 commit into from
Jul 14, 2023
Merged
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
39 changes: 34 additions & 5 deletions src/EdgeDB.Net.Driver/Utils/TypeArgumentUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,31 +30,60 @@ public TypeArgumentBuilder()
{
_type = typeof(T);

var param = Expression.Parameter(_type, "value");
if (RuntimeFeature.IsDynamicCodeCompiled)
_factory = CompileExpressionBuilder(_type);
else
_factory = GetReflectionBuilder(_type);
}

private static Func<T, IDictionary<string, object?>> GetReflectionBuilder(Type type)
{
var propMap = EdgeDBPropertyMapInfo.Create(type);

return value =>
{
var dict = new Dictionary<string, object?>();

foreach(var prop in propMap.Map)
{
dict.Add(prop.Key, prop.Value.PropertyInfo.GetValue(value));
}

return dict;
};
}

private static Func<T, IDictionary<string, object?>> CompileExpressionBuilder(Type type)
{
var param = Expression.Parameter(type, "value");
var dictExp = Expression.Variable(typeof(Dictionary<string, object?>), "dict");

var body = new List<Expression>()
{
Expression.Assign(dictExp, Expression.New(typeof(Dictionary<string, object?>)))
};

var propMap = EdgeDBPropertyMapInfo.Create(_type);
var propMap = EdgeDBPropertyMapInfo.Create(type);

foreach(var prop in propMap.Map)
foreach (var prop in propMap.Map)
{
Expression value = prop.Value.Type.IsValueType
? Expression.TypeAs(Expression.MakeMemberAccess(param, prop.Value.PropertyInfo), typeof(object))
: Expression.MakeMemberAccess(param, prop.Value.PropertyInfo);

body.Add(
Expression.Call(
dictExp,
_dictAddMethod,
Expression.Constant(prop.Key),
Expression.MakeMemberAccess(param, prop.Value.PropertyInfo)
value
)
);
}

body.Add(dictExp);

_factory = Expression.Lambda<Func<T, IDictionary<string, object?>>>(
return Expression.Lambda<Func<T, IDictionary<string, object?>>>(
Expression.Block(
typeof(IDictionary<string, object?>),
new ParameterExpression[] { dictExp },
Expand Down
Loading