Search in sources :

Example 41 with BsonDocumentReader

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());
}
Also used : Path(org.immutables.criteria.expression.Path) Query(org.immutables.criteria.expression.Query) BsonDocumentReader(org.bson.BsonDocumentReader) LocalDate(java.time.LocalDate) TypeHolder(org.immutables.criteria.typemodel.TypeHolder) LocalDateHolderCriteria(org.immutables.criteria.typemodel.LocalDateHolderCriteria) BsonDocument(org.bson.BsonDocument) BsonDateTime(org.bson.BsonDateTime) BsonString(org.bson.BsonString) BsonArray(org.bson.BsonArray) ProjectedTuple(org.immutables.criteria.backend.ProjectedTuple) Test(org.junit.jupiter.api.Test)

Example 42 with BsonDocumentReader

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");
}
Also used : Path(org.immutables.criteria.expression.Path) BsonInt32(org.bson.BsonInt32) Query(org.immutables.criteria.expression.Query) BsonDocument(org.bson.BsonDocument) BsonDocumentReader(org.bson.BsonDocumentReader) ProjectedTuple(org.immutables.criteria.backend.ProjectedTuple) Person(org.immutables.criteria.personmodel.Person) Test(org.junit.jupiter.api.Test)

Example 43 with BsonDocumentReader

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;
}
Also used : BsonDocument(org.bson.BsonDocument) BsonReader(org.immutables.mongo.bson4gson.BsonReader) BsonDocumentReader(org.bson.BsonDocumentReader)

Aggregations

BsonDocumentReader (org.bson.BsonDocumentReader)43 BsonDocument (org.bson.BsonDocument)42 Test (org.junit.Test)30 SingleMapReaderImpl (org.apache.drill.exec.vector.complex.impl.SingleMapReaderImpl)29 BaseTest (org.apache.drill.test.BaseTest)15 BsonString (org.bson.BsonString)9 FieldReader (org.apache.drill.exec.vector.complex.reader.FieldReader)6 BsonDocumentWriter (org.bson.BsonDocumentWriter)6 BsonWriter (org.bson.BsonWriter)6 Test (org.junit.jupiter.api.Test)5 ArrayList (java.util.ArrayList)3 BsonBoolean (org.bson.BsonBoolean)3 BsonDateTime (org.bson.BsonDateTime)3 BsonDouble (org.bson.BsonDouble)3 BsonInt32 (org.bson.BsonInt32)3 BsonInt64 (org.bson.BsonInt64)3 BsonNull (org.bson.BsonNull)3 ProjectedTuple (org.immutables.criteria.backend.ProjectedTuple)3 Path (org.immutables.criteria.expression.Path)3 Query (org.immutables.criteria.expression.Query)3