Skip to content

Commit

Permalink
exclude non exisiting paths. (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
FransVanEk authored Oct 16, 2022
1 parent 8e554da commit 44cce34
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion JLio.Functions/Partial.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ private IEnumerable<string> GetPathsToRemove(JToken currentToken, List<JToken> v
{
var sourcePaths = currentToken.GetAllElements().Select(i => i.Path).ToList();
var selectionPaths = new List<JToken>();
values.ForEach(v => selectionPaths.AddRange(v.GetAllElements()));
values.Where(v => !JToken.DeepEquals(v, JValue.CreateNull())).ToList().ForEach(v => selectionPaths.AddRange(v.GetAllElements()));
selectionPaths.Select(s => s.Path).ToList().ForEach(p =>
{
RemoveSelectionItems(p, sourcePaths);
Expand Down
12 changes: 12 additions & 0 deletions JLio.UnitTests/FunctionsTests/PartialTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,18 @@ public void WillReturnError(string function, string data)
Assert.IsTrue(executeContext.Logger.LogEntries.Any(i => i.Level == LogLevel.Error));
}

[TestCase("=partial(@.z, @.c.d, @.c.z)", "{\"result\":{\"a\":1,\"b\":[1,2,3],\"c\":{\"d\":5,\"e\":[4,5,6]}}}",
"{\"c\":{\"d\":5}}")]
public void CanHandleNonExisitedPaths(string function, string data, string expectedResult)
{
var script = $"[{{\"path\":\"$.result\",\"value\":\"{function}\",\"command\":\"set\"}}]";
var result = JLioConvert.Parse(script, parseOptions).Execute(JToken.Parse(data), executeContext);

Assert.IsTrue(result.Success);
Assert.IsTrue(executeContext.Logger.LogEntries.TrueForAll(i => i.Level != LogLevel.Error));
Assert.IsTrue(JToken.DeepEquals(JToken.Parse(expectedResult), result.Data.SelectToken("$.result")));
}

[Test]
public void CanBeUsedInFluentApi()
{
Expand Down

0 comments on commit 44cce34

Please sign in to comment.