Skip to content
Draft
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace JSLTSharp.Tests.Transforms
{
[TestClass]
public class ToDecimalTransformTests : BaseTestsClass
{
[TestMethod]
public void TestFloat_ToDecimal()
{
TestJsonTransformation(@"{
'value': 13246.51
}",
@"{
'result': '$.value->ToDecimal()'
}",
@"{
'result': 13246.51
}");
}

[TestMethod]
public void TestInteger_ToDecimal()
{
TestJsonTransformation(@"{
'value': 100
}",
@"{
'result': '$.value->ToDecimal()'
}",
@"{
'result': 100.0
}");
}

[TestMethod]
public void TestString_ValidNumber()
{
TestJsonTransformation(@"{
'value': '42.5'
}",
@"{
'result': '$.value->ToDecimal()'
}",
@"{
'result': 42.5
}");
}

[TestMethod]
public void TestString_InvalidNumber()
{
TestJsonTransformation(@"{
'value': 'notanumber'
}",
@"{
'result': '$.value->ToDecimal()'
}",
@"{
'result': null
}");
}

[TestMethod]
public void TestBoolean_True()
{
TestJsonTransformation(@"{
'value': true
}",
@"{
'result': '$.value->ToDecimal()'
}",
@"{
'result': 1.0
}");
}

[TestMethod]
public void TestBoolean_False()
{
TestJsonTransformation(@"{
'value': false
}",
@"{
'result': '$.value->ToDecimal()'
}",
@"{
'result': 0.0
}");
}

[TestMethod]
public void TestNull()
{
TestJsonTransformation(@"{
'value': null
}",
@"{
'result': '$.value->ToDecimal()'
}",
@"{
'result': null
}");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace JSLTSharp.Tests.Transforms
{
[TestClass]
public class ToLowerTransformTests : BaseTestsClass
{
[TestMethod]
public void TestValidResult()
{
TestJsonTransformation(@"{
'upper': 'AZERTY'
}",
@"{
'lower': '$.upper->ToLower()'
}",
@"{
'lower': 'azerty'
}");
}

[TestMethod]
public void TestNumber()
{
TestJsonTransformation(@"{
'upper': 123
}",
@"{
'lower': '$.upper->ToLower()'
}",
@"{
'lower': 123
}");
}

[TestMethod]
public void TestNull()
{
TestJsonTransformation(@"{
'upper': null
}",
@"{
'lower': '$.upper->ToLower()'
}",
@"{
'lower': null
}");
}

[TestMethod]
public void TestMixedCase()
{
TestJsonTransformation(@"{
'value': 'Hello World'
}",
@"{
'result': '$.value->ToLower()'
}",
@"{
'result': 'hello world'
}");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace JSLTSharp.Tests.Transforms
{
[TestClass]
public class TrimTransformTests : BaseTestsClass
{
[TestMethod]
public void TestLeadingAndTrailingWhitespace()
{
TestJsonTransformation(@"{
'value': ' hello '
}",
@"{
'result': '$.value->Trim()'
}",
@"{
'result': 'hello'
}");
}

[TestMethod]
public void TestLeadingWhitespaceOnly()
{
TestJsonTransformation(@"{
'value': ' hello'
}",
@"{
'result': '$.value->Trim()'
}",
@"{
'result': 'hello'
}");
}

[TestMethod]
public void TestTrailingWhitespaceOnly()
{
TestJsonTransformation(@"{
'value': 'hello '
}",
@"{
'result': '$.value->Trim()'
}",
@"{
'result': 'hello'
}");
}

[TestMethod]
public void TestNoWhitespace()
{
TestJsonTransformation(@"{
'value': 'hello'
}",
@"{
'result': '$.value->Trim()'
}",
@"{
'result': 'hello'
}");
}

[TestMethod]
public void TestNumber()
{
TestJsonTransformation(@"{
'value': 123
}",
@"{
'result': '$.value->Trim()'
}",
@"{
'result': 123
}");
}

[TestMethod]
public void TestNull()
{
TestJsonTransformation(@"{
'value': null
}",
@"{
'result': '$.value->Trim()'
}",
@"{
'result': null
}");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using JSLTSharp.JsonTransforms.Abstractions;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Globalization;

namespace JSLTSharp.JsonTransforms.EmbededFunctions.ValueTransformations
{
public class ToDecimalTransformOperation : IJsonTransformCustomOperation
{
/// <inheritdoc />
public string OperationName => "ToDecimal";

/// <inheritdoc />
public JToken Apply(JToken dataSource, JToken objectToApplyTo, IList<string> parameters)
{
switch (objectToApplyTo.Type)
{
case JTokenType.String:
var stringValue = objectToApplyTo.Value<string>();
if (decimal.TryParse(stringValue, NumberStyles.Any, CultureInfo.InvariantCulture, out decimal convertedString))
return JValue.FromObject(convertedString);
else
return JValue.CreateNull();
case JTokenType.Integer:
return JValue.FromObject((decimal)objectToApplyTo.Value<long>());
case JTokenType.Float:
return JValue.FromObject((decimal)objectToApplyTo.Value<double>());
case JTokenType.Boolean:
var boolValue = objectToApplyTo.Value<bool>();
return JValue.FromObject(boolValue ? 1m : 0m);
default:
return JValue.CreateNull();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using JSLTSharp.JsonTransforms.Abstractions;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;

namespace JSLTSharp.JsonTransforms.EmbededFunctions.ValueTransformations
{
public class ToLowerTransformationOperation : IJsonTransformCustomOperation
{
/// <inheritdoc />
public virtual string OperationName => "ToLower";

/// <inheritdoc />
public virtual JToken Apply(JToken dataSource, JToken objectToApplyTo, IList<string> parameters)
{
if (objectToApplyTo.Type != JTokenType.String)
return objectToApplyTo;

return objectToApplyTo.ToString().ToLowerInvariant();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using JSLTSharp.JsonTransforms.Abstractions;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;

namespace JSLTSharp.JsonTransforms.EmbededFunctions.ValueTransformations
{
public class TrimTransformOperation : IJsonTransformCustomOperation
{
/// <inheritdoc />
public virtual string OperationName => "Trim";

/// <inheritdoc />
public virtual JToken Apply(JToken dataSource, JToken objectToApplyTo, IList<string> parameters)
{
if (objectToApplyTo.Type != JTokenType.String)
return objectToApplyTo;

return objectToApplyTo.ToString().Trim();
}
}
}
15 changes: 15 additions & 0 deletions src/JSLTSharp/JsonTransforms/Extensions/JsonTransformExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@ public static void RegisterJsonCustomTransformFunctions(this IServiceCollection
serviceCollection.AddSingleton<IJsonTransformCustomOperation, FormatDateTransformOperation>();
serviceCollection.AddSingleton<IJsonTransformCustomOperation, ToBooleanTransformOperation>();
serviceCollection.AddSingleton<IJsonTransformCustomOperation, ToIntegerTransformOperation>();
serviceCollection.AddSingleton<IJsonTransformCustomOperation, ToDecimalTransformOperation>();
serviceCollection.AddSingleton<IJsonTransformCustomOperation, ConcatStringTransformationOperation>();
serviceCollection.AddSingleton<IJsonTransformCustomOperation, DistinctArrayTransformOperation>();
serviceCollection.AddSingleton<IJsonTransformCustomOperation, ToUpperTransformationOperation>();
serviceCollection.AddSingleton<IJsonTransformCustomOperation, ToLowerTransformationOperation>();
serviceCollection.AddSingleton<IJsonTransformCustomOperation, TrimTransformOperation>();

serviceCollection.AddSingleton<IJsonTransformConditionalCustomOperation, IfIsNotEqualsConditionalKeyOperation>();
serviceCollection.AddSingleton<IJsonTransformConditionalCustomOperation, IfIsEqualsConditionalKeyOperation>();
Expand All @@ -27,5 +30,17 @@ public static void RegisterJsonCustomTransformFunctions(this IServiceCollection
serviceCollection.AddSingleton<IJsonTransformConditionalCustomOperation, IfNotEmptyConditionalKeyOperation>();
serviceCollection.AddSingleton<IJsonTransformConditionalCustomOperation, NotNullConditionalTransformOperation>();
}

/// <summary>
/// Register the JsonTransform engine along with all built-in transformation functions in the service collection.
/// This is a convenience method that combines <see cref="RegisterJsonCustomTransformFunctions"/> with
/// registering the <see cref="JsonTransform"/> engine itself.
/// </summary>
/// <param name="serviceCollection">The service collection to register dependencies into.</param>
public static void AddJsonTransform(this IServiceCollection serviceCollection)
{
serviceCollection.RegisterJsonCustomTransformFunctions();
serviceCollection.AddSingleton<JsonTransform>();
}
}
}