mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-26 10:18:26 +02:00
Update Handlebars Transformer logic (ReplaceNodeOptions) (#1036)
* Update Handlebars Transformer logic (ReplaceNodeOptions) * okeee * EvaluateAndKeep = Evaluate * fix? * linux * _ * tt * xxx * fx * x * fix test
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using HandlebarsDotNet.Helpers.Models;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Stef.Validation;
|
||||
@@ -13,8 +13,6 @@ namespace WireMock.Transformers;
|
||||
|
||||
internal class Transformer : ITransformer
|
||||
{
|
||||
private static readonly Type[] SupportedTypes = { typeof(bool), typeof(long), typeof(int), typeof(double), typeof(Guid), typeof(DateTime), typeof(TimeSpan), typeof(Uri) };
|
||||
|
||||
private readonly JsonSerializer _jsonSerializer;
|
||||
private readonly ITransformerContextFactory _factory;
|
||||
|
||||
@@ -268,9 +266,10 @@ internal class Transformer : ITransformer
|
||||
return;
|
||||
|
||||
case string transformedString:
|
||||
if (TryConvert(transformedString, out var convertedFromStringValue))
|
||||
var (isConvertedFromString, convertedValueFromString) = TryConvert(options, transformedString);
|
||||
if (isConvertedFromString)
|
||||
{
|
||||
node.Replace(JToken.FromObject(convertedFromStringValue, _jsonSerializer));
|
||||
node.Replace(JToken.FromObject(convertedValueFromString, _jsonSerializer));
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -292,7 +291,8 @@ internal class Transformer : ITransformer
|
||||
break;
|
||||
|
||||
case { }:
|
||||
if (TryConvert(transformedValue, out var convertedValue))
|
||||
var (isConverted, convertedValue) = TryConvert(options, transformedValue);
|
||||
if (isConverted)
|
||||
{
|
||||
node.Replace(JToken.FromObject(convertedValue, _jsonSerializer));
|
||||
}
|
||||
@@ -308,23 +308,28 @@ internal class Transformer : ITransformer
|
||||
return JsonUtils.TryParseAsJObject(stringValue, out var parsedAsjObject) ? parsedAsjObject : stringValue;
|
||||
}
|
||||
|
||||
private static bool TryConvert(object? transformedValue, [NotNullWhen(true)] out object? convertedValue)
|
||||
private static (bool IsConverted, object ConvertedValue) TryConvert(ReplaceNodeOptions options, object value)
|
||||
{
|
||||
foreach (var supportedType in SupportedTypes)
|
||||
var valueAsString = value as string;
|
||||
|
||||
if (options == ReplaceNodeOptions.Evaluate)
|
||||
{
|
||||
try
|
||||
if (valueAsString != null && WrappedString.TryDecode(valueAsString, out var decoded))
|
||||
{
|
||||
convertedValue = Convert.ChangeType(transformedValue, supportedType);
|
||||
return convertedValue is not null;
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Ignore
|
||||
return (true, decoded);
|
||||
}
|
||||
|
||||
return (false, value);
|
||||
}
|
||||
|
||||
convertedValue = null;
|
||||
return false;
|
||||
if (valueAsString != null)
|
||||
{
|
||||
return WrappedString.TryDecode(valueAsString, out var decoded) ?
|
||||
(true, decoded) :
|
||||
StringUtils.TryConvertToKnownType(valueAsString);
|
||||
}
|
||||
|
||||
return (false, value);
|
||||
}
|
||||
|
||||
private static IBodyData TransformBodyAsString(ITransformerContext transformerContext, object model, IBodyData original)
|
||||
|
||||
Reference in New Issue
Block a user