Skip to content
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

feat(datastore): Return nested data for belongsTo associations in datastore #1390

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,8 @@ public void queryModelsInSerializedForm() throws DataStoreException {
serializedBlogData.put("id", blog.getId());
serializedBlogData.put("name", blog.getName());
serializedBlogData.put("owner", SerializedModel.builder()
.serializedData(Collections.singletonMap("id", blogOwner.getId()))
.modelSchema(null)
.serializedData(serializedBlogOwnerData)
.modelSchema(blogOwnerSchema)
.build()
);
assertEquals(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.amplifyframework.core.Consumer;
import com.amplifyframework.core.async.Cancelable;
import com.amplifyframework.core.model.Model;
import com.amplifyframework.core.model.ModelAssociation;
import com.amplifyframework.core.model.ModelField;
import com.amplifyframework.core.model.ModelProvider;
import com.amplifyframework.core.model.ModelSchema;
Expand Down Expand Up @@ -416,7 +417,6 @@ public <T extends Model> void query(
/**
* {@inheritDoc}
*/
@SuppressWarnings("unchecked")
@Override
public void query(
@NonNull String modelName,
Expand Down Expand Up @@ -447,26 +447,8 @@ public void query(

if (cursor.moveToFirst()) {
do {
final Map<String, Object> serializedData = new HashMap<>();
for (Map.Entry<String, Object> entry : converter.buildMapForModel(cursor).entrySet()) {
ModelField field = modelSchema.getFields().get(entry.getKey());
if (field == null || entry.getValue() == null) {
// Skip it
} else if (field.isModel()) {
String id = (String) ((Map<String, Object>) entry.getValue()).get("id");
serializedData.put(entry.getKey(), SerializedModel.builder()
.serializedData(Collections.singletonMap("id", id))
.modelSchema(null)
.build()
);
} else {
serializedData.put(entry.getKey(), entry.getValue());
}
}
SerializedModel model = SerializedModel.builder()
.serializedData(serializedData)
.modelSchema(modelSchema)
.build();
final Map<String, Object> data = converter.buildMapForModel(cursor);
final SerializedModel model = createSerializedModel(modelSchema, data);
models.add(model);
} while (cursor.moveToNext());
}
Expand Down Expand Up @@ -831,4 +813,36 @@ private Completable updateModels() {
return PersistentModelVersion.saveToLocalStorage(this, persistentModelVersion);
}).ignoreElement();
}

/**
* recursively creates nested SerializedModels from raw data.
*/
private SerializedModel createSerializedModel(ModelSchema modelSchema, Map<String, Object> data) {
final Map<String, Object> serializedData = new HashMap<>();
for (Map.Entry<String, Object> entry : data.entrySet()) {
ModelField field = modelSchema.getFields().get(entry.getKey());
if (field != null && entry.getValue() != null) {
if (field.isModel()) {
ModelAssociation association = modelSchema.getAssociations().get(entry.getKey());
if (association != null) {
String associatedType = association.getAssociatedType();
final ModelSchema nestedModelSchema = modelSchemaRegistry.getModelSchemaForModelClass(
associatedType
);
@SuppressWarnings("unchecked")
SerializedModel model = createSerializedModel(
nestedModelSchema, (Map<String, Object>) entry.getValue()
);
serializedData.put(entry.getKey(), model);
}
} else {
serializedData.put(entry.getKey(), entry.getValue());
}
}
}
return SerializedModel.builder()
.serializedData(serializedData)
.modelSchema(modelSchema)
.build();
}
}