Skip to content

Commit

Permalink
Fix output fields order inconsistent with the input (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shawyeok committed Jul 26, 2021
1 parent 733f39c commit d58feee
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package net.thisptr.jackson.jq.internal.tree;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
Expand All @@ -26,7 +26,7 @@ public void add(final FieldConstruction field) {

@Override
public void apply(final Scope scope, final JsonNode in, final Path ipath, final PathOutput output, final boolean requirePath) throws JsonQueryException {
final Map<String, JsonNode> tmp = new HashMap<>();
final Map<String, JsonNode> tmp = new LinkedHashMap<>(fields.size());
applyRecursive(scope, in, output, fields, tmp);
}

Expand Down
21 changes: 18 additions & 3 deletions jackson-jq/src/test/java/net/thisptr/jackson/jq/JsonQueryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Stream;

import com.fasterxml.jackson.core.JsonProcessingException;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.params.ParameterizedTest;
Expand Down Expand Up @@ -162,15 +163,15 @@ private void test(final TestCase tc, final Version version) throws Throwable {
if (!tc.ignoreTrueJqBehavior && hasJqCache.computeIfAbsent(version, v -> TrueJqEvaluator.hasJq(v))) {
final Result result = cachedJqEvaluator.evaluate(tc.q, tc.in, version, 2000L);
assumeThat(result.error).as("%s", command).isNull();
assumeThat(wrap(tc.out)).as("%s", command).isEqualTo(wrap(result.values));
assertEquals(tc.out, result.values, "%s", command);
}

boolean failed = false;
try {
final JsonQuery q = JsonQuery.compile(tc.q, version);
final List<JsonNode> out = new ArrayList<>();
q.apply(scope, tc.in, out::add);
assertThat(wrap(out)).as("%s", command).isEqualTo(wrap(tc.out));
assertEquals(tc.out, out, "%s", command);

// JsonQuery.compile($.toString()).toString() === $.toString()
final String s1 = q.toString();
Expand All @@ -181,7 +182,7 @@ private void test(final TestCase tc, final Version version) throws Throwable {
final JsonQuery q1 = JsonQuery.compile(s1, version);
final List<JsonNode> out1 = new ArrayList<>();
q1.apply(scope, tc.in, out1::add);
assertThat(wrap(out1)).as("bad tostring: %s", command).isEqualTo(wrap(tc.out));
assertEquals(tc.out, out1, "bad tostring: %s", command);
} catch (final Throwable e) {
failed = true;
if (!tc.failing) {
Expand All @@ -196,6 +197,20 @@ private void test(final TestCase tc, final Version version) throws Throwable {
assertThat(failed).describedAs("unexpectedly succeeded").isTrue();
}

private void assertEquals(List<JsonNode> expected, List<JsonNode> actual, String description, Object... args) {
assertThat(actual).as(description, args)
.usingComparator((o1, o2) -> {
try {
String o1Json = JSON_MAPPER.writeValueAsString(o1);
String o2Json = JSON_MAPPER.writeValueAsString(o2);
return o1Json.compareTo(o2Json);
} catch (JsonProcessingException e) {
throw new IllegalStateException(e);
}
})
.isEqualTo(expected);
}

@ParameterizedTest
@MethodSource("defaultTestCases")
public void test(final String tcText) throws Throwable {
Expand Down
2 changes: 1 addition & 1 deletion jackson-jq/src/test/resources/jq-test-extra-ok.json
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@
"q": ".num_entries[\"1\"] = 10",
"in": {"num_entries": {"2": 20}},
"out": [
{"num_entries": {"1": 10, "2": 20}}
{"num_entries": {"2": 20, "1": 10}}
]
},
{
Expand Down
4 changes: 2 additions & 2 deletions jackson-jq/src/test/resources/tests/jq-1.5-manual.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -440,12 +440,12 @@
- q: 'floor'
in: 3.14159
out:
- 3
- 3.0
v: '[1.5, 1.5]'
- q: 'sqrt'
in: 9
out:
- 3
- 3.0
v: '[1.5, 1.5]'
- q: '.[] | tonumber'
in: [1, "1"]
Expand Down
22 changes: 11 additions & 11 deletions jackson-jq/src/test/resources/tests/jq-1.5.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@
- q: '1+1'
in: "wtasdf"
out:
- 2.0
- 2
v: '[1.5, 1.5]'
- q: '2-1'
in: null
Expand All @@ -478,12 +478,12 @@
- q: '1e+0+0.001e3'
in: "I wonder what this will be?"
out:
- 2.0
- 2
v: '[1.5, 1.5]'
- q: '.+4'
in: 15
out:
- 19.0
- 19
v: '[1.5, 1.5]'
- q: '.+null'
in: {"a": 42}
Expand Down Expand Up @@ -601,8 +601,8 @@
- q: 'def f: . + 1; def g: def g: . + 100; f | g | f; (f | g), g'
in: 3.0
out:
- 106.0
- 105.0
- 106
- 105
v: '[1.5, 1.5]'
- q: 'def f: (1000,2000); f'
in: 123412345
Expand Down Expand Up @@ -650,17 +650,17 @@
- q: '[.[]|floor]'
in: [-1.1, 1.1, 1.9]
out:
- [-2, 1, 1]
- [-2.0, 1.0, 1.0]
v: '[1.5, 1.5]'
- q: '[.[]|sqrt]'
in: [4, 9]
out:
- [2, 3]
- [2.0, 3.0]
v: '[1.5, 1.5]'
- q: '(add / length) as $m | map((. - $m) as $d | $d * $d) | add / length | sqrt'
in: [2, 4, 4, 4, 5, 5, 7, 9]
out:
- 2
- 2.0
v: '[1.5, 1.5]'
- q: 'atan * 4 * 1000000|floor / 1000000'
in: 1
Expand Down Expand Up @@ -694,7 +694,7 @@
- q: 'def id(x):x; 2000 as $x | def f(x):1 as $x | id([$x, x, x]); def g(x): 100 as $x | f($x,$x+x); g($x)'
in: "more testing"
out:
- [1, 100, 2100.0, 100, 2100.0]
- [1, 100, 2100, 100, 2100]
v: '[1.5, 1.5]'
- q: 'def x(a;b): a as $a | b as $b | $a + $b; def y($a;$b): $a + $b; def check(a;b): [x(a;b)] == [y(a;b)]; check(.[];.[]*2)'
in: [1, 2, 3]
Expand All @@ -704,7 +704,7 @@
- q: '[[20,10][1,0] as $x | def f: (100,200) as $y | def g: [$x + $y, .]; . + $x | g; f[0] | [f][0][1] | f]'
in: 999999999
out:
- [[110.0, 130.0], [210.0, 130.0], [110.0, 230.0], [210.0, 230.0], [120.0, 160.0], [220.0, 160.0], [120.0, 260.0], [220.0, 260.0]]
- [[110, 130], [210, 130], [110, 230], [210, 230], [120, 160], [220, 160], [120, 260], [220, 260]]
v: '[1.5, 1.5]'
- q: 'def fac: if . == 1 then 1 else . * (. - 1 | fac) end; [.[] | fac]'
in: [1, 2, 3, 4]
Expand Down Expand Up @@ -844,7 +844,7 @@
- q: '.foo = .bar'
in: {"bar": 42}
out:
- {"foo": 42, "bar": 42}
- {"bar": 42, "foo": 42}
v: '[1.5, 1.5]'
- q: '.foo |= .+1'
in: {"foo": 42}
Expand Down
4 changes: 2 additions & 2 deletions jackson-jq/src/test/resources/tests/jq-1.6-manual.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -490,12 +490,12 @@
- q: 'floor'
in: 3.14159
out:
- 3
- 3.0
v: '[1.6, 1.6]'
- q: 'sqrt'
in: 9
out:
- 3
- 3.0
v: '[1.6, 1.6]'
- q: '.[] | tonumber'
in: [1, "1"]
Expand Down
22 changes: 11 additions & 11 deletions jackson-jq/src/test/resources/tests/jq-1.6.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@
- q: '1+1'
in: "wtasdf"
out:
- 2.0
- 2
v: '[1.6, 1.6]'
- q: '2-1'
in: null
Expand All @@ -504,12 +504,12 @@
- q: '1e+0+0.001e3'
in: "I wonder what this will be?"
out:
- 2.0
- 2
v: '[1.6, 1.6]'
- q: '.+4'
in: 15
out:
- 19.0
- 19
v: '[1.6, 1.6]'
- q: '.+null'
in: {"a": 42}
Expand Down Expand Up @@ -637,8 +637,8 @@
- q: 'def f: . + 1; def g: def g: . + 100; f | g | f; (f | g), g'
in: 3.0
out:
- 106.0
- 105.0
- 106
- 105
v: '[1.6, 1.6]'
- q: 'def f: (1000,2000); f'
in: 123412345
Expand Down Expand Up @@ -686,17 +686,17 @@
- q: '[.[]|floor]'
in: [-1.1, 1.1, 1.9]
out:
- [-2, 1, 1]
- [-2.0, 1.0, 1.0]
v: '[1.6, 1.6]'
- q: '[.[]|sqrt]'
in: [4, 9]
out:
- [2, 3]
- [2.0, 3.0]
v: '[1.6, 1.6]'
- q: '(add / length) as $m | map((. - $m) as $d | $d * $d) | add / length | sqrt'
in: [2, 4, 4, 4, 5, 5, 7, 9]
out:
- 2
- 2.0
v: '[1.6, 1.6]'
- q: 'atan * 4 * 1000000|floor / 1000000'
in: 1
Expand Down Expand Up @@ -730,7 +730,7 @@
- q: 'def id(x):x; 2000 as $x | def f(x):1 as $x | id([$x, x, x]); def g(x): 100 as $x | f($x,$x+x); g($x)'
in: "more testing"
out:
- [1, 100, 2100.0, 100, 2100.0]
- [1, 100, 2100, 100, 2100]
v: '[1.6, 1.6]'
- q: 'def x(a;b): a as $a | b as $b | $a + $b; def y($a;$b): $a + $b; def check(a;b): [x(a;b)] == [y(a;b)]; check(.[];.[]*2)'
in: [1, 2, 3]
Expand All @@ -740,7 +740,7 @@
- q: '[[20,10][1,0] as $x | def f: (100,200) as $y | def g: [$x + $y, .]; . + $x | g; f[0] | [f][0][1] | f]'
in: 999999999
out:
- [[110.0, 130.0], [210.0, 130.0], [110.0, 230.0], [210.0, 230.0], [120.0, 160.0], [220.0, 160.0], [120.0, 260.0], [220.0, 260.0]]
- [[110, 130], [210, 130], [110, 230], [210, 230], [120, 160], [220, 160], [120, 260], [220, 260]]
v: '[1.6, 1.6]'
- q: 'def fac: if . == 1 then 1 else . * (. - 1 | fac) end; [.[] | fac]'
in: [1, 2, 3, 4]
Expand Down Expand Up @@ -1047,7 +1047,7 @@
- q: '.foo = .bar'
in: {"bar": 42}
out:
- {"foo": 42, "bar": 42}
- {"bar": 42, "foo": 42}
v: '[1.6, 1.6]'
- q: '.foo |= .+1'
in: {"foo": 42}
Expand Down

0 comments on commit d58feee

Please sign in to comment.