use of org.immutables.criteria.backend.ProjectedTuple in project immutables by immutables.
the class TupleCodecProviderTest method optionalAttribute_nickname.
/**
* Projection of an optional attribute
*/
@Test
@SuppressWarnings("unchecked")
public void optionalAttribute_nickname() {
Query query = Query.of(Person.class).addProjections(Matchers.toExpression(PersonCriteria.person.nickName));
Path idPath = Visitors.toPath(KeyExtractor.defaultFactory().create(Person.class).metadata().keys().get(0));
TupleCodecProvider provider = new TupleCodecProvider(query, new MongoPathNaming(idPath, PathNaming.defaultNaming()).toExpression());
Codec<ProjectedTuple> codec = provider.get(ProjectedTuple.class, registry);
ProjectedTuple tuple1 = codec.decode(new BsonDocumentReader(new BsonDocument("nickName", new BsonString("aaa"))), DecoderContext.builder().build());
check(tuple1.values()).hasSize(1);
check((Optional<String>) tuple1.values().get(0)).is(Optional.of("aaa"));
ProjectedTuple tuple2 = codec.decode(new BsonDocumentReader(new BsonDocument()), DecoderContext.builder().build());
check(tuple2.values()).hasSize(1);
check((Optional<String>) tuple2.values().get(0)).is(Optional.empty());
}
use of org.immutables.criteria.backend.ProjectedTuple in project immutables by immutables.
the class AggregateQueryBuilder method processResult.
List<ProjectedTuple> processResult(Json.Result result) {
final List<ProjectedTuple> tuples = new ArrayList<>();
if (result.aggregations() != null) {
Converter<String, Expression> converter = naming.asConverter().reverse();
// collect values
Json.visitValueNodes(result.aggregations(), m -> {
Map<Expression, Object> values = Maps.newHashMapWithExpectedSize(query.projections().size());
for (String field : m.keySet()) {
Expression expression = converter.convert(field);
Object value = m.get(field);
if (value == null) {
// otherwise jackson returns null even for optionals
value = NullNode.getInstance();
} else if (value instanceof Number && (expression.returnType() == LocalDate.class || expression.returnType() == LocalDateTime.class)) {
// hack/work-around because JavaTimeModule doesn't handle epoch millis for LocalDate and LocalDateTime
// and elastic always returns epoch millis
// this ideally should be handled directly by Deserializer
Instant instant = Instant.ofEpochMilli(((Number) value).longValue());
value = nodeFactory.textNode(instant.toString());
}
values.put(expression, mapper.convertValue(value, mapper.getTypeFactory().constructType(expression.returnType())));
}
List<Object> projections = query.projections().stream().map(values::get).collect(Collectors.toList());
tuples.add(ProjectedTuple.of(query.projections(), projections));
});
}
// elastic exposes total number of documents matching a query in "/hits/total" path
// this can be used for simple "select count(*) from table"
final long total = result.searchHits().total().value();
return tuples;
}
use of org.immutables.criteria.backend.ProjectedTuple in project immutables by immutables.
the class TupleCodecProviderTest method localDate.
@Test
void localDate() {
LocalDateHolderCriteria criteria = LocalDateHolderCriteria.localDateHolder;
Query query = Query.of(TypeHolder.LocalDateHolder.class).addProjections(Matchers.toExpression(criteria.value), Matchers.toExpression(criteria.nullable), Matchers.toExpression(criteria.optional));
Path idPath = Visitors.toPath(KeyExtractor.defaultFactory().create(TypeHolder.LocalDateHolder.class).metadata().keys().get(0));
TupleCodecProvider provider = new TupleCodecProvider(query, new MongoPathNaming(idPath, PathNaming.defaultNaming()).toExpression());
Codec<ProjectedTuple> codec = provider.get(ProjectedTuple.class, registry);
LocalDate now = LocalDate.now();
final long millisEpoch = now.atStartOfDay().toInstant(ZoneOffset.UTC).toEpochMilli();
BsonDocument doc = new BsonDocument().append("id", new BsonString("id1")).append("value", new BsonDateTime(millisEpoch)).append("nullable", BsonNull.VALUE).append("optional", BsonNull.VALUE).append("array", new BsonArray()).append("list", new BsonArray());
ProjectedTuple tuple = codec.decode(new BsonDocumentReader(doc), DecoderContext.builder().build());
check(tuple.get(Matchers.toExpression(criteria.value))).is(now);
check(tuple.get(Matchers.toExpression(criteria.nullable))).isNull();
check(tuple.get(Matchers.toExpression(criteria.optional))).is(Optional.empty());
}
use of org.immutables.criteria.backend.ProjectedTuple in project immutables by immutables.
the class TupleCodecProviderTest method age.
@Test
public void age() {
Query query = Query.of(Person.class).addProjections(Matchers.toExpression(PersonCriteria.person.age));
Path idPath = Visitors.toPath(KeyExtractor.defaultFactory().create(Person.class).metadata().keys().get(0));
TupleCodecProvider provider = new TupleCodecProvider(query, new MongoPathNaming(idPath, PathNaming.defaultNaming()).toExpression());
Codec<ProjectedTuple> codec = provider.get(ProjectedTuple.class, registry);
ProjectedTuple tuple = codec.decode(new BsonDocumentReader(new BsonDocument("age", new BsonInt32(10))), DecoderContext.builder().build());
check(tuple.values()).hasSize(1);
check(tuple.values().get(0)).asString().is("10");
}
Aggregations