diff --git a/src/Handlebars.Net.Helpers.Core/Handlebars.Net.Helpers.Core.csproj b/src/Handlebars.Net.Helpers.Core/Handlebars.Net.Helpers.Core.csproj
index f5e8612..b3c725b 100644
--- a/src/Handlebars.Net.Helpers.Core/Handlebars.Net.Helpers.Core.csproj
+++ b/src/Handlebars.Net.Helpers.Core/Handlebars.Net.Helpers.Core.csproj
@@ -34,5 +34,4 @@
-
\ No newline at end of file
diff --git a/src/Handlebars.Net.Helpers.Core/Utils/SimpleJsonUtils.cs b/src/Handlebars.Net.Helpers.Core/Utils/SimpleJsonUtils.cs
new file mode 100644
index 0000000..4a001fd
--- /dev/null
+++ b/src/Handlebars.Net.Helpers.Core/Utils/SimpleJsonUtils.cs
@@ -0,0 +1,16 @@
+using HandlebarsDotNet.Helpers.Json;
+
+namespace HandlebarsDotNet.Helpers.Utils;
+
+public static class SimpleJsonUtils
+{
+ public static string? SerializeObject(object? value)
+ {
+ return SimpleJson.SerializeObject(value);
+ }
+
+ public static T? DeserializeObject(string? json)
+ {
+ return SimpleJson.DeserializeObject(json);
+ }
+}
\ No newline at end of file
diff --git a/src/Handlebars.Net.Helpers.Random/Handlebars.Net.Helpers.Random.csproj b/src/Handlebars.Net.Helpers.Random/Handlebars.Net.Helpers.Random.csproj
index 0dead86..7acb2eb 100644
--- a/src/Handlebars.Net.Helpers.Random/Handlebars.Net.Helpers.Random.csproj
+++ b/src/Handlebars.Net.Helpers.Random/Handlebars.Net.Helpers.Random.csproj
@@ -12,6 +12,10 @@
+
+ all
+ runtime; build; native; contentfiles; analyzers; buildtransitive
+
diff --git a/src/Handlebars.Net.Helpers.Random/Helpers/RandomHelpers.cs b/src/Handlebars.Net.Helpers.Random/Helpers/RandomHelpers.cs
index 3aa4030..3e3d0e3 100644
--- a/src/Handlebars.Net.Helpers.Random/Helpers/RandomHelpers.cs
+++ b/src/Handlebars.Net.Helpers.Random/Helpers/RandomHelpers.cs
@@ -4,10 +4,12 @@
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
@@ -19,14 +21,39 @@ public RandomHelpers(IHandlebars context) : base(context)
///
/// For backwards compatibility with WireMock.Net
///
- [HandlebarsWriter(WriterType.Value, "Random")]
+ [HandlebarsWriter(WriterType.String, "Random")]
public object? Random(IDictionary hash)
{
return Generate(hash);
}
+ ///
+ /// For backwards compatibility with WireMock.Net
+ ///
+ [HandlebarsWriter(WriterType.String, "RandomKeepType")]
+ public string? RandomKeepType(IDictionary hash)
+ {
+ return GenerateAsOutputWithType(hash);
+ }
+
[HandlebarsWriter(WriterType.Value)]
public object? Generate(IDictionary hash)
+ {
+ var keepType = hash.TryGetValue("KeepType", out var value) && value is true;
+
+ return GenerateInternal(hash, keepType);
+ }
+
+ ///
+ /// It returns a JSON string.
+ ///
+ [HandlebarsWriter(WriterType.String)]
+ public string? GenerateAsOutputWithType(IDictionary hash)
+ {
+ return GenerateInternal(hash, true) is not OutputWithType outputWithType ? null : outputWithType.Serialize();
+ }
+
+ private object? GenerateInternal(IDictionary hash, bool outputWithType)
{
var fieldOptions = GetFieldOptionsFromHash(hash);
dynamic randomizer = RandomizerFactory.GetRandomizerAsDynamic(fieldOptions);
@@ -35,34 +62,52 @@ public RandomHelpers(IHandlebars context) : base(context)
if (fieldOptions is IFieldOptionsDateTime)
{
DateTime? date = randomizer.Generate();
- return date?.ToString("s", Context.Configuration.FormatProvider);
+ 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 fieldOptionsGuid.Uppercase ? randomizer.GenerateAsString() : randomizer.Generate();
+ return GetRandomValue(outputWithType,
+ () => fieldOptionsGuid.Uppercase ? randomizer.GenerateAsString() : randomizer.Generate(),
+ () => randomizer.Generate()
+ );
}
- return randomizer.Generate();
+ return GetRandomValue(outputWithType, () => randomizer.Generate(), null);
}
private FieldOptionsAbstract GetFieldOptionsFromHash(IDictionary hash)
{
- if (hash.TryGetValue("Type", out var value) && value is string randomType)
+ if (!hash.TryGetValue("Type", out var value) || value is not string randomType)
+ {
+ throw new HandlebarsException("The Type argument is missing.");
+ }
+
+ var newProperties = new Dictionary();
+ foreach (var item in hash.Where(p => p.Key != "Type"))
{
- var newProperties = new Dictionary();
- foreach (var item in hash.Where(p => p.Key != "Type"))
- {
- bool convertObjectArrayToStringList = randomType == "StringList";
- var parsedArgumentValue = ArgumentsParser.Parse(Context, item.Value, convertObjectArrayToStringList);
+ var convertObjectArrayToStringList = randomType == "StringList";
+ var parsedArgumentValue = ArgumentsParser.Parse(Context, item.Value, convertObjectArrayToStringList);
+
+ newProperties.Add(item.Key, parsedArgumentValue);
+ }
- newProperties.Add(item.Key, parsedArgumentValue);
- }
+ return FieldOptionsFactory.GetFieldOptions(randomType, newProperties!);
+ }
- return FieldOptionsFactory.GetFieldOptions(randomType, newProperties!);
+ private static object? GetRandomValue(bool outputWithType, Func