-
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathRandomHelpers.cs
More file actions
113 lines (96 loc) · 3.86 KB
/
Copy pathRandomHelpers.cs
File metadata and controls
113 lines (96 loc) · 3.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using System;
using System.Collections.Generic;
using System.Linq;
using HandlebarsDotNet.Helpers.Attributes;
using HandlebarsDotNet.Helpers.Enums;
using HandlebarsDotNet.Helpers.Helpers;
using HandlebarsDotNet.Helpers.Models;
using HandlebarsDotNet.Helpers.Parsers;
using RandomDataGenerator.FieldOptions;
using RandomDataGenerator.Randomizers;
// ReSharper disable once CheckNamespace
namespace HandlebarsDotNet.Helpers;
internal class RandomHelpers : BaseHelpers, IHelpers
{
public RandomHelpers(IHandlebars context) : base(context)
{
}
/// <summary>
/// For backwards compatibility with WireMock.Net
/// </summary>
[HandlebarsWriter(WriterType.String, "Random")]
public object? Random(IDictionary<string, object?> hash)
{
return Generate(hash);
}
/// <summary>
/// For backwards compatibility with WireMock.Net
/// </summary>
[HandlebarsWriter(WriterType.String, "RandomKeepType")]
public string? RandomKeepType(IDictionary<string, object?> hash)
{
return GenerateAsOutputWithType(hash);
}
[HandlebarsWriter(WriterType.Value)]
public object? Generate(IDictionary<string, object?> hash)
{
var keepType = hash.TryGetValue("KeepType", out var value) && value is true;
return GenerateInternal(hash, keepType);
}
/// <summary>
/// It returns a JSON string.
/// </summary>
[HandlebarsWriter(WriterType.String)]
public string? GenerateAsOutputWithType(IDictionary<string, object?> hash)
{
return GenerateInternal(hash, true) is not OutputWithType outputWithType ? null : outputWithType.Serialize();
}
private object? GenerateInternal(IDictionary<string, object?> hash, bool outputWithType)
{
var fieldOptions = GetFieldOptionsFromHash(hash);
dynamic randomizer = RandomizerFactory.GetRandomizerAsDynamic(fieldOptions);
// Format DateTime as ISO 8601
if (fieldOptions is IFieldOptionsDateTime)
{
DateTime? date = randomizer.Generate();
return GetRandomValue(outputWithType, () => date?.ToString("s", Context.Configuration.FormatProvider), () => date);
}
// If the IFieldOptionsGuid defines Uppercase, use the 'GenerateAsString' method.
if (fieldOptions is IFieldOptionsGuid fieldOptionsGuid)
{
return GetRandomValue(outputWithType,
() => fieldOptionsGuid.Uppercase ? randomizer.GenerateAsString() : randomizer.Generate(),
() => randomizer.Generate()
);
}
return GetRandomValue(outputWithType, () => randomizer.Generate(), null);
}
private FieldOptionsAbstract GetFieldOptionsFromHash(IDictionary<string, object?> hash)
{
if (!hash.TryGetValue("Type", out var value) || value is not string randomType)
{
throw new HandlebarsException("The Type argument is missing.");
}
var newProperties = new Dictionary<string, object?>();
foreach (var item in hash.Where(p => p.Key != "Type"))
{
var convertObjectArrayToStringList = randomType == "StringList";
var parsedArgumentValue = ArgumentsParser.Parse(Context, item.Value, convertObjectArrayToStringList);
newProperties.Add(item.Key, parsedArgumentValue);
}
return FieldOptionsFactory.GetFieldOptions(randomType, newProperties!);
}
private static object? GetRandomValue(bool outputWithType, Func<object?> funcNormal, Func<object?>? funcWithType)
{
object? value;
if (outputWithType && funcWithType != null)
{
value = funcWithType();
}
else
{
value = funcNormal();
}
return outputWithType ? OutputWithType.Parse(value) : value;
}
}