Search in sources :

Example 11 with PropertyModel

use of dev.morphia.mapping.codec.pojo.PropertyModel in project morphia by mongodb.

the class PathTarget method resolve.

private void resolve() {
    context = this.root;
    position = 0;
    PropertyModel property = null;
    while (hasNext()) {
        String segment = next();
        // array operator
        if ("$".equals(segment) || (segment.startsWith("$[") && segment.endsWith("]")) || segment.matches("[0-9]+")) {
            if (!hasNext()) {
                break;
            }
            segment = next();
        }
        property = resolveProperty(segment);
        if (property != null) {
            if (hasNext() && property.isReference()) {
                failValidation(segment);
            }
            translate(property.getMappedName());
            if (property.isMap() && hasNext()) {
                // consume the map key segment
                next();
            }
        } else {
            if (validateNames) {
                failValidation(segment);
            }
        }
    }
    target = property;
    resolved = true;
}
Also used : PropertyModel(dev.morphia.mapping.codec.pojo.PropertyModel)

Example 12 with PropertyModel

use of dev.morphia.mapping.codec.pojo.PropertyModel in project morphia by mongodb.

the class TestPropertyModel method basicFieldMapping.

@Test
public void basicFieldMapping() {
    final PropertyModel property = getMappedField("name");
    Assert.assertTrue(property.isScalarValue());
    Assert.assertSame(property.getType(), String.class);
    Assert.assertEquals(property.getName(), "name");
    Assert.assertEquals(property.getMappedName(), "n");
}
Also used : PropertyModel(dev.morphia.mapping.codec.pojo.PropertyModel) Test(org.testng.annotations.Test)

Example 13 with PropertyModel

use of dev.morphia.mapping.codec.pojo.PropertyModel in project morphia by mongodb.

the class TestPropertyModel method collectionFieldMapping.

@Test
public void collectionFieldMapping() {
    final PropertyModel property = getMappedField("listOfString");
    Assert.assertFalse(property.isScalarValue());
    Assert.assertTrue(property.isMultipleValues());
    Assert.assertFalse(property.isArray());
    Assert.assertSame(property.getType(), List.class);
    Assert.assertSame(property.getNormalizedType(), String.class);
    Assert.assertEquals(property.getName(), "listOfString");
    Assert.assertEquals(property.getMappedName(), "listOfString");
}
Also used : PropertyModel(dev.morphia.mapping.codec.pojo.PropertyModel) Test(org.testng.annotations.Test)

Example 14 with PropertyModel

use of dev.morphia.mapping.codec.pojo.PropertyModel in project morphia by mongodb.

the class TestPropertyModel method nestedCollectionsMapping.

@Test
public void nestedCollectionsMapping() {
    final PropertyModel property = getMappedField("listOfListOfString");
    Assert.assertFalse(property.isScalarValue());
    Assert.assertTrue(property.isMultipleValues());
    Assert.assertFalse(property.isArray());
    Assert.assertSame(property.getType(), List.class);
    final TypeData<?> typeData = property.getTypeData();
    Assert.assertSame(typeData.getType(), List.class);
    Assert.assertEquals(typeData.getTypeParameters().get(0).getTypeParameters().get(0).getType(), String.class);
    Assert.assertEquals(property.getName(), "listOfListOfString");
    Assert.assertEquals(property.getMappedName(), "listOfListOfString");
    final List<List<String>> list = new ArrayList<>();
    list.add(dbList("a", "b", "c"));
    list.add(dbList("d", "e", "f"));
    TestEntity testEntity = new TestEntity();
    testEntity.listOfListOfString = list;
    getDs().save(testEntity);
    Assert.assertEquals(list, getDs().find(TestEntity.class).filter(eq("_id", testEntity.id)).first().listOfListOfString);
}
Also used : PropertyModel(dev.morphia.mapping.codec.pojo.PropertyModel) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.testng.annotations.Test)

Example 15 with PropertyModel

use of dev.morphia.mapping.codec.pojo.PropertyModel in project morphia by mongodb.

the class TestPropertyModel method idFieldMapping.

@Test
public void idFieldMapping() {
    final PropertyModel property = getMappedField("id");
    Assert.assertTrue(property.isScalarValue());
    Assert.assertSame(property.getType(), ObjectId.class);
    Assert.assertEquals(property.getName(), "id");
    Assert.assertEquals(property.getMappedName(), "_id");
}
Also used : PropertyModel(dev.morphia.mapping.codec.pojo.PropertyModel) Test(org.testng.annotations.Test)

Aggregations

PropertyModel (dev.morphia.mapping.codec.pojo.PropertyModel)22 EntityModel (dev.morphia.mapping.codec.pojo.EntityModel)10 Test (org.testng.annotations.Test)10 Document (org.bson.Document)4 Nullable (com.mongodb.lang.Nullable)3 Datastore (dev.morphia.Datastore)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 PathTarget (dev.morphia.internal.PathTarget)2 MappingException (dev.morphia.mapping.MappingException)2 PropertyHandler (dev.morphia.mapping.codec.pojo.PropertyHandler)2 DocumentWriter (dev.morphia.mapping.codec.writer.DocumentWriter)2 OperationTarget (dev.morphia.query.OperationTarget)2 MethodMappedSpecializedEntity (dev.morphia.test.models.methods.MethodMappedSpecializedEntity)2 Key (dev.morphia.Key)1 Morphia.createDatastore (dev.morphia.Morphia.createDatastore)1 Mapper (dev.morphia.mapping.Mapper)1 NotMappableException (dev.morphia.mapping.NotMappableException)1 TestBase (dev.morphia.test.TestBase)1 CityPopulation (dev.morphia.test.models.CityPopulation)1