Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,19 @@ private ArrayNode getValidEachRHS(
}

// only allowed to have $each in the json object.
if (objectNode.size() != 1 || !(eachOpRHS instanceof ArrayNode arrayNode)) {
if (objectNode.size() != 1) {
throw UpdateException.Code.INVALID_PUSH_OPERATOR_USAGE.get(
errVars(
tableSchemaObject,
map -> {
map.put(
"reason",
"extraneous '%s' field encountered"
.formatted(firstFieldOtherThanEach(objectNode)));
}));
}

if (!(eachOpRHS instanceof ArrayNode arrayNode)) {
throw UpdateException.Code.INVALID_PUSH_OPERATOR_USAGE.get(
errVars(
tableSchemaObject,
Expand All @@ -241,6 +253,15 @@ private ArrayNode getValidEachRHS(
return arrayNode;
}

private String firstFieldOtherThanEach(ObjectNode objectNode) {
for (var field : objectNode.properties()) {
if (!UpdateOperatorModifier.EACH.apiName().equals(field.getKey())) {
return field.getKey();
}
}
throw new IllegalStateException("Expected a field other than $each");
}

/**
* The map $each array can be an array of tuples or an array of entries:
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,14 @@ static Stream<Arguments> pushOperations() {
Map.of("key1", "value1"),
null,
null),
Arguments.of(
"""
{"%s": {"$each": [["key1", "value1"]], "$position": 1}}
"""
.formatted(names().CQL_MAP_COLUMN),
null,
UpdateException.Code.INVALID_PUSH_OPERATOR_USAGE,
"extraneous '$position' field encountered"),
Arguments.of(
"""
{"%s": {"$each": ["abc"]}}
Expand All @@ -176,6 +184,14 @@ static Stream<Arguments> pushOperations() {
List.of("abc"),
null,
null),
Arguments.of(
"""
{"%s": {"$each": ["abc"], "$position": 1}}
"""
.formatted(names().CQL_LIST_COLUMN),
null,
UpdateException.Code.INVALID_PUSH_OPERATOR_USAGE,
"extraneous '$position' field encountered"),
Arguments.of(
"""
{"%s": {"$each": ["abc"]}}
Expand Down
Loading