Skip to content

Commit 365c5fb

Browse files
authored
chore(api)!: Model .fromJson() Refactor (#4665)
1 parent 5d080ce commit 365c5fb

File tree

112 files changed

+4928
-3336
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

112 files changed

+4928
-3336
lines changed

.github/composite_actions/fetch_backends/action.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ runs:
4646

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

5151
- name: Delete AWS profile
5252
shell: bash

packages/amplify_core/lib/src/types/datastore/conflict_handler/datastore_conflict_handler.dart

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,8 @@ class ConflictData {
1616
ModelType modelType,
1717
Map<String, dynamic> localJson,
1818
Map<String, dynamic> remoteJson,
19-
) : local = modelType.fromJson(
20-
(localJson['serializedData'] as Map).cast<String, dynamic>(),
21-
),
22-
remote = modelType.fromJson(
23-
(remoteJson['serializedData'] as Map).cast<String, dynamic>(),
24-
);
19+
) : local = modelType.fromJson(localJson),
20+
remote = modelType.fromJson(remoteJson);
2521

2622
final Model local;
2723
final Model remote;

packages/amplify_core/lib/src/types/datastore/hub/hub_event_element.dart

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,7 @@ Model _parseModelFromMap(
2929
) {
3030
final serializedElement = serializedHubEventElement['element'] as Map;
3131
final modelName = serializedHubEventElement['modelName'] as String;
32-
final modelData = serializedElement['model'] as Map;
33-
final serializedModelData =
34-
(modelData['serializedData'] as Map).cast<String, dynamic>();
35-
return provider
36-
.getModelTypeByModelName(modelName)
37-
.fromJson(serializedModelData);
32+
final modelData = Map<String, dynamic>.from(serializedElement['model'] as Map)
33+
.cast<String, dynamic>();
34+
return provider.getModelTypeByModelName(modelName).fromJson(modelData);
3835
}

packages/amplify_core/lib/src/types/datastore/models/subscription_event.dart

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ class SubscriptionEvent<T extends Model> {
2121
final serializedItem = Map<String, dynamic>.from(map['item'] as Map);
2222

2323
return SubscriptionEvent(
24-
item: modelType.fromJson(
25-
Map<String, dynamic>.from(serializedItem['serializedData'] as Map),
26-
),
24+
item: modelType.fromJson(serializedItem),
2725
eventType: EventType.values
2826
.firstWhere((e) => e.name == map['eventType'] as String?),
2927
modelType: modelType,

packages/amplify_datastore/android/src/main/kotlin/com/amazonaws/amplify/amplify_datastore/types/model/FlutterSerializedCustomType.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

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

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

1819
fun toMap(): Map<String, Any> {
19-
return mapOf(
20-
"serializedData" to serializedData,
21-
"customTypeName" to customTypeName
22-
)
20+
val cleanedSerializedData: Map<String, Any> = serializedData.filterValues { it != null }.cast()
21+
22+
val modelNameMap = mapOf("customTypeName" to customTypeName)
23+
24+
return cleanedSerializedData+modelNameMap
2325
}
2426

2527
private fun parseCustomTypeName(customTypeName: String?): String = customTypeName ?: ""

packages/amplify_datastore/android/src/main/kotlin/com/amazonaws/amplify/amplify_datastore/types/model/FlutterSerializedModel.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ data class FlutterSerializedModel(val serializedModel: SerializedModel) {
2222
parseModelName(serializedModel.modelName) // ModelSchema -> SerializedModel should always have a name
2323

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

27-
return mapOf(
28-
"serializedData" to cleanedSerializedData,
29-
"modelName" to modelName
30-
)
26+
val cleanedSerializedData: Map<String, Any> = serializedData.filterValues { it != null }.cast()
27+
28+
val modelNameMap = mapOf("__modelName" to modelName)
29+
30+
return modelNameMap+cleanedSerializedData
3131
}
3232

3333
private fun parseModelName(modelName: String?): String {

packages/amplify_datastore/android/src/test/kotlin/com/amazonaws/amplify/amplify_datastore/AmplifyDataStoreHubTest.kt

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,11 @@ class AmplifyDataStoreHubTest {
7979
val element: HashMap<String, Any> = eventData["element"] as HashMap<String, Any>
8080
var metadataMap: HashMap<String, Any>
8181
val modelMap: HashMap<String, Any> = element["model"] as HashMap<String, Any>
82-
val serializedData: HashMap<String, Any> = modelMap["serializedData"] as HashMap<String, Any>
8382
val modelMetadata = ModelMetadata(modelMap["id"] as String, null, null, null)
8483
val modelData = mapOf(
85-
"id" to serializedData["id"] as String,
86-
"title" to serializedData["title"] as String,
87-
"created" to Temporal.DateTime(serializedData["created"] as String)
84+
"id" to modelMap["id"] as String,
85+
"title" to modelMap["title"] as String,
86+
"created" to Temporal.DateTime(modelMap["created"] as String)
8887
)
8988
val instance = SerializedModel.builder()
9089
.modelSchema(modelSchema)
@@ -128,17 +127,16 @@ class AmplifyDataStoreHubTest {
128127
val element: HashMap<String, Any> = eventData["element"] as HashMap<String, Any>
129128
val metadataMap: HashMap<String, Any> = element["syncMetadata"] as HashMap<String, Any>
130129
val modelMap: HashMap<String, Any> = element["model"] as HashMap<String, Any>
131-
val serializedData: HashMap<String, Any> = modelMap["serializedData"] as HashMap<String, Any>
132130
val modelMetadata = ModelMetadata(
133131
metadataMap["id"] as String,
134132
metadataMap["_deleted"] as Boolean,
135133
metadataMap["_version"] as Int,
136134
time
137135
)
138136
val modelData = mapOf(
139-
"id" to serializedData["id"] as String,
140-
"title" to serializedData["title"] as String,
141-
"created" to Temporal.DateTime(serializedData["created"] as String)
137+
"id" to modelMap["id"] as String,
138+
"title" to modelMap["title"] as String,
139+
"created" to Temporal.DateTime(modelMap["created"] as String)
142140
)
143141
val instance = SerializedModel.builder()
144142
.modelSchema(modelSchema)

packages/amplify_datastore/android/src/test/kotlin/com/amazonaws/amplify/amplify_datastore/AmplifySerializedModelTest.kt

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@ package com.amazonaws.amplify.amplify_datastore
66
import com.amazonaws.amplify.amplify_datastore.types.model.FlutterSerializedModel
77
import org.junit.Assert
88
import org.junit.Test
9-
import org.junit.runner.RunWith
10-
import org.robolectric.RobolectricTestRunner
119

12-
@RunWith(RobolectricTestRunner::class)
1310
class AmplifySerializedModelTest {
1411

1512
private var serializedModelMaps: Map<String, Any> = (
@@ -63,14 +60,8 @@ class AmplifySerializedModelTest {
6360
// timestamp value is expected as Long, however readMapFromFile util create Integer for this field
6461
// Correct the field before assertion
6562
val correctedRefMap = refMap.mapValues {
66-
if (it.key == "serializedData") {
67-
(it.value as Map<String, Any>).mapValues { entry ->
68-
if (entry.key == "timestampType") {
69-
(entry.value as Number).toLong()
70-
} else {
71-
entry.value
72-
}
73-
}
63+
if (it.key == "timestampType") {
64+
(it.value as Number).toLong()
7465
} else {
7566
it.value
7667
}
@@ -90,8 +81,8 @@ class AmplifySerializedModelTest {
9081

9182
// Verify result
9283
Assert.assertEquals(
93-
flutterSerializedModel.toMap(),
94-
expectedResult
84+
expectedResult,
85+
flutterSerializedModel.toMap()
9586
)
9687
}
9788
}

packages/amplify_datastore/example/ios/Flutter/AppFrameworkInfo.plist

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/amplify_datastore/example/ios/Runner.xcodeproj/project.pbxproj

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/amplify_datastore/example/ios/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)