Skip to content

DATACOUCH-27: Mapping @Id field with MappingCouchbaseConverter #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 10, 2013
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
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ target/

.classpath
.project
.settings/*
.settings

*.iml
.idea/*
.idea
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public <R> R read(Class<R> clazz, CouchbaseDocument doc) {
protected <R> R read(TypeInformation<R> type, CouchbaseDocument doc) {
return read(type, doc, null);
}

protected <R> R read(TypeInformation<R> type, final CouchbaseDocument source, Object parent) {

if (source == null) {
Expand Down Expand Up @@ -118,13 +118,17 @@ protected <R extends Object> R read(final CouchbasePersistentEntity<R> entity, f

entity.doWithProperties(new PropertyHandler<CouchbasePersistentProperty>() {
public void doWithPersistentProperty(final CouchbasePersistentProperty prop) {
if (!source.containsKey(prop.getFieldName()) || entity.isConstructorArgument(prop)) {
if (!doesPropertyExistInSource(prop) || entity.isConstructorArgument(prop)) {
return;
}

Object obj = prop.isIdProperty() ? source.getId() : getValueInternal(prop, source, evaluator, result);
wrapper.setProperty(prop, obj, useFieldAccessOnly);
}

private boolean doesPropertyExistInSource(CouchbasePersistentProperty property) {
return property.isIdProperty() || source.containsKey(property.getFieldName());
}
});

entity.doWithAssociations(new AssociationHandler<CouchbasePersistentProperty>() {
Expand Down Expand Up @@ -201,7 +205,6 @@ private Object getPotentiallyConvertedSimpleRead(Object value, Class<?> target)
return target.isAssignableFrom(value.getClass()) ? value : conversionService.convert(value, target);
}


@Override
public void write(final Object source, final CouchbaseDocument target) {
if (source == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,8 @@ public class Beer {

String description;

public String getId() {
return id;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ public void needsIDOnEntity() {
new CouchbaseDocument());
}


@Test
public void writesString() {
CouchbaseDocument converted = new CouchbaseDocument();
Expand All @@ -94,7 +93,6 @@ public void readsString() {
assertEquals("foobar", converted.attr0);
}


@Test
public void writesNumber() {
CouchbaseDocument converted = new CouchbaseDocument();
Expand All @@ -119,7 +117,6 @@ public void readsNumber() {
assertEquals(42, converted.attr0);
}


@Test
public void writesBoolean() {
CouchbaseDocument converted = new CouchbaseDocument();
Expand Down Expand Up @@ -176,6 +173,15 @@ public void readsMixedSimpleTypes() {
assertTrue(converted.attr3);
}

@Test
public void readsID() {
CouchbaseDocument document = new CouchbaseDocument("001");

BasicCouchbasePersistentPropertyTests.Beer beer = converter.read(BasicCouchbasePersistentPropertyTests.Beer.class,
document);

assertEquals("001", beer.getId());
}

@Test
public void writesUninitializedValues() {
Expand Down Expand Up @@ -336,7 +342,6 @@ public void writesAndReadsSetAndNestedSet() {
assertEquals(attr2, readConverted.attr2);
}


@Test
public void writesAndReadsValueClass() {
CouchbaseDocument converted = new CouchbaseDocument();
Expand Down