Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,126 @@
import java.util.stream.Collectors;
import org.eclipse.microprofile.openapi.annotations.enums.SchemaType;
import org.eclipse.microprofile.openapi.annotations.media.Schema;
import org.eclipse.microprofile.openapi.annotations.media.SchemaProperty;

/**
* Value type read from incoming JSON and used for resolving into actual {@link UpdateOperation}s
* with validation and additional context.
*/
@JsonDeserialize(using = UpdateClauseDeserializer.class)
@Schema(
name = "UpdateClause",
type = SchemaType.OBJECT,
implementation = Object.class,
examples =
"""
additionalProperties = Schema.False.class,
description =
"The Update Clause to use when updating Documents in a Collection or Rows in a Table.",
properties = {
@SchemaProperty(
name = UpdateOperator.Names.SET,
type = SchemaType.OBJECT,
description = "Sets fields to the provided JSON value."),
@SchemaProperty(
name = UpdateOperator.Names.SET_ON_INSERT,
type = SchemaType.OBJECT,
description =
"Sets fields to the provided JSON value, only when an upsert inserts the document."),
@SchemaProperty(
name = UpdateOperator.Names.UNSET,
type = SchemaType.OBJECT,
description = "Removes fields from the Document, or sets Table columns to null."),
@SchemaProperty(
name = UpdateOperator.Names.INC,
type = SchemaType.OBJECT,
description =
"Updates numeric fields by incrementing them by the provided value (use negative values to decrement)."),
@SchemaProperty(
name = UpdateOperator.Names.MUL,
type = SchemaType.OBJECT,
description = "Updates numeric fields by multiplying them by the provided value."),
@SchemaProperty(
name = UpdateOperator.Names.MIN,
type = SchemaType.OBJECT,
description =
"Updates fields to the smaller of the current value or the provided value."),
@SchemaProperty(
name = UpdateOperator.Names.MAX,
type = SchemaType.OBJECT,
description = "Updates fields to the larger of the current value or the provided value."),
@SchemaProperty(
name = UpdateOperator.Names.RENAME,
type = SchemaType.OBJECT,
description = "Renames fields to the provided names."),
@SchemaProperty(
name = UpdateOperator.Names.CURRENT_DATE,
type = SchemaType.OBJECT,
description =
"Sets fields to the current date. The value must be true or {\"$type\": \"date\"}."),
@SchemaProperty(
name = UpdateOperator.Names.POP,
type = SchemaType.OBJECT,
description =
"Updates array fields to remove the first (-1) or the last (1) element of the array."),
@SchemaProperty(
name = UpdateOperator.Names.PUSH,
type = SchemaType.OBJECT,
description =
"Updates array fields in a Document, or list, set, and map columns in a Table, by appending values. Use $each for multiple values. Only collections accept $position."),
@SchemaProperty(
name = UpdateOperator.Names.ADD_TO_SET,
type = SchemaType.OBJECT,
description =
"Updates array fields in a Document by appending values that are not already present. Use $each for multiple values."),
@SchemaProperty(
name = UpdateOperator.Names.PULL_ALL,
type = SchemaType.OBJECT,
description =
"Updates list and set columns in a Table by removing the provided values, and map columns by removing the provided keys.")
},
examples = {
"""
{
"$set" : {"location": "New York"},
"$unset" : {"new_data": 1}
"$set": {"location": "New York"},
"$unset": {"obsolete": 1}
}
""")
public record UpdateClause(EnumMap<UpdateOperator, ObjectNode> updateOperationDefs) {
""",
"""
{
"$inc": {"score": 0.25},
"$mul": {"price": 1.5},
"$min": {"lowest": 2.5},
"$max": {"highest": 9.5}
}
""",
"""
{
"$push": {
"items": {"$each": [1, 2], "$position": 0}
},
"$addToSet": {
"tags": {"$each": ["a", "b"]}
},
"$pop": {"history": -1},
"$rename": {"oldName": "newName"}
}
""",
"""
{
"$currentDate": {"updated": true},
"$setOnInsert": {"created": {"$date": 123456}},
"$set": {"optional": null}
}
""",
"""
{
"$pullAll": {"tags": ["a", "b"]}
}
""",
"{}"
})
// Hide parsed operations from schemas without changing Jackson serialization.
public record UpdateClause(
@Schema(hidden = true) EnumMap<UpdateOperator, ObjectNode> updateOperationDefs) {
/**
* Method that will validate update operation definitions of the clause and construct an ordered
* set of {@link UpdateOperation}s.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,92 +7,108 @@

/** List of update operator that's supported in update. */
public enum UpdateOperator {
ADD_TO_SET("$addToSet") {
ADD_TO_SET(Names.ADD_TO_SET) {
Comment thread
amorton marked this conversation as resolved.
Resolved
@Override
public UpdateOperation resolveOperation(ObjectNode arguments) {
public UpdateOperation<?> resolveOperation(ObjectNode arguments) {
return AddToSetOperation.construct(arguments);
}
},

CURRENT_DATE("$currentDate") {
CURRENT_DATE(Names.CURRENT_DATE) {
@Override
public UpdateOperation resolveOperation(ObjectNode arguments) {
public UpdateOperation<?> resolveOperation(ObjectNode arguments) {
return CurrentDateOperation.construct(arguments);
}
},

INC("$inc") {
INC(Names.INC) {
@Override
public UpdateOperation resolveOperation(ObjectNode arguments) {
public UpdateOperation<?> resolveOperation(ObjectNode arguments) {
return IncOperation.construct(arguments);
}
},

MAX("$max") {
MAX(Names.MAX) {
@Override
public UpdateOperation resolveOperation(ObjectNode arguments) {
public UpdateOperation<?> resolveOperation(ObjectNode arguments) {
return MinMaxOperation.constructMax(arguments);
}
},

MIN("$min") {
MIN(Names.MIN) {
@Override
public UpdateOperation resolveOperation(ObjectNode arguments) {
public UpdateOperation<?> resolveOperation(ObjectNode arguments) {
return MinMaxOperation.constructMin(arguments);
}
},

MUL("$mul") {
MUL(Names.MUL) {
@Override
public UpdateOperation resolveOperation(ObjectNode arguments) {
public UpdateOperation<?> resolveOperation(ObjectNode arguments) {
return MulOperation.construct(arguments);
}
},

POP("$pop") {
POP(Names.POP) {
@Override
public UpdateOperation resolveOperation(ObjectNode arguments) {
public UpdateOperation<?> resolveOperation(ObjectNode arguments) {
return PopOperation.construct(arguments);
}
},

PUSH("$push") {
PUSH(Names.PUSH) {
@Override
public UpdateOperation resolveOperation(ObjectNode arguments) {
public UpdateOperation<?> resolveOperation(ObjectNode arguments) {
return PushOperation.construct(arguments);
}
},

RENAME("$rename") {
RENAME(Names.RENAME) {
@Override
public UpdateOperation resolveOperation(ObjectNode arguments) {
public UpdateOperation<?> resolveOperation(ObjectNode arguments) {
return RenameOperation.construct(arguments);
}
},

SET("$set") {
SET(Names.SET) {
@Override
public UpdateOperation resolveOperation(ObjectNode arguments) {
public UpdateOperation<?> resolveOperation(ObjectNode arguments) {
return SetOperation.constructSet(arguments);
}
},
SET_ON_INSERT("$setOnInsert") {
SET_ON_INSERT(Names.SET_ON_INSERT) {
@Override
public UpdateOperation resolveOperation(ObjectNode arguments) {
public UpdateOperation<?> resolveOperation(ObjectNode arguments) {
return SetOperation.constructSetOnInsert(arguments);
}
},

UNSET("$unset") {
UNSET(Names.UNSET) {
@Override
public UpdateOperation resolveOperation(ObjectNode arguments) {
public UpdateOperation<?> resolveOperation(ObjectNode arguments) {
return UnsetOperation.construct(arguments);
}
},

// Then operators that we recognize but do not (yet) support

PULL_ALL("$pullAll");
PULL_ALL(Names.PULL_ALL);

public interface Names {
Comment thread
erichare marked this conversation as resolved.
Resolved
String ADD_TO_SET = "$addToSet";
String CURRENT_DATE = "$currentDate";
String INC = "$inc";
String MAX = "$max";
String MIN = "$min";
String MUL = "$mul";
String POP = "$pop";
String PUSH = "$push";
String RENAME = "$rename";
String SET = "$set";
String SET_ON_INSERT = "$setOnInsert";
String UNSET = "$unset";
String PULL_ALL = "$pullAll";
}

private String apiName;

Expand All @@ -104,7 +120,7 @@ public String apiName() {
return apiName;
}

public UpdateOperation resolveOperation(ObjectNode arguments) {
public UpdateOperation<?> resolveOperation(ObjectNode arguments) {
throw UpdateException.Code.UNSUPPORTED_UPDATE_OPERATION.get(
Map.of("errorMessage", "Unsupported update operator '%s'".formatted(apiName)));
}
Expand Down
Loading