Search in sources :

Example 1 with CustomTypeSchema

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

the class SelectionSetTest method nestedSerializedModelAndSerializedCustomType.

/**
 * Test generating SelectionSet for ModelSchema that nests CustomTypeSchema.
 * @throws AmplifyException if a ModelSchema can't be derived from OwnerAuth.class
 */
@Test
public void nestedSerializedModelAndSerializedCustomType() throws AmplifyException {
    SchemaRegistry schemaRegistry = SchemaRegistry.instance();
    CustomTypeField phoneCountryField = CustomTypeField.builder().targetType("String").isRequired(true).build();
    CustomTypeField phoneAreaField = CustomTypeField.builder().targetType("String").isRequired(true).build();
    CustomTypeField phoneNumber = CustomTypeField.builder().targetType("String").isRequired(true).build();
    Map<String, CustomTypeField> phoneFields = new HashMap<>();
    phoneFields.put("country", phoneCountryField);
    phoneFields.put("area", phoneAreaField);
    phoneFields.put("number", phoneNumber);
    CustomTypeSchema phoneSchema = CustomTypeSchema.builder().fields(phoneFields).name("Phone").pluralName("Phones").build();
    CustomTypeField addressCityField = CustomTypeField.builder().targetType("String").isRequired(true).build();
    CustomTypeField addressPhoneNumberField = CustomTypeField.builder().targetType("Phone").isCustomType(true).build();
    CustomTypeField addressLine1Field = CustomTypeField.builder().targetType("String").isRequired(true).build();
    CustomTypeField addressLine2Field = CustomTypeField.builder().targetType("String").build();
    CustomTypeField addressStateField = CustomTypeField.builder().targetType("String").isRequired(true).build();
    CustomTypeField addressPostalCodeField = CustomTypeField.builder().targetType("String").isRequired(true).build();
    Map<String, CustomTypeField> addressFields = new HashMap<>();
    addressFields.put("city", addressCityField);
    addressFields.put("phoneNumber", addressPhoneNumberField);
    addressFields.put("line1", addressLine1Field);
    addressFields.put("line2", addressLine2Field);
    addressFields.put("state", addressStateField);
    addressFields.put("postalCode", addressPostalCodeField);
    CustomTypeSchema addressSchema = CustomTypeSchema.builder().fields(addressFields).name("Address").pluralName("Addresses").build();
    ModelField personAddressField = ModelField.builder().name("address").isCustomType(true).targetType("Address").isRequired(true).build();
    ModelField personNameField = ModelField.builder().name("name").targetType("String").isRequired(true).build();
    ModelField personPhonesField = ModelField.builder().name("phoneNumbers").targetType("Phone").isCustomType(true).isArray(true).build();
    Map<String, ModelField> personFields = new HashMap<>();
    personFields.put("address", personAddressField);
    personFields.put("name", personNameField);
    personFields.put("phones", personPhonesField);
    ModelSchema personSchema = ModelSchema.builder().fields(personFields).name("Person").pluralName("People").modelClass(SerializedModel.class).build();
    // Register custom type schema for usage in SelectionSet
    schemaRegistry.register("Address", addressSchema);
    schemaRegistry.register("Phone", phoneSchema);
    // Register model schema for usage in SelectionSet
    schemaRegistry.register("Person", personSchema);
    SelectionSet selectionSet = SelectionSet.builder().modelClass(SerializedModel.class).modelSchema(personSchema).operation(QueryType.GET).requestOptions(new DefaultGraphQLRequestOptions()).build();
    String result = selectionSet.toString();
    assertEquals(Resources.readAsString("selection-set-nested-serialized-model-serialized-custom-type.txt"), result + "\n");
}
Also used : CustomTypeSchema(com.amplifyframework.core.model.CustomTypeSchema) ModelSchema(com.amplifyframework.core.model.ModelSchema) ModelField(com.amplifyframework.core.model.ModelField) HashMap(java.util.HashMap) CustomTypeField(com.amplifyframework.core.model.CustomTypeField) SerializedModel(com.amplifyframework.core.model.SerializedModel) SchemaRegistry(com.amplifyframework.core.model.SchemaRegistry) Test(org.junit.Test)

Example 2 with CustomTypeSchema

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

the class AppSyncRequestFactoryTest method buildSerializedModelNestsSerializedCustomTypeSchemas.

private void buildSerializedModelNestsSerializedCustomTypeSchemas() {
    SchemaRegistry schemaRegistry = SchemaRegistry.instance();
    CustomTypeField addressLine1Field = CustomTypeField.builder().name("line1").isRequired(true).targetType("String").build();
    CustomTypeField addressLine2Field = CustomTypeField.builder().name("line2").isRequired(false).targetType("String").build();
    CustomTypeField addressCityField = CustomTypeField.builder().name("city").isRequired(true).targetType("String").build();
    CustomTypeField addressStateField = CustomTypeField.builder().name("state").isRequired(true).targetType("String").build();
    CustomTypeField addressPostalCodeField = CustomTypeField.builder().name("postalCode").isRequired(true).targetType("String").build();
    Map<String, CustomTypeField> addressFields = new HashMap<>();
    addressFields.put("line1", addressLine1Field);
    addressFields.put("line2", addressLine2Field);
    addressFields.put("city", addressCityField);
    addressFields.put("state", addressStateField);
    addressFields.put("postalCode", addressPostalCodeField);
    CustomTypeSchema addressSchema = CustomTypeSchema.builder().name("Address").pluralName("Addresses").fields(addressFields).build();
    CustomTypeField phoneCountryField = CustomTypeField.builder().name("country").isRequired(true).targetType("String").build();
    CustomTypeField phoneAreaField = CustomTypeField.builder().name("area").isRequired(true).targetType("String").build();
    CustomTypeField phoneNumberField = CustomTypeField.builder().name("number").isRequired(true).targetType("String").build();
    Map<String, CustomTypeField> phoneFields = new HashMap<>();
    phoneFields.put("country", phoneCountryField);
    phoneFields.put("area", phoneAreaField);
    phoneFields.put("number", phoneNumberField);
    CustomTypeSchema phoneSchema = CustomTypeSchema.builder().name("Phone").pluralName("Phones").fields(phoneFields).build();
    CustomTypeField bioEmailField = CustomTypeField.builder().name("email").isRequired(false).targetType("String").build();
    CustomTypeField bioPhoneField = CustomTypeField.builder().name("phone").isCustomType(true).targetType("Phone").build();
    Map<String, CustomTypeField> bioFields = new HashMap<>();
    bioFields.put("email", bioEmailField);
    bioFields.put("phone", bioPhoneField);
    CustomTypeSchema bioSchema = CustomTypeSchema.builder().name("Bio").fields(bioFields).build();
    ModelField personBioField = ModelField.builder().name("bio").targetType("Bio").isCustomType(true).isRequired(true).build();
    ModelField personNameField = ModelField.builder().name("name").targetType("String").isRequired(true).build();
    ModelField personMailingAddressesField = ModelField.builder().name("mailingAddresses").targetType("Address").isCustomType(true).isArray(true).build();
    ModelField personIdField = ModelField.builder().name("id").targetType("String").isRequired(true).build();
    Map<String, ModelField> personFields = new HashMap<>();
    personFields.put("bio", personBioField);
    personFields.put("name", personNameField);
    personFields.put("mailingAddresses", personMailingAddressesField);
    personFields.put("id", personIdField);
    ModelSchema personSchema = ModelSchema.builder().name("Person").pluralName("People").fields(personFields).modelClass(SerializedModel.class).build();
    schemaRegistry.register("Address", addressSchema);
    schemaRegistry.register("Phone", phoneSchema);
    schemaRegistry.register("Bio", bioSchema);
    schemaRegistry.register("Person", personSchema);
}
Also used : CustomTypeSchema(com.amplifyframework.core.model.CustomTypeSchema) ModelSchema(com.amplifyframework.core.model.ModelSchema) ModelField(com.amplifyframework.core.model.ModelField) HashMap(java.util.HashMap) CustomTypeField(com.amplifyframework.core.model.CustomTypeField) SerializedModel(com.amplifyframework.core.model.SerializedModel) SchemaRegistry(com.amplifyframework.core.model.SchemaRegistry)

Example 3 with CustomTypeSchema

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

the class CompoundModelProvider method of.

/**
 * Gets an {@link CompoundModelProvider} that provides all of the same models as the
 * constituent {@link ModelProvider} that are provided. The version of the returned
 * {@link CompoundModelProvider} shall be stable UUID hash of the versions of all
 * provided {@link ModelProvider}s.
 * @param modelProviders model providers
 * @return A compound provider, which provides the models of all of the input providers
 */
@NonNull
public static CompoundModelProvider of(@NonNull ModelProvider... modelProviders) {
    final Map<String, ModelSchema> modelSchemaMap = new HashMap<>();
    final Map<String, CustomTypeSchema> customTypeSchemaMap = new HashMap<>();
    StringBuilder componentVersionBuffer = new StringBuilder();
    for (ModelProvider componentProvider : modelProviders) {
        componentVersionBuffer.append(componentProvider.version());
        modelSchemaMap.putAll(componentProvider.modelSchemas());
        customTypeSchemaMap.putAll(componentProvider.customTypeSchemas());
    }
    String version = UUID.nameUUIDFromBytes(componentVersionBuffer.toString().getBytes()).toString();
    SimpleModelProvider delegateProvider = SimpleModelProvider.instance(version, modelSchemaMap, customTypeSchemaMap);
    return new CompoundModelProvider(delegateProvider);
}
Also used : CustomTypeSchema(com.amplifyframework.core.model.CustomTypeSchema) ModelSchema(com.amplifyframework.core.model.ModelSchema) HashMap(java.util.HashMap) ModelProvider(com.amplifyframework.core.model.ModelProvider) NonNull(androidx.annotation.NonNull)

Example 4 with CustomTypeSchema

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

the class SQLiteStorageAdapter method getValueOfListCustomTypeField.

private List<SerializedCustomType> getValueOfListCustomTypeField(String fieldTargetType, List<Map<String, Object>> listItems) {
    // if the filed is optional and has null value instead of an array
    if (listItems == null) {
        return null;
    }
    final CustomTypeSchema nestedCustomTypeSchema = schemaRegistry.getCustomTypeSchemaForCustomTypeClass(fieldTargetType);
    List<SerializedCustomType> listOfCustomType = new ArrayList<>();
    for (Map<String, Object> listItem : listItems) {
        SerializedCustomType customType = createSerializedCustomType(nestedCustomTypeSchema, listItem);
        listOfCustomType.add(customType);
    }
    return listOfCustomType;
}
Also used : CustomTypeSchema(com.amplifyframework.core.model.CustomTypeSchema) SerializedCustomType(com.amplifyframework.core.model.SerializedCustomType) ArrayList(java.util.ArrayList)

Example 5 with CustomTypeSchema

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

the class SQLiteStorageAdapter method createSerializedCustomType.

private SerializedCustomType createSerializedCustomType(CustomTypeSchema customTypeSchema, Map<String, Object> data) {
    final Map<String, Object> serializedData = new HashMap<>();
    for (Map.Entry<String, Object> entry : data.entrySet()) {
        CustomTypeField field = customTypeSchema.getFields().get(entry.getKey());
        if (field == null) {
            continue;
        }
        if (field.isCustomType() && entry.getValue() != null) {
            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") Map<String, Object> nestedData = (Map<String, Object>) entry.getValue();
                serializedData.put(entry.getKey(), createSerializedCustomType(nestedCustomTypeSchema, nestedData));
            }
        } else {
            serializedData.put(entry.getKey(), entry.getValue());
        }
    }
    return SerializedCustomType.builder().serializedData(serializedData).customTypeSchema(customTypeSchema).build();
}
Also used : CustomTypeSchema(com.amplifyframework.core.model.CustomTypeSchema) HashMap(java.util.HashMap) SerializedCustomType(com.amplifyframework.core.model.SerializedCustomType) CustomTypeField(com.amplifyframework.core.model.CustomTypeField) 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