Skip to content

Commit

Permalink
Misc improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
soenneker committed Feb 19, 2024
1 parent ca7e485 commit fccae21
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 42 deletions.
2 changes: 2 additions & 0 deletions src/Abstract/IAutoFakerBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public interface IAutoFakerBinder
/// </summary>
/// <typeparam name="TType">The type of instance to create.</typeparam>
/// <param name="context">The <see cref="AutoFakerContext"/> instance for the generate request.</param>
/// <param name="cachedType"></param>
/// <returns>The created instance.</returns>
TType? CreateInstance<TType>(AutoFakerContext context, CachedType cachedType);

Expand All @@ -22,6 +23,7 @@ public interface IAutoFakerBinder
/// <typeparam name="TType">The type of instance to populate.</typeparam>
/// <param name="instance">The instance to populate.</param>
/// <param name="context">The <see cref="AutoFakerContext"/> instance for the generate request.</param>
/// <param name="cachedType"></param>
/// <remarks>
/// Due to the boxing nature of value types, the <paramref name="instance"/> parameter is an object. This means the populated
/// values are applied to the provided instance and not a copy.
Expand Down
26 changes: 12 additions & 14 deletions src/AutoFakerBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public AutoFakerBinder(AutoFakerConfig autoFakerConfig)
/// </summary>
/// <typeparam name="TType">The type of instance to create.</typeparam>
/// <param name="context">The <see cref="AutoFakerContext"/> instance for the generate request.</param>
/// <param name="cachedType"></param>
/// <returns>The created instance of <typeparamref name="TType"/>.</returns>
public TType? CreateInstance<TType>(AutoFakerContext context, CachedType cachedType)
{
Expand Down Expand Up @@ -71,6 +72,7 @@ public AutoFakerBinder(AutoFakerConfig autoFakerConfig)
/// <typeparam name="TType">The type of instance to populate.</typeparam>
/// <param name="instance">The instance to populate.</param>
/// <param name="context">The <see cref="AutoFakerContext"/> instance for the generate request.</param>
/// <param name="cachedType"></param>
/// <remarks>
/// Due to the boxing nature of value types, the <paramref name="instance"/> parameter is an object. This means the populated
/// values are applied to the provided instance and not a copy.
Expand Down Expand Up @@ -134,23 +136,19 @@ private bool ShouldSkip(AutoMember autoMember, AutoFakerContext context)
return true;

//check if tree depth is reached
int? treeDepth = _autoFakerConfig.TreeDepth;

if (treeDepth != null)
if (_autoFakerConfig.TreeDepth != null)
{
if (context.TypesStack.Count >= treeDepth)
if (context.TypesStack.Count >= _autoFakerConfig.TreeDepth)
return true;
}

if (context.TypesStack.Count == 0)
if (context.TypesStack.Count < _autoFakerConfig.RecursiveDepth)
return false;

// Finally check if the recursive depth has been reached
int typeCount = context.TypesStack.Count(c => c == autoMember.CachedType.CacheKey);

int count = context.TypesStack.Count(c => c == autoMember.CachedType.CacheKey);
int recursiveDepth = _autoFakerConfig.RecursiveDepth;

if (count >= recursiveDepth)
if (typeCount >= _autoFakerConfig.RecursiveDepth)
return true;

return false;
Expand All @@ -163,15 +161,15 @@ private bool ShouldSkip(AutoMember autoMember, AutoFakerContext context)

CachedConstructor[]? constructors = cachedType.GetCachedConstructors();

if (constructors == null)
return null;

if (cachedType.IsDictionary)
return ResolveTypedConstructor(CachedTypeService.IDictionary.Value, constructors);

if (cachedType.IsEnumerable)
return ResolveTypedConstructor(CachedTypeService.IEnumerable.Value, constructors);

if (constructors == null)
return null;

for (var i = 0; i < constructors.Length; i++)
{
CachedConstructor constructor = constructors[i];
Expand Down Expand Up @@ -232,15 +230,15 @@ private List<AutoMember> GetMembersToPopulate(CachedType cachedType)
var autoMembers = new List<AutoMember>();

CachedProperty[] cachedProperties = cachedType.GetCachedProperties()!;
CachedField[]? cachedFields = cachedType.GetCachedFields();
autoMembers.Capacity = cachedProperties.Length + (cachedFields?.Length ?? 0);

for (var i = 0; i < cachedProperties.Length; i++)
{
CachedProperty cachedProperty = cachedProperties[i];
autoMembers.Add(new AutoMember(cachedProperty, _autoFakerConfig));
}

CachedField[]? cachedFields = cachedType.GetCachedFields();

if (cachedFields != null)
{
for (var i = 0; i < cachedFields.Length; i++)
Expand Down
9 changes: 0 additions & 9 deletions src/Extensions/AutoGenerateOverrideContextExtension.cs

This file was deleted.

15 changes: 10 additions & 5 deletions src/Generators/AutoFakerGeneratorMemberOverride.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
using System;
using Soenneker.Reflection.Cache.Types;
using Soenneker.Utils.AutoBogus.Context;
using Soenneker.Utils.AutoBogus.Services;

namespace Soenneker.Utils.AutoBogus.Generators;

internal sealed class AutoFakerGeneratorMemberOverride<TType, TValue> : AutoFakerGeneratorOverride
{
private Type Type { get; }
private readonly CachedType _cachedType;

private string MemberName { get; }

private Func<AutoFakerOverrideContext, TValue> Generator { get; }

internal AutoFakerGeneratorMemberOverride(string memberName, Func<AutoFakerOverrideContext, TValue> generator)
{
if (string.IsNullOrWhiteSpace(memberName))
Expand All @@ -14,17 +22,14 @@ internal AutoFakerGeneratorMemberOverride(string memberName, Func<AutoFakerOverr
}

Type = typeof(TType);
_cachedType = CacheService.Cache.GetCachedType(Type);
MemberName = memberName;
Generator = generator ?? throw new ArgumentNullException(nameof(generator));
}

private Type Type { get; }
private string MemberName { get; }
private Func<AutoFakerOverrideContext, TValue> Generator { get; }

public override bool CanOverride(AutoFakerContext context)
{
return context.ParentType == CacheService.Cache.GetCachedType(Type) && MemberName.Equals(context.GenerateName, StringComparison.OrdinalIgnoreCase);
return context.ParentType == _cachedType && MemberName.Equals(context.GenerateName, StringComparison.OrdinalIgnoreCase);
}

public override void Generate(AutoFakerOverrideContext context)
Expand Down
7 changes: 3 additions & 4 deletions src/Generators/AutoFakerGeneratorOverrideInvoker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,17 @@

namespace Soenneker.Utils.AutoBogus.Generators;

internal sealed class AutoFakerGeneratorOverrideInvoker
: IAutoFakerGenerator
internal sealed class AutoFakerGeneratorOverrideInvoker : IAutoFakerGenerator
{
internal AutoFakerGeneratorOverrideInvoker(IAutoFakerGenerator generator, List<AutoFakerGeneratorOverride> overrides)
{
Generator = generator;
Overrides = overrides;
}

internal IAutoFakerGenerator Generator { get; }
private IAutoFakerGenerator Generator { get; }

internal List<AutoFakerGeneratorOverride> Overrides { get; }
private List<AutoFakerGeneratorOverride> Overrides { get; }

object IAutoFakerGenerator.Generate(AutoFakerContext context)
{
Expand Down
8 changes: 4 additions & 4 deletions src/Generators/AutoFakerGeneratorTypeOverride.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@ namespace Soenneker.Utils.AutoBogus.Generators;

internal sealed class AutoFakerGeneratorTypeOverride<TType> : AutoFakerGeneratorOverride
{
private CachedType CachedType { get; }
private readonly CachedType _cachedType;

private Func<AutoFakerOverrideContext, TType> Generator { get; }

internal AutoFakerGeneratorTypeOverride(Func<AutoFakerOverrideContext, TType> generator)
{
CachedType = CacheService.Cache.GetCachedType(typeof(TType));

Generator = generator ?? throw new ArgumentNullException(nameof(generator));

_cachedType = CacheService.Cache.GetCachedType(typeof(TType));
}

public override bool CanOverride(AutoFakerContext context)
{
return context.CachedType == CachedType;
return context.CachedType == _cachedType;
}

public override void Generate(AutoFakerOverrideContext context)
Expand Down
11 changes: 8 additions & 3 deletions src/Generators/Types/DateTimeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@ internal sealed class DateTimeGenerator
{
object IAutoFakerGenerator.Generate(AutoFakerContext context)
{
return context.Config.DateTimeKind == DateTimeKind.Utc
? context.Faker.Date.Recent().ToUniversalTime()
: context.Faker.Date.Recent();
DateTime dateTime = context.Faker.Date.Recent();

if (context.Config.DateTimeKind == DateTimeKind.Utc)
{
return dateTime.ToUniversalTime();
}

return dateTime;
}
}
4 changes: 2 additions & 2 deletions src/Generators/Types/DateTimeOffsetGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ internal sealed class DateTimeOffsetGenerator
{
object IAutoFakerGenerator.Generate(AutoFakerContext context)
{
DateTime dateTime = context.Faker.Date.Recent();
return new DateTimeOffset(dateTime);
DateTimeOffset result = context.Faker.Date.RecentOffset();
return result;
}
}
3 changes: 3 additions & 0 deletions src/Generators/Types/TypeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ object IAutoFakerGenerator.Generate(AutoFakerContext context)

object instance = context.Binder.CreateInstance<TType>(context, cachedType);

Check warning on line 17 in src/Generators/Types/TypeGenerator.cs

View workflow job for this annotation

GitHub Actions / publish-package

Converting null literal or possible null value to non-nullable type.

if (instance == null)
return null!;

// Populate the generated instance
context.Binder.PopulateInstance<TType>(instance, context, cachedType);

Expand Down
5 changes: 5 additions & 0 deletions src/Override/Abstract/IAutoFakerOverride.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace Soenneker.Utils.AutoBogus.Override.Abstract;

public interface IAutoFakerOverride
{
}
4 changes: 3 additions & 1 deletion src/Override/AutoFakerOverride.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using System;
using Soenneker.Utils.AutoBogus.Context;
using Soenneker.Utils.AutoBogus.Generators;
using Soenneker.Utils.AutoBogus.Override.Abstract;

namespace Soenneker.Utils.AutoBogus.Override;

public abstract class AutoFakerOverride<T> : AutoFakerGeneratorOverride
///<inheritdoc cref="IAutoFakerOverride"/>
public abstract class AutoFakerOverride<T> : AutoFakerGeneratorOverride, IAutoFakerOverride
{
protected Type Type { get; }

Expand Down

0 comments on commit fccae21

Please sign in to comment.