Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(api)!: Model .fromJson() Refactor #4665

Merged
merged 8 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/composite_actions/fetch_backends/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ runs:

- name: Undo any codegen changes from amplify pull
shell: bash
run: dart pub global run aft exec --include=${{ inputs.scope }} -- [ -d "lib/models" ] && git checkout lib/models/ || exit 0
run: dart pub global run aft exec --include=${{ inputs.scope }} -- [ -d "lib/models" ] && git checkout '**/lib/models/*' || exit 0

- name: Delete AWS profile
shell: bash
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,8 @@ class ConflictData {
ModelType modelType,
Map<String, dynamic> localJson,
Map<String, dynamic> remoteJson,
) : local = modelType.fromJson(
(localJson['serializedData'] as Map).cast<String, dynamic>(),
),
remote = modelType.fromJson(
(remoteJson['serializedData'] as Map).cast<String, dynamic>(),
);
) : local = modelType.fromJson(localJson),
remote = modelType.fromJson(remoteJson);

final Model local;
final Model remote;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@ Model _parseModelFromMap(
) {
final serializedElement = serializedHubEventElement['element'] as Map;
final modelName = serializedHubEventElement['modelName'] as String;
final modelData = serializedElement['model'] as Map;
final serializedModelData =
(modelData['serializedData'] as Map).cast<String, dynamic>();
return provider
.getModelTypeByModelName(modelName)
.fromJson(serializedModelData);
final modelData = Map<String, dynamic>.from(serializedElement['model'] as Map)
.cast<String, dynamic>();
return provider.getModelTypeByModelName(modelName).fromJson(modelData);
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ class SubscriptionEvent<T extends Model> {
final serializedItem = Map<String, dynamic>.from(map['item'] as Map);

return SubscriptionEvent(
item: modelType.fromJson(
Map<String, dynamic>.from(serializedItem['serializedData'] as Map),
),
item: modelType.fromJson(serializedItem),
eventType: EventType.values
.firstWhere((e) => e.name == map['eventType'] as String?),
modelType: modelType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

package com.amazonaws.amplify.amplify_datastore.types.model

import com.amazonaws.amplify.amplify_datastore.util.cast
import com.amplifyframework.core.model.CustomTypeSchema
import com.amplifyframework.core.model.SerializedCustomType
import com.amplifyframework.core.model.temporal.Temporal
Expand All @@ -16,10 +17,11 @@ data class FlutterSerializedCustomType(val serializedCustomType: SerializedCusto
private val customTypeName: String = parseCustomTypeName(serializedCustomType.customTypeName)

fun toMap(): Map<String, Any> {
return mapOf(
"serializedData" to serializedData,
"customTypeName" to customTypeName
)
val cleanedSerializedData: Map<String, Any> = serializedData.filterValues { it != null }.cast()

val modelNameMap = mapOf("customTypeName" to customTypeName)

return cleanedSerializedData+modelNameMap
}

private fun parseCustomTypeName(customTypeName: String?): String = customTypeName ?: ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ data class FlutterSerializedModel(val serializedModel: SerializedModel) {
parseModelName(serializedModel.modelName) // ModelSchema -> SerializedModel should always have a name

fun toMap(): Map<String, Any> {
val cleanedSerializedData: Map<String, Any?> = serializedData.filterValues { it != null }

return mapOf(
"serializedData" to cleanedSerializedData,
"modelName" to modelName
)
val cleanedSerializedData: Map<String, Any> = serializedData.filterValues { it != null }.cast()

val modelNameMap = mapOf("__modelName" to modelName)

return modelNameMap+cleanedSerializedData
}

private fun parseModelName(modelName: String?): String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,11 @@ class AmplifyDataStoreHubTest {
val element: HashMap<String, Any> = eventData["element"] as HashMap<String, Any>
var metadataMap: HashMap<String, Any>
val modelMap: HashMap<String, Any> = element["model"] as HashMap<String, Any>
val serializedData: HashMap<String, Any> = modelMap["serializedData"] as HashMap<String, Any>
val modelMetadata = ModelMetadata(modelMap["id"] as String, null, null, null)
val modelData = mapOf(
"id" to serializedData["id"] as String,
"title" to serializedData["title"] as String,
"created" to Temporal.DateTime(serializedData["created"] as String)
"id" to modelMap["id"] as String,
"title" to modelMap["title"] as String,
"created" to Temporal.DateTime(modelMap["created"] as String)
)
val instance = SerializedModel.builder()
.modelSchema(modelSchema)
Expand Down Expand Up @@ -128,17 +127,16 @@ class AmplifyDataStoreHubTest {
val element: HashMap<String, Any> = eventData["element"] as HashMap<String, Any>
val metadataMap: HashMap<String, Any> = element["syncMetadata"] as HashMap<String, Any>
val modelMap: HashMap<String, Any> = element["model"] as HashMap<String, Any>
val serializedData: HashMap<String, Any> = modelMap["serializedData"] as HashMap<String, Any>
val modelMetadata = ModelMetadata(
metadataMap["id"] as String,
metadataMap["_deleted"] as Boolean,
metadataMap["_version"] as Int,
time
)
val modelData = mapOf(
"id" to serializedData["id"] as String,
"title" to serializedData["title"] as String,
"created" to Temporal.DateTime(serializedData["created"] as String)
"id" to modelMap["id"] as String,
"title" to modelMap["title"] as String,
"created" to Temporal.DateTime(modelMap["created"] as String)
)
val instance = SerializedModel.builder()
.modelSchema(modelSchema)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ package com.amazonaws.amplify.amplify_datastore
import com.amazonaws.amplify.amplify_datastore.types.model.FlutterSerializedModel
import org.junit.Assert
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner

@RunWith(RobolectricTestRunner::class)
class AmplifySerializedModelTest {

private var serializedModelMaps: Map<String, Any> = (
Expand Down Expand Up @@ -63,14 +60,8 @@ class AmplifySerializedModelTest {
// timestamp value is expected as Long, however readMapFromFile util create Integer for this field
// Correct the field before assertion
val correctedRefMap = refMap.mapValues {
if (it.key == "serializedData") {
(it.value as Map<String, Any>).mapValues { entry ->
if (entry.key == "timestampType") {
(entry.value as Number).toLong()
} else {
entry.value
}
}
if (it.key == "timestampType") {
(it.value as Number).toLong()
} else {
it.value
}
Expand All @@ -90,8 +81,8 @@ class AmplifySerializedModelTest {

// Verify result
Assert.assertEquals(
flutterSerializedModel.toMap(),
expectedResult
expectedResult,
flutterSerializedModel.toMap()
)
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions packages/amplify_datastore/example/ios/Podfile

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading