Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Loop alias on #applyover function #287

Merged
merged 4 commits into from
Dec 30, 2023
Merged
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
27 changes: 16 additions & 11 deletions JUST.net/JsonTransformer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -912,11 +912,17 @@ private object ParseFunction(string functionString, State state)
}
}

private object ParseApplyOver(State state, object[] parameters)
private object ParseApplyOver(IList<object> listParameters, State state)
{
object output;
var contextInput = Context.Input;
var input = JToken.Parse(Transform(parameters[0].ToString(), contextInput.ToString()));
JToken tmpContext = Context.Input, contextInput = Context.Input;
if (state.ParentArray.Any())
{
var alias = ParseLoopAlias(listParameters, 3, state.ParentArray.Last().Key.Key);
contextInput = state.GetAliasToken(alias);
}

var input = JToken.Parse(Transform(listParameters.ElementAt(0).ToString(), contextInput.ToString()));
Context.Input = input;

IDictionary<LevelKey, JToken> tmpArray = new Dictionary<LevelKey, JToken>(state.CurrentArrayToken);
Expand All @@ -928,21 +934,21 @@ private object ParseApplyOver(State state, object[] parameters)
state.CurrentScopeToken.Clear();
state.CurrentScopeToken.Add(new LevelKey { Key = "root", Level = 0 }, input);

if (parameters[1].ToString().Trim().Trim('\'').StartsWith("{"))
if (listParameters.ElementAt(1).ToString().Trim().Trim('\'').StartsWith("{"))
{
var jobj = JObject.Parse(parameters[1].ToString().Trim().Trim('\''));
var jobj = JObject.Parse(listParameters.ElementAt(1).ToString().Trim().Trim('\''));
output = new JsonTransformer(Context).Transform(jobj, input);
}
else if (parameters[1].ToString().Trim().Trim('\'').StartsWith("["))
else if (listParameters.ElementAt(1).ToString().Trim().Trim('\'').StartsWith("["))
{
var jarr = JArray.Parse(parameters[1].ToString().Trim().Trim('\''));
var jarr = JArray.Parse(listParameters.ElementAt(1).ToString().Trim().Trim('\''));
output = new JsonTransformer(Context).Transform(jarr, input);
}
else
{
output = ParseFunction(parameters[1].ToString().Trim().Trim('\''), state);
output = ParseFunction(listParameters.ElementAt(1).ToString().Trim().Trim('\''), state);
}
Context.Input = contextInput;
Context.Input = tmpContext;

state.CurrentArrayToken.Clear();
foreach (KeyValuePair<LevelKey, JToken> item in tmpArray)
Expand All @@ -955,7 +961,6 @@ private object ParseApplyOver(State state, object[] parameters)
{
state.CurrentScopeToken.Add(item);
}

return output;
}

Expand Down Expand Up @@ -1049,7 +1054,7 @@ private object GetFunctionOutput(string functionName, IList<object> listParamete
}
else if (functionName == "applyover")
{
output = ParseApplyOver(state, listParameters.ToArray());
output = ParseApplyOver(listParameters, state);
}
else
{
Expand Down
30 changes: 30 additions & 0 deletions UnitTests/ApplyOverTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,35 @@ public void ReplaceGraveAccentInJsonPathExpressions()

Assert.AreEqual("{\"data\":1}", result);
}

[Test]
public void InsideLoopInputIsLoopElement()
{
var input = "{ \"sections\": [ { \"id\": \"first\", \"label\": \"First section\" }, { \"id\": \"second\", \"label\": \"Second section\" } ] }";
var transformer = "{ \"areas\": { \"#loop($.sections)\": { \"#eval(#currentvalueatpath($.id))\": \"#applyover({ 'description': '#valueof($.label)' }, '#valueof($)')\" } } }";

var context = new JUSTContext
{
EvaluationMode = EvaluationMode.Strict
};
var result = new JsonTransformer(context).Transform(transformer, input);

Assert.AreEqual("{\"areas\":[{\"first\":{\"description\":\"First section\"}},{\"second\":{\"description\":\"Second section\"}}]}", result);
}

[Test]
public void InsideNestedLoopsWithAlias()
{
var input = "{ \"headers\": [ { \"id\": \"first\", \"label\": \"First header\", \"sections\": [{ \"title\": \"first section first header\" },{ \"title\": \"second section first header\" }] }, { \"id\": \"second\", \"label\": \"Second header\", \"sections\": [{ \"title\": \"first section second header\" },{ \"title\": \"second section second header\" }] }] }";
var transformer = "{ \"areas\": { \"#loop($.headers,outside_alias)\": { \"#eval(#currentvalueatpath($.id))\": { \"#loop($.sections,inside_alias)\": { \"section\": \"#applyover({ 'description': '#valueof($.label)' }, '#valueof($)',outside_alias)\" } } } } }";

var context = new JUSTContext
{
EvaluationMode = EvaluationMode.Strict
};
var result = new JsonTransformer(context).Transform(transformer, input);

Assert.AreEqual("{\"areas\":[{\"first\":[{\"section\":{\"description\":\"First header\"}},{\"section\":{\"description\":\"First header\"}}]},{\"second\":[{\"section\":{\"description\":\"Second header\"}},{\"section\":{\"description\":\"Second header\"}}]}]}", result);
}
}
}