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

Use Lazy for expensive properties in System Dataset #550

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
64 changes: 35 additions & 29 deletions Source/Bogus/DataSets/System.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,41 +17,47 @@ public class System : DataSet
/// <param name="locale">The locale that will be used to generate values.</param>
public System(string locale = "en") : base(locale)
{
mimes = this.GetArray("mimeTypes");

lookup = mimes.OfType<BObject>()
.ToDictionary(o => o["mime"].StringValue);

mimeKeys = mimes
.OfType<BObject>()
.Select(o => o["mime"].StringValue)
.Distinct()
.ToArray();

exts = mimes
.OfType<BObject>()
.SelectMany(bObject =>
mimes = new Lazy<BArray>(
() => this.GetArray("mimeTypes"));

lookup = new Lazy<Dictionary<string, BObject>>(
() => mimes.Value.OfType<BObject>()
.ToDictionary(o => o["mime"].StringValue));

mimeKeys = new Lazy<string[]>(
() => mimes.Value
.OfType<BObject>()
.Select(o => o["mime"].StringValue)
.Distinct()
.ToArray());

exts = new Lazy<string[]>(
() => mimes.Value
.OfType<BObject>()
.SelectMany(bObject =>
{
if( bObject.ContainsKey("extensions") )
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also, if you could; small nit: avoid these small reformatting changes in existing source that doesn't change.

if (bObject.ContainsKey("extensions"))
{
var extensions = bObject["extensions"] as BArray;
return extensions.OfType<BValue>().Select(s => s.StringValue);
}

return Enumerable.Empty<string>();
})
.ToArray();

types = mimeKeys.Select(k => k.Substring(0, k.IndexOf('/')))
.Distinct()
.ToArray();
.ToArray());

types = new Lazy<string[]>(
() => mimeKeys.Value.Select(k => k.Substring(0, k.IndexOf('/')))
.Distinct()
.ToArray());
}

protected Lorem Lorem = null;
private readonly Dictionary<string, BObject> lookup;
private readonly BArray mimes;
private readonly string[] exts;
private readonly string[] types;
private readonly string[] mimeKeys;
private readonly Lazy<Dictionary<string, BObject>> lookup;
private readonly Lazy<BArray> mimes;
private readonly Lazy<string[]> exts;
private readonly Lazy<string[]> types;
private readonly Lazy<string[]> mimeKeys;

private static readonly string[] commonFileTypes =
{ "video", "audio", "image", "text", "application" };
Expand Down Expand Up @@ -145,7 +151,7 @@ public string CommonFileName(string ext = null)
/// </returns>
public string MimeType()
{
return this.Random.ArrayElement(this.mimeKeys);
return this.Random.ArrayElement(this.mimeKeys.Value);
}


Expand Down Expand Up @@ -181,7 +187,7 @@ public string CommonFileExt()
/// </returns>
public string FileType()
{
return this.Random.ArrayElement(this.types);
return this.Random.ArrayElement(this.types.Value);
}


Expand All @@ -194,13 +200,13 @@ public string FileType()
public string FileExt(string mimeType = null)
{
if( mimeType != null &&
lookup.TryGetValue(mimeType, out var mime) &&
lookup.Value.TryGetValue(mimeType, out var mime) &&
mime.ContainsKey("extensions") )
{
return this.Random.ArrayElement(mime["extensions"] as BArray);
}

return this.Random.ArrayElement(exts);
return this.Random.ArrayElement(exts.Value);
}

/// <summary>
Expand Down