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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class SimpleTest {
}

@Test
void testSimpleInsertQuery() throws Exception {
void testSimpleInsertQuery() {
assertThat(collection.countDocuments()).isZero();

// creates the database and collection in memory and insert the object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -514,25 +513,18 @@ private boolean checkExpressionMatch(Object value, Object expressionValue, Strin
}

static boolean matchTypes(Object value, Object expressionValue) {
if (Objects.equals(expressionValue, "number")) {
List<String> types = Stream.of(BsonType.INT, BsonType.LONG, BsonType.DOUBLE, BsonType.DECIMAL128)
.map(BsonType::getAlias)
.collect(Collectors.toList());
return matchTypes(value, types);
} else if (expressionValue instanceof String stringValue) {
return matchTypes(value, BsonType.forString(stringValue));
} else if (expressionValue instanceof Number numberValue) {
return matchTypes(value, BsonType.forNumber(numberValue));
} else if (expressionValue instanceof Collection<?> values) {
for (Object type : values) {
if (matchTypes(value, type)) {
return true;
}
return switch (expressionValue) {
case String stringValue when stringValue.equals("number") -> {
List<String> types = Stream.of(BsonType.INT, BsonType.LONG, BsonType.DOUBLE, BsonType.DECIMAL128)
.map(BsonType::getAlias)
.collect(Collectors.toList());
yield matchTypes(value, types);
}
return false;
} else {
throw new MongoServerError(ErrorCode.TypeMismatch, "type must be represented as a number or a string");
}
case String stringValue -> matchTypes(value, BsonType.forString(stringValue));
case Number numberValue -> matchTypes(value, BsonType.forNumber(numberValue));
case Collection<?> values -> values.stream().anyMatch(type -> matchTypes(value, type));
case null, default -> throw new MongoServerError(ErrorCode.TypeMismatch, "type must be represented as a number or a string");
};
}

private static boolean matchTypes(Object value, BsonType type) {
Expand Down
58 changes: 26 additions & 32 deletions core/src/main/java/de/bwaldvogel/mongo/backend/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,48 +89,42 @@ public static String getCollectionNameFromFullName(String fullName) {
}

public static boolean isTrue(Object value) {
if (Missing.isNullOrMissing(value)) {
return false;
}

if (value instanceof Boolean bool) {
return bool.booleanValue();
}

if (value instanceof Number number) {
return number.doubleValue() != 0.0;
}

return true;
return switch (value) {
case null -> false;
case Missing ignored -> false;
case Boolean bool -> bool.booleanValue();
case Number number -> number.doubleValue() != 0.0;
default -> true;
};
}

static Object normalizeValue(Object value) {
if (Missing.isNullOrMissing(value)) {
return null;
}
if (value instanceof Long && cannotBeRepresentedAsDouble((Long) value)) {
return value;
} else if (value instanceof Number number) {
double doubleValue = number.doubleValue();
if (doubleValue == -0.0) {
doubleValue = 0.0;
return switch (value) {
case Long longValue when cannotBeRepresentedAsDouble(longValue) -> longValue;
case Number number -> {
double doubleValue = number.doubleValue();
if (doubleValue == -0.0) {
doubleValue = 0.0;
}
yield Double.valueOf(doubleValue);
}
return Double.valueOf(doubleValue);
} else if (value instanceof Map) {
@SuppressWarnings("unchecked")
Map<String, Object> map = (Map<String, Object>) value;
Document result = new Document();
for (Entry<String, Object> entry : map.entrySet()) {
result.put(entry.getKey(), normalizeValue(entry.getValue()));
case Map<?, ?> mapValue -> {
@SuppressWarnings("unchecked")
Map<String, Object> map = (Map<String, Object>) mapValue;
Document result = new Document();
for (Entry<String, Object> entry : map.entrySet()) {
result.put(entry.getKey(), normalizeValue(entry.getValue()));
}
yield result;
}
return result;
} else if (value instanceof Collection<?> collection) {
return collection.stream()
case Collection<?> collection -> collection.stream()
.map(Utils::normalizeValue)
.collect(Collectors.toList());
} else {
return value;
}
default -> value;
};
}

private static boolean cannotBeRepresentedAsDouble(Long value) {
Expand Down
Loading
Loading