Skip to content

Commit bc988ac

Browse files
authored
fix(datastore): fix invalid model id field name implication (#1600)
* chore(datastore): convert source file from CRLF to LF * fix(datastore): fix invalid model id field name implication * chore(datastore): convert query_sort.dart to LF mode * fix(datastore): fix bad model id naming and ensure backwards compatibility * fix(datastore): remove redundant logic from native implementation
1 parent 1132cdb commit bc988ac

30 files changed

+854
-846
lines changed

packages/amplify_core/lib/src/types/query/query_predicate.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class QueryPredicateOperation extends QueryPredicate {
7979
Map<String, dynamic> serializeAsMap() {
8080
return <String, dynamic>{
8181
'queryPredicateOperation': <String, dynamic>{
82-
'field': field,
82+
'field': getFieldName(field),
8383
'fieldOperator': queryFieldOperator.serializeAsMap(),
8484
},
8585
};
Lines changed: 67 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,67 @@
1-
/*
2-
* Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3-
*
4-
* Licensed under the Apache License, Version 2.0 (the "License").
5-
* You may not use this file except in compliance with the License.
6-
* A copy of the License is located at
7-
*
8-
* http://aws.amazon.com/apache2.0
9-
*
10-
* or in the "license" file accompanying this file. This file is distributed
11-
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12-
* express or implied. See the License for the specific language governing
13-
* permissions and limitations under the License.
14-
*/
15-
16-
part of 'query_field.dart';
17-
18-
enum QuerySortOrder { ascending, descending }
19-
20-
extension QuerySortOrderExtension on QuerySortOrder {
21-
String toShortString() {
22-
return toString().split('.').last; // toString returns the enumName.value
23-
}
24-
}
25-
26-
/// Represents a model field and an order to sort by (ascending or descending),
27-
/// used to specify the order of results from a query operation
28-
class QuerySortBy {
29-
final QuerySortOrder order;
30-
final String field;
31-
32-
const QuerySortBy({required this.order, required this.field});
33-
34-
int compare<T extends Model>(T a, T b) {
35-
String fieldName = getFieldName(field);
36-
dynamic valueA = a.toJson()[fieldName];
37-
dynamic valueB = b.toJson()[fieldName];
38-
int orderMultiplier = order == QuerySortOrder.ascending ? 1 : -1;
39-
if (valueA == null || valueB == null) {
40-
return orderMultiplier * _compareNull(valueA, valueB);
41-
} else if (valueA is bool && valueB is bool) {
42-
return orderMultiplier * _compareBool(valueA, valueB);
43-
} else if (valueA is Comparable && valueB is Comparable) {
44-
return orderMultiplier * valueA.compareTo(valueB);
45-
}
46-
throw const AmplifyException(
47-
'A non-comparable field was used as a QuerySortBy',
48-
);
49-
}
50-
51-
int _compareBool(bool a, bool b) {
52-
if (a == b) return 0;
53-
return a ? 1 : -1;
54-
}
55-
56-
int _compareNull(Object? a, Object? b) {
57-
if (a == b) return 0;
58-
return a != null ? 1 : -1;
59-
}
60-
61-
Map<String, dynamic> serializeAsMap() {
62-
return <String, dynamic>{'field': field, 'order': order.toShortString()};
63-
}
64-
}
1+
/*
2+
* Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
part of 'query_field.dart';
17+
18+
enum QuerySortOrder { ascending, descending }
19+
20+
extension QuerySortOrderExtension on QuerySortOrder {
21+
String toShortString() {
22+
return toString().split('.').last; // toString returns the enumName.value
23+
}
24+
}
25+
26+
/// Represents a model field and an order to sort by (ascending or descending),
27+
/// used to specify the order of results from a query operation
28+
class QuerySortBy {
29+
final QuerySortOrder order;
30+
final String field;
31+
32+
const QuerySortBy({required this.order, required this.field});
33+
34+
int compare<T extends Model>(T a, T b) {
35+
String fieldName = getFieldName(field);
36+
dynamic valueA = a.toJson()[fieldName];
37+
dynamic valueB = b.toJson()[fieldName];
38+
int orderMultiplier = order == QuerySortOrder.ascending ? 1 : -1;
39+
if (valueA == null || valueB == null) {
40+
return orderMultiplier * _compareNull(valueA, valueB);
41+
} else if (valueA is bool && valueB is bool) {
42+
return orderMultiplier * _compareBool(valueA, valueB);
43+
} else if (valueA is Comparable && valueB is Comparable) {
44+
return orderMultiplier * valueA.compareTo(valueB);
45+
}
46+
throw const AmplifyException(
47+
'A non-comparable field was used as a QuerySortBy',
48+
);
49+
}
50+
51+
int _compareBool(bool a, bool b) {
52+
if (a == b) return 0;
53+
return a ? 1 : -1;
54+
}
55+
56+
int _compareNull(Object? a, Object? b) {
57+
if (a == b) return 0;
58+
return a != null ? 1 : -1;
59+
}
60+
61+
Map<String, dynamic> serializeAsMap() {
62+
return <String, dynamic>{
63+
'field': getFieldName(field),
64+
'order': order.toShortString(),
65+
};
66+
}
67+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ class AmplifyDataStorePluginTest {
306306
fun test_query_with_predicates_success_zero_result() {
307307
val queryOptions =
308308
Where.matches(
309-
field("post.id").eq("123").or(
309+
field("id").eq("123").or(
310310
field("rating").ge(4).and(
311311
not(
312312
field("created").eq("2020-02-20T20:20:20-08:00")
@@ -315,7 +315,7 @@ class AmplifyDataStorePluginTest {
315315
)
316316
)
317317
.paginated(Page.startingAt(2).withLimit(8))
318-
.sorted(field("post.id").ascending(), field("created").descending())
318+
.sorted(field("id").ascending(), field("created").descending())
319319

320320
doAnswer { invocation: InvocationOnMock ->
321321
assertEquals("Post", invocation.arguments[0])

packages/amplify_datastore/android/src/test/kotlin/com/amazonaws/amplify/amplify_datastore/types/query/QueryPredicateBuilderTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import org.junit.Assert
2525
import org.junit.Test
2626

2727
class QueryPredicateBuilderTest {
28-
private val id: QueryField = QueryField.field("post.id")
28+
private val id: QueryField = QueryField.field("id")
2929
private val title: QueryField = QueryField.field("title")
3030
private val rating: QueryField = QueryField.field("rating")
3131
private val created: QueryField = QueryField.field("created")

packages/amplify_datastore/android/src/test/kotlin/com/amazonaws/amplify/amplify_datastore/types/query/QuerySortBuilderTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import org.junit.Assert
2222
import org.junit.Test
2323

2424
class QuerySortBuilderTest {
25-
private val id: QueryField = QueryField.field("post.id")
25+
private val id: QueryField = QueryField.field("id")
2626
private val rating: QueryField = QueryField.field("rating")
2727

2828
@Test

packages/amplify_datastore/ios/Classes/types/query/QueryPredicateBuilder.swift

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,13 @@ import Foundation
1818
import Amplify
1919

2020
public class QueryPredicateBuilder {
21-
22-
// Remove model name if queryPredicate is on id
23-
private static func extractFieldName( field : String? ) -> String? {
24-
25-
if let queryField = field, queryField.hasSuffix(".id"){
26-
return "id"
27-
}
28-
return field;
29-
}
30-
3121
static func fromSerializedMap(_ serializedMap: [String: Any]?) throws -> QueryPredicate {
32-
3322
guard let data = serializedMap else {
3423
return QueryPredicateConstant.all
3524
}
36-
37-
38-
25+
3926
if let queryPredicateOperationMap = data["queryPredicateOperation"] as? [String: Any] {
40-
if let fieldValue = extractFieldName(field: queryPredicateOperationMap["field"] as? String),
27+
if let fieldValue = queryPredicateOperationMap["field"] as? String,
4128
let queryFieldOperatorMap = queryPredicateOperationMap["fieldOperator"] as? [String: Any],
4229
let operatorName = queryFieldOperatorMap["operatorName"] as? String {
4330
let operand = convertToAmplifyPersistable(operand: queryFieldOperatorMap["value"])

0 commit comments

Comments
 (0)