Skip to content

Commit

Permalink
disimbiguate type names for anonymous accessors
Browse files Browse the repository at this point in the history
  • Loading branch information
danielcrenna committed Aug 20, 2019
1 parent 6a1bda0 commit 579d58b
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 20 deletions.
3 changes: 2 additions & 1 deletion build/build.props
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<Configurations>Debug;Release;Package</Configurations>
<Version>1.0.6</Version>
<Version>1.0.9</Version>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)'!='Debug'">
Expand All @@ -19,4 +19,5 @@
<Exec Command="dotnet nuget push &quot;$(TargetDir)..\$(PackageId).$(PackageVersion).nupkg&quot; -s https://www.nuget.org/api/v2/package" />
</Target>


</Project>
21 changes: 11 additions & 10 deletions src/TypeKitchen/ComputedExpressions.cs
Expand Up @@ -4,14 +4,17 @@
using System;
using System.Reflection;
using System.Text.RegularExpressions;
using Microsoft.Extensions.Primitives;

namespace TypeKitchen
{
public static class ComputedExpressions
{
internal static string ResolveExpression(object @this, string expression, bool quoted)
internal static string ResolveExpression(object @this, string expression, bool inline = false)
{
var accessor = ReadAccessor.Create(@this.GetType());
bool IsQuoted(Type type) => !inline && (type == typeof(string) || type == typeof(StringValues) || type == typeof(char));

var accessor = ReadAccessor.Create(@this.GetType(), out var members);
foreach (Match match in Regex.Matches(expression, @"{{([a-zA-Z\.,\""()\s]+)}}",
RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace | RegexOptions.Compiled))
{
Expand All @@ -21,23 +24,21 @@ internal static string ResolveExpression(object @this, string expression, bool q
{
var key = keys[0];

if (accessor.TryGetValue(@this, key, out var value))
if (members.TryGetValue(key, out var propertyMember) && accessor.TryGetValue(@this, key, out var value))
{
var replacer = value?.ToString();
expression = expression.Replace(match.Groups[0].Value, quoted ? $"\"{replacer}\"" : replacer);
expression = expression.Replace(match.Groups[0].Value, IsQuoted(propertyMember.Type) ? $"\"{value}\"" : $"{value}");
}
else if (key.Contains("("))
{
var members = AccessorMembers.Create(@this, AccessorMemberScope.Public,
AccessorMemberTypes.Methods);
foreach (var member in members)
var callMembers = AccessorMembers.Create(@this, AccessorMemberScope.Public, AccessorMemberTypes.Methods);
foreach (var member in callMembers)
if (key.StartsWith(member.Name) && member.MemberInfo is MethodInfo method)
{
var caller = CallAccessor.Create(method);
var result = caller.Call(@this, new object[0]); // FIXME: parse parameters
var replacer = result?.ToString();
var resultType = result.GetType();
expression = expression.Replace(match.Groups[0].Value,
quoted ? $"\"{replacer}\"" : replacer);
IsQuoted(resultType) ? $"\"{result}\"" : $"{result}");
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/TypeKitchen/ComputedPredicate.cs
Expand Up @@ -23,7 +23,7 @@ public static bool Compute(object @this, string expression)
var code = Pooling.StringBuilderPool.Scoped(sb =>
{
sb.Append(
$"public static bool Method() {{ return {ComputedExpressions.ResolveExpression(@this, expression, true)}; }}");
$"public static bool Method() {{ return {ComputedExpressions.ResolveExpression(@this, expression, false)}; }}");
});

//
Expand Down
3 changes: 1 addition & 2 deletions src/TypeKitchen/ComputedString.cs
Expand Up @@ -22,8 +22,7 @@ public static string Compute(object @this, string expression)
// Pass 1: Resolve any {{ Member }} against self.
var code = Pooling.StringBuilderPool.Scoped(sb =>
{
sb.Append(
$"public static string Method() {{ return \"{ComputedExpressions.ResolveExpression(@this, expression, false)}\"; }}");
sb.Append($"public static string Method() {{ return \"{ComputedExpressions.ResolveExpression(@this, expression, true)}\"; }}");
});

//
Expand Down
4 changes: 2 additions & 2 deletions src/TypeKitchen/ReadAccessor.cs
Expand Up @@ -263,8 +263,8 @@ private static AccessorMembersKey KeyForType(Type type, AccessorMemberTypes type
private static string CreateNameForType(Type type)
{
return type.IsAnonymous()
? $"ReadAccessor_Anonymous_{(type.Assembly.IsDynamic ? "DynamicAssembly" : type.Assembly.GetName().Name)}_{type.Name}_{type.MetadataToken}"
: $"ReadAccessor_{(type.Assembly.IsDynamic ? "DynamicAssembly" : type.Assembly.GetName().Name)}_{type.Name}_{type.MetadataToken}";
? $"ReadAccessor_Anonymous_{(type.Assembly.IsDynamic ? $"Dynamic_{type.Name}_{type.MetadataToken}" : type.Assembly.GetName().Name)}_{type.Name}_{type.MetadataToken}"
: $"ReadAccessor_{(type.Assembly.IsDynamic ? $"Dynamic_{type.Name}_{type.MetadataToken}" : type.Assembly.GetName().Name)}_{type.Name}_{type.MetadataToken}";
}

private static AccessorMembers CreateReadAccessorMembers(Type type,
Expand Down
6 changes: 3 additions & 3 deletions src/TypeKitchen/TypeKitchen.csproj
Expand Up @@ -17,15 +17,15 @@
<PackageReference Include="System.Reflection.Emit" Version="4.3.0" />
<PackageReference Include="System.Reflection.Emit.Lightweight" Version="4.3.0" />
<PackageReference Include="Microsoft.CSharp" Version="4.5.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.2.1" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Scripting" Version="3.2.1" />
<PackageReference Include="Microsoft.CodeAnalysis.Features" Version="3.2.1" />
<PackageReference Include="Microsoft.Extensions.ObjectPool" Version="2.2.0" />
<PackageReference Include="System.Runtime.Loader" Version="4.3.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Scripting" Version="3.2.1" />

<!-- See: https://github.com/dotnet/roslyn-analyzers/issues/1771 -->
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="2.9.4" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="3.2.1" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.2.1" PrivateAssets="all"/>
<PackageReference Include="Microsoft.CodeAnalysis.Features" Version="3.2.1" PrivateAssets="all"/>
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion test/TypeKitchen.Benchmarks/TypeKitchen.Benchmarks.csproj
Expand Up @@ -8,7 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Autofac" Version="4.9.3" />
<PackageReference Include="Autofac" Version="4.9.4" />
<PackageReference Include="BenchmarkDotNet" Version="0.11.5" />
<PackageReference Include="DotLiquid" Version="2.0.314" />
<PackageReference Include="FastMember" Version="1.5.0" />
Expand Down

0 comments on commit 579d58b

Please sign in to comment.