Search in sources :

Example 6 with CustomTypeSchema

use of com.amplifyframework.core.model.CustomTypeSchema in project amplify-android by aws-amplify.

the class SQLiteStorageAdapter method createSerializedModel.

/**
 * 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 = schemaRegistry.getModelSchemaForModelClass(associatedType);
                    @SuppressWarnings("unchecked") SerializedModel model = createSerializedModel(nestedModelSchema, (Map<String, Object>) entry.getValue());
                    serializedData.put(entry.getKey(), model);
                }
            } else if (field.isCustomType()) {
                if (field.isArray()) {
                    @SuppressWarnings("unchecked") List<Map<String, Object>> listItems = (List<Map<String, Object>>) entry.getValue();
                    List<SerializedCustomType> listOfCustomType = getValueOfListCustomTypeField(field.getTargetType(), listItems);
                    serializedData.put(entry.getKey(), listOfCustomType);
                } else {
                    final CustomTypeSchema nestedCustomTypeSchema = schemaRegistry.getCustomTypeSchemaForCustomTypeClass(field.getTargetType());
                    @SuppressWarnings("unchecked") SerializedCustomType customType = createSerializedCustomType(nestedCustomTypeSchema, (Map<String, Object>) entry.getValue());
                    serializedData.put(entry.getKey(), customType);
                }
            } else {
                serializedData.put(entry.getKey(), entry.getValue());
            }
        }
    }
    return SerializedModel.builder().serializedData(serializedData).modelSchema(modelSchema).build();
}
Also used : CustomTypeSchema(com.amplifyframework.core.model.CustomTypeSchema) ModelAssociation(com.amplifyframework.core.model.ModelAssociation) HashMap(java.util.HashMap) SerializedCustomType(com.amplifyframework.core.model.SerializedCustomType) SerializedModel(com.amplifyframework.core.model.SerializedModel) ModelSchema(com.amplifyframework.core.model.ModelSchema) ModelField(com.amplifyframework.core.model.ModelField) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

CustomTypeSchema (com.amplifyframework.core.model.CustomTypeSchema)6 HashMap (java.util.HashMap)5 ModelSchema (com.amplifyframework.core.model.ModelSchema)4 CustomTypeField (com.amplifyframework.core.model.CustomTypeField)3 ModelField (com.amplifyframework.core.model.ModelField)3 SerializedCustomType (com.amplifyframework.core.model.SerializedCustomType)3 SerializedModel (com.amplifyframework.core.model.SerializedModel)3 ArrayList (java.util.ArrayList)3 SchemaRegistry (com.amplifyframework.core.model.SchemaRegistry)2 List (java.util.List)2 Map (java.util.Map)2 NonNull (androidx.annotation.NonNull)1 ModelAssociation (com.amplifyframework.core.model.ModelAssociation)1 ModelProvider (com.amplifyframework.core.model.ModelProvider)1 Test (org.junit.Test)1