Search in sources :

Example 11 with ModelField

use of com.amplifyframework.core.model.ModelField 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)

Example 12 with ModelField

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

the class SerializedModelAdapter method deserialize.

@Override
public SerializedModel deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    JsonObject object = json.getAsJsonObject();
    ModelSchema modelSchema = context.deserialize(object.get("modelSchema"), ModelSchema.class);
    JsonObject serializedDataObject = object.get("serializedData").getAsJsonObject();
    Map<String, Object> serializedData = new HashMap<>(GsonObjectConverter.toMap(serializedDataObject));
    // Patch up nested models as SerializedModels themselves.
    for (Map.Entry<String, JsonElement> item : serializedDataObject.entrySet()) {
        ModelField field = modelSchema.getFields().get(item.getKey());
        if (field != null && field.isModel()) {
            SchemaRegistry schemaRegistry = SchemaRegistry.instance();
            ModelSchema nestedModelSchema = schemaRegistry.getModelSchemaForModelClass(field.getTargetType());
            serializedData.put(field.getName(), SerializedModel.builder().serializedData(Collections.singletonMap("id", item.getValue().getAsString())).modelSchema(nestedModelSchema).build());
        }
    }
    return SerializedModel.builder().serializedData(serializedData).modelSchema(modelSchema).build();
}
Also used : ModelSchema(com.amplifyframework.core.model.ModelSchema) ModelField(com.amplifyframework.core.model.ModelField) HashMap(java.util.HashMap) JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject) JsonObject(com.google.gson.JsonObject) HashMap(java.util.HashMap) Map(java.util.Map) SchemaRegistry(com.amplifyframework.core.model.SchemaRegistry)

Example 13 with ModelField

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

the class SelectionSetTest method nestedSerializedModel.

/**
 * Test generating SelectionSet for nested ModelSchema using SerializedModel.
 * @throws AmplifyException if a ModelSchema can't be derived from postSchema
 */
@Test
public void nestedSerializedModel() throws AmplifyException {
    SchemaRegistry schemaRegistry = SchemaRegistry.instance();
    ModelField blogModelId = ModelField.builder().isRequired(true).targetType("ID").build();
    ModelField blogName = ModelField.builder().isRequired(true).targetType("String").build();
    Map<String, ModelField> blogFields = new HashMap<>();
    blogFields.put("id", blogModelId);
    blogFields.put("name", blogName);
    ModelSchema blogSchema = ModelSchema.builder().name("Blog").pluralName("Blogs").modelClass(SerializedModel.class).fields(blogFields).build();
    ModelField postModelId = ModelField.builder().isRequired(true).targetType("ID").build();
    ModelField postTitle = ModelField.builder().isRequired(true).targetType("String").build();
    ModelField postBlog = ModelField.builder().isRequired(true).targetType("Blog").isModel(true).build();
    Map<String, ModelField> postFields = new HashMap<>();
    postFields.put("id", postModelId);
    postFields.put("title", postTitle);
    postFields.put("blog", postBlog);
    Map<String, ModelAssociation> associations = new HashMap<>();
    associations.put("blog", ModelAssociation.builder().name("BelongsTo").targetName("blogId").associatedType("Blog").build());
    ModelSchema postSchema = ModelSchema.builder().name("Post").pluralName("Posts").modelClass(SerializedModel.class).fields(postFields).associations(associations).build();
    schemaRegistry.register("Blog", blogSchema);
    schemaRegistry.register("Post", postSchema);
    SelectionSet selectionSet = SelectionSet.builder().modelClass(SerializedModel.class).modelSchema(postSchema).operation(QueryType.SYNC).requestOptions(new JustIDGraphQLRequestOptions()).build();
    assertEquals(Resources.readAsString("selection-set-post-nested.txt"), selectionSet.toString() + "\n");
}
Also used : ModelSchema(com.amplifyframework.core.model.ModelSchema) ModelAssociation(com.amplifyframework.core.model.ModelAssociation) ModelField(com.amplifyframework.core.model.ModelField) HashMap(java.util.HashMap) SerializedModel(com.amplifyframework.core.model.SerializedModel) SchemaRegistry(com.amplifyframework.core.model.SchemaRegistry) Test(org.junit.Test)

Example 14 with ModelField

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

the class SelectionSetTest method ownerFieldAddedForImplicitOwnerAuthWhenUsingSchema.

/**
 * As in {@link #ownerFieldAddedForImplicitOwnerAuth()}, tests that owner field is added to
 * selection set when a model has an {@link AuthStrategy#OWNER} auth strategy, and the owner is
 * implicit. The difference in this test is that the {@link SelectionSet} is built directly
 * from a {@link ModelSchema} instead of an Java model class.
 * @throws AmplifyException on failure to build selection set (not expected)
 */
@Test
public void ownerFieldAddedForImplicitOwnerAuthWhenUsingSchema() throws AmplifyException {
    ModelField modelId = ModelField.builder().isRequired(true).targetType("ID").javaClassForValue(String.class).build();
    ModelField title = ModelField.builder().isRequired(true).targetType("String").javaClassForValue(String.class).build();
    Map<String, ModelField> fields = new HashMap<>();
    fields.put("id", modelId);
    fields.put("title", title);
    ModelSchema schema = ModelSchema.builder().name("OwnerAuth").pluralName("OwnerAuths").modelClass(SerializedModel.class).fields(fields).authRules(Collections.singletonList(AuthRule.builder().authStrategy(AuthStrategy.OWNER).identityClaim("cognito:username").ownerField("owner").operations(Arrays.asList(ModelOperation.CREATE, ModelOperation.UPDATE, ModelOperation.DELETE, ModelOperation.READ)).build())).build();
    SelectionSet selectionSet = SelectionSet.builder().modelClass(// Note: this is different from the above test.
    SerializedModel.class).modelSchema(// Note: this test passes an explicit schema, instead of relying on modelClass().
    schema).operation(QueryType.GET).requestOptions(new DefaultGraphQLRequestOptions()).build();
    assertEquals(Resources.readAsString("selection-set-ownerauth.txt"), selectionSet.toString() + "\n");
}
Also used : ModelSchema(com.amplifyframework.core.model.ModelSchema) ModelField(com.amplifyframework.core.model.ModelField) HashMap(java.util.HashMap) SerializedModel(com.amplifyframework.core.model.SerializedModel) Test(org.junit.Test)

Aggregations

ModelField (com.amplifyframework.core.model.ModelField)14 HashMap (java.util.HashMap)9 ModelSchema (com.amplifyframework.core.model.ModelSchema)8 SerializedModel (com.amplifyframework.core.model.SerializedModel)7 ModelAssociation (com.amplifyframework.core.model.ModelAssociation)5 SchemaRegistry (com.amplifyframework.core.model.SchemaRegistry)4 Test (org.junit.Test)4 AmplifyException (com.amplifyframework.AmplifyException)3 CustomTypeSchema (com.amplifyframework.core.model.CustomTypeSchema)3 ArrayList (java.util.ArrayList)3 NonNull (androidx.annotation.NonNull)2 AuthRule (com.amplifyframework.core.model.AuthRule)2 CustomTypeField (com.amplifyframework.core.model.CustomTypeField)2 DataStoreException (com.amplifyframework.datastore.DataStoreException)2 Field (java.lang.reflect.Field)2 List (java.util.List)2 Map (java.util.Map)2 Consumer (com.amplifyframework.core.Consumer)1 Model (com.amplifyframework.core.model.Model)1 ModelIndex (com.amplifyframework.core.model.ModelIndex)1