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

[ES-449270] Fix duplicate key error in object comprehension #156

Merged
merged 1 commit into from May 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions sjsonnet/src/sjsonnet/Evaluator.scala
Expand Up @@ -589,12 +589,16 @@ class Evaluator(resolver: CachedResolver,

visitExpr(e.key)(s) match {
case Val.Str(_, k) =>
val prev_length = builder.size()
builder.put(k, new Val.Obj.Member(false, Visibility.Normal) {
def invoke(self: Val.Obj, sup: Val.Obj, fs: FileScope, ev: EvalScope): Val =
visitExpr(e.value)(
s.extend(newBindings, self, null)
)
})
if (prev_length == builder.size()) {
Error.fail(s"Duplicate key ${k} in evaluated object comprehension.", e.pos);
}
case Val.Null(_) => // do nothing
}
}
Expand Down
13 changes: 13 additions & 0 deletions sjsonnet/test/src/sjsonnet/EvaluatorTests.scala
Expand Up @@ -338,6 +338,19 @@ object EvaluatorTests extends TestSuite{
test("givenNoDuplicateFieldsInListComprehension2_expectSuccess") {
eval("""{ ["bar_" + x]: x for x in [5,12]}""") ==> ujson.Obj("bar_5" -> 5, "bar_12" -> 12)
}
test("givenDuplicateFieldsInListComprehension_expectFailure") {
evalErr("""{ [x]: x for x in ["A", "A"]}""") ==>
"""sjsonnet.Error: Duplicate key A in evaluated object comprehension.
|at .(:1:3)""".stripMargin
}
test("givenDuplicateFieldsInIndirectListComprehension_expectFailure") {
evalErr(
"""local y = { a: "A" };
|local z = { a: "A" };
|{ [x.a]: x for x in [y, z]}""".stripMargin) ==>
"""sjsonnet.Error: Duplicate key A in evaluated object comprehension.
|at .(:3:3)""".stripMargin
}
test("functionEqualsNull") {
eval("""local f(x)=null; f == null""") ==> ujson.False
eval("""local f=null; f == null""") ==> ujson.True
Expand Down