use of org.bson.BsonDocumentReader 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.bson.BsonDocumentReader 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");
}
use of org.bson.BsonDocumentReader in project immutables by immutables.
the class Jsons method readerAt.
/**
* Creates reader for position at {@code value}
*/
static JsonReader readerAt(BsonValue value) throws IOException {
BsonDocument doc = new BsonDocument("value", value);
BsonReader reader = new BsonReader(new BsonDocumentReader(doc));
// advance AFTER value token
reader.beginObject();
check(reader.peek()).is(JsonToken.NAME);
check(reader.nextName()).is("value");
return reader;
}
Aggregations