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();
}
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();
}
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");
}
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");
}
Aggregations