Skip to content

Commit

Permalink
Merge pull request #35 from neuroglia-io/fix-jq-string-interpolation
Browse files Browse the repository at this point in the history
Handled string interpolation in JQ expression evaluator
  • Loading branch information
cdavernas authored Nov 7, 2022
2 parents 1e415fd + e088fe8 commit 89e660e
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 8 deletions.
12 changes: 6 additions & 6 deletions src/Data/Neuroglia.Data.Expressions.JQ/JQExpressionEvaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ public virtual bool Supports(string language)
.ToDictionary(a => a.Key, a => JsonConvert.SerializeObject(JToken.FromObject(a.Value), Formatting.None, serializerSettings))
.Aggregate(
new StringBuilder(),
(accumulator, source) => accumulator.Append(@$"--argjson ""{source.Key}"" ""{this.EscapeDoubleQuotes(source.Value)}"" "),
(accumulator, source) => accumulator.Append(@$"--argjson ""{source.Key}"" ""{this.EscapeArgs(source.Value)}"" "),
sb => sb.ToString()
);
var processArguments = $"\"{this.EscapeDoubleQuotes(expression)}\" {serializedArgs} -c";
var processArguments = @$"""{this.EscapeArgs(expression)}"" {serializedArgs} -c";
var files = new List<string>();
var maxLength = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? 8000 : 200000;
if (processArguments.Length >= maxLength)
Expand Down Expand Up @@ -160,11 +160,11 @@ public virtual bool Supports(string language)
}

/// <summary>
/// Escapes double quotes in the specified string
/// Escapes double quotes (") or backslashes (\) in the provided string
/// </summary>
/// <param name="input">The string for which to escape double quotes</param>
/// <returns>The string with escaped double quotes</returns>
protected virtual string EscapeDoubleQuotes(string input) => Regex.Replace(input, "([\"\\\\])", @"\$1", RegexOptions.Compiled);
/// <param name="input">The string to escape</param>
/// <returns>The escaped string</returns>
protected virtual string EscapeArgs(string input) => Regex.Replace(input, @"(\\(?!\()|"")", @"\$1", RegexOptions.Compiled);


}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
${ .foo + " " + .bar }
4 changes: 4 additions & 0 deletions test/Neuroglia.UnitTests/Assets/string-concat.input.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"foo": "hello",
"bar": "world"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
${ "\(.foo.bar) is a greeting" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"foo": {
"bar": "hello world"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,36 @@ public void Evaluate_EscapedJsonInput_UsingNewtonsoft_ShouldWork()
Assert.NotEmpty(result.name);
}

[Fact]
public void Evaluate_String_Concatenation_ShouldWork()
{
//arrange
var evaluator = BuildExpressionEvaluatorWithSystemTextJsonSerializer();
var data = Newtonsoft.Json.JsonConvert.DeserializeObject<ExpandoObject>(File.ReadAllText(Path.Combine("Assets", "string-concat.input.json")));
var expression = File.ReadAllText(Path.Combine("Assets", "string-concat.expression.txt"));

//act
string result = (string)evaluator.Evaluate(expression, data, typeof(string), null);

//assert
result.Should().Be("hello world");
}

[Fact]
public void Evaluate_String_Interpolation_ShouldWork()
{
//arrange
var evaluator = BuildExpressionEvaluatorWithSystemTextJsonSerializer();
var data = Newtonsoft.Json.JsonConvert.DeserializeObject<ExpandoObject>(File.ReadAllText(Path.Combine("Assets", "string-interpolation.input.json")));
var expression = File.ReadAllText(Path.Combine("Assets", "string-interpolation.expression.txt"));

//act
string result = (string)evaluator.Evaluate(expression, data, typeof(string), null);

//assert
result.Should().Be("hello world is a greeting");
}

static IExpressionEvaluator BuildExpressionEvaluatorWithNewtonsoftJsonSerializer()
{
var services = new ServiceCollection();
Expand Down
12 changes: 12 additions & 0 deletions test/Neuroglia.UnitTests/Neuroglia.UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,18 @@
<None Update="Assets\openapi.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Assets\string-concat.expression.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Assets\string-concat.input.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Assets\string-interpolation.input.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Assets\string-interpolation.expression.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Assets\pets.expression.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down

0 comments on commit 89e660e

Please sign in to comment.