Skip to content

Commit

Permalink
♻️ more internalising
Browse files Browse the repository at this point in the history
  • Loading branch information
fluffynuts committed Apr 17, 2024
1 parent b88b9dd commit 84bc3ce
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ private static Type TryFindBuilderTypeInClassHeirachyFor(Type potentialBuilder,
{
var current = TryFindGenericBuilderInClassHeirachy(potentialBuilder);
if (current == null)
{
return null;
}

var typeParameters = current.GetGenericArguments();
return typeParameters.Length > 1 && typeParameters[1] == buildType
? current
Expand All @@ -43,14 +46,19 @@ internal static Type TryFindGenericBuilderInClassHeirachy(this Type current)
{
var genericBase = current.GetGenericTypeDefinition();
if (genericBase == GenericBuilderBaseType)
{
break;
}
}

current = current.BaseType();
}

if (current == ObjectType || current == null)
{
return null;
}

return current;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ public static class EnumerableExtensions
public static IEnumerable<T> Randomize<T>(this IEnumerable<T> input)
{
if (input == null)
{
return null;
}

var src = new List<T>(input);
var result = new List<T>();
while (src.Count > 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,17 @@ private static ModuleBuilder CreateOrReuseDynamicModule()
internal static Type ReuseOrGenerateDynamicBuilderFor(Type type)
{
if (type == null)
{
return null;
}

lock (DynamicBuilders)
{
if (DynamicBuilders.TryGetValue(type, out var dynamicBuilderType))
{
return dynamicBuilderType;
}

var t = typeof(GenericBuilder<,>);

var modBuilder = CreateOrReuseDynamicModule();
Expand Down Expand Up @@ -336,14 +342,18 @@ bool retrying

private static void AttemptToLoadAssemblyAlongside<T>(string fileName)
{
var codeBase = new Uri(typeof(T).Assembly.CodeBase).LocalPath;
var codeBase = new Uri(typeof(T).Assembly.Location).LocalPath;
if (!File.Exists(codeBase))
{
return;
}

var folder = Path.GetDirectoryName(codeBase);
var search = Path.Combine(folder ?? "", fileName);
if (!File.Exists(search))
{
return;
}

try
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,16 @@ public static Type GetBuilderFor(Type type)
public static Type TryFindExistingBuilderFor(Type type)
{
if (type == null)
{
return null;
}

lock (BuilderTypeCache)
{
if (BuilderTypeCache.TryGetValue(type, out var result))
{
return result;
}
}

return TryFindExistingBuilderAndCacheFor(type);
Expand Down Expand Up @@ -94,7 +99,10 @@ private static Type TryFindExistingBuilderAndCacheFor(Type type)
private static void CacheBuilderType(Type type, Type builderType)
{
if (type == null)
{
return;
}

lock (BuilderTypeCache)
{
BuilderTypeCache[type] = builderType;
Expand All @@ -107,7 +115,10 @@ private static void TryCacheBuilderType(Type builderType)
{
var genericBuilder = builderType.TryFindGenericBuilderInClassHeirachy();
if (genericBuilder.GenericTypeArguments.Length != 2)
{
return;
}

var builtType = genericBuilder.GenericTypeArguments[1]; // Naive, but let's run with it
CacheBuilderType(builderType, builtType);
}
Expand Down Expand Up @@ -160,7 +171,10 @@ internal static Type TryFindBuilderInAnyOtherAssemblyInAppDomainFor(Type propert
var types = allBuilders.Where(t => t.IsBuilderFor(propertyType))
.ToArray();
if (!types.Any())
{
return null;
}

return types.Length == 1
? types.First()
: FindClosestNamespaceMatchFor(propertyType, types);
Expand All @@ -183,7 +197,10 @@ private static void LoadImmediateAssembliesIfRequired()
lock (ReferenceLoadLock)
{
if (_haveLoadedImmediateAssemblies)
{
return;
}

_haveLoadedImmediateAssemblies = true;
AppDomain.CurrentDomain.GetAssemblies().ForEach(LoadReferencedAssemblies);
}
Expand Down Expand Up @@ -247,14 +264,20 @@ private static Type TryFindBuilderInCurrentAssemblyFor(Type propType)
private static Type FindClosestNamespaceMatchFor(Type propertyType, IEnumerable<Type> types)
{
if (propertyType?.Namespace == null) // R# is convinced this might happen :/
{
return null;
}

var seekNamespace = propertyType.Namespace.Split('.');
return types.Aggregate(
(Type)null,
(acc, cur) =>
{
if (acc?.Namespace == null || cur.Namespace == null)
{
return cur;
}

var accParts = acc.Namespace.Split('.');
var curParts = cur.Namespace.Split('.');
var accMatchIndex = seekNamespace.MatchIndexFor(accParts);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net462;netstandard2.0</TargetFrameworks>
<TargetFrameworks>net462;netstandard2.0;net8.0;net7.0;net6.0</TargetFrameworks>
<Configurations>Debug;Release;Debug-X</Configurations>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<Platforms>AnyCPU</Platforms>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<PropertyGroup Condition="'$(TargetFramework)'=='netstandard2.0'">
<PropertyGroup Condition="'$(TargetFramework)'!='net462'">
<DefineConstants>NETSTANDARD</DefineConstants>
</PropertyGroup>
<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ public override void Init(Type entityType)

var generatorKey = new Tuple<Type, string>(entityType, PropertyNames.Single());
if (!Generators.ContainsKey(generatorKey))
{
Generators[generatorKey] = UniqueRandomValueGenerator.For(PropertyType);
}

_generator = Generators[generatorKey];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ public static int MatchIndexFor(this string[] src, string[] other)
private static int StringCompare(string first, string second)
{
if (first == second)
{
return 0;
}

var ordered = new[]
{
first,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,10 @@ private int GenerateInt()
{
var max = _used.Count * 2;
if (max < 10)
{
max = 10;
}

return GetRandomInt(1, max);
}
}

0 comments on commit 84bc3ce

Please sign in to comment.