public Object convertValueFromSource()

in aws-datastore/src/main/java/com/amplifyframework/datastore/storage/sqlite/SQLiteModelFieldTypeConverter.java [157:239]


    public Object convertValueFromSource(
            @NonNull Cursor cursor,
            @NonNull ModelField field
    ) throws DataStoreException {
        final JavaFieldType javaFieldType = TypeConverter.getJavaFieldType(field);
        try {
            // Skip if there is no equivalent column for field in object
            final SQLiteColumn column = columns.get(field.getName());
            if (column == null) {
                LOGGER.verbose(String.format("Column with name %s does not exist", field.getName()));
                return null;
            }

            String columnName = column.getAliasedName();
            if (javaFieldType == JavaFieldType.MODEL) {
                int newInnerModelCount = 1;
                String fieldTargetType = field.getTargetType();
                if (cursorInnerModelCounts.containsKey(fieldTargetType)) {
                    Integer currentInnerModelCount = cursorInnerModelCounts.get(fieldTargetType);
                    newInnerModelCount += currentInnerModelCount == null ? 0 : currentInnerModelCount;
                }
                cursorInnerModelCounts.put(fieldTargetType, newInnerModelCount);
            }
            if (isInnerModel && cursorInnerModelCounts.containsKey(parentSchema.getName())) {
                Integer modelCount = cursorInnerModelCounts.get(parentSchema.getName());
                if (!Objects.equals(modelCount, 1)) {
                    // More than 1 of the model the field belongs to is present in the cursor
                    columnName += modelCount;
                }
            }
            
            final int columnIndex = cursor.getColumnIndexOrThrow(columnName);
            // This check is necessary, because primitive values will return 0 even when null
            if (cursor.isNull(columnIndex)) {
                return null;
            }

            final String valueAsString = cursor.getString(columnIndex);
            LOGGER.verbose(String.format(
                    "Attempt to convert value \"%s\" from field %s of type %s in model %s",
                    valueAsString, field.getName(), field.getTargetType(), parentSchema.getName()
            ));

            switch (javaFieldType) {
                case STRING:
                    return cursor.getString(columnIndex);
                case MODEL:
                    return convertModelAssociationToTarget(cursor, field);
                case ENUM:
                    return convertEnumValueToTarget(valueAsString, field);
                case CUSTOM_TYPE:
                    return convertCustomTypeToTarget(cursor, field, columnIndex);
                case INTEGER:
                    return cursor.getInt(columnIndex);
                case BOOLEAN:
                    return cursor.getInt(columnIndex) != 0;
                case FLOAT:
                    return cursor.getFloat(columnIndex);
                case DOUBLE:
                    return cursor.getDouble(columnIndex);
                case LONG:
                    return cursor.getLong(columnIndex);
                case DATE:
                    return new Temporal.Date(valueAsString);
                case DATE_TIME:
                    return new Temporal.DateTime(valueAsString);
                case TIME:
                    return new Temporal.Time(valueAsString);
                case TIMESTAMP:
                    return new Temporal.Timestamp(cursor.getLong(columnIndex), TimeUnit.SECONDS);
                default:
                    LOGGER.warn(String.format("Field of type %s is not supported. Fallback to null.", javaFieldType));
                    return null;
            }
        } catch (Exception exception) {
            throw new DataStoreException(
                    String.format("Error converting field \"%s\" from model \"%s\"",
                    field.getName(), parentSchema.getName()),
                    exception,
                    AmplifyException.REPORT_BUG_TO_AWS_SUGGESTION
            );
        }
    }