-
Notifications
You must be signed in to change notification settings - Fork 254
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(datastore): model identifier support in dart (#1430)
* Convert Model.dart to LF mode * feat(datastore): Adding ModelIdentifier interface * - Update test models t conform interface changes - Update DataStore unit tests to use test_models provided by amplify_test package - Update API unit tests to use updated test models * Update DataStore example App models * Re-apply change after merge main * Regenerated test models with fix adding @OverRide * Fix transitive dependency error * Restore unexpected change * Add unit tests for modelIdentifier getter
- Loading branch information
Showing
76 changed files
with
2,899 additions
and
2,075 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,88 @@ | ||
/* | ||
* Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
import 'model_schema.dart'; | ||
import 'model_schema_definition.dart'; | ||
|
||
abstract class Model { | ||
ModelType getInstanceType(); | ||
|
||
String getId(); | ||
|
||
Map<String, dynamic> toJson(); | ||
|
||
const Model(); | ||
|
||
static ModelSchema defineSchema( | ||
{required Function(ModelSchemaDefinition) define}) { | ||
var definition = ModelSchemaDefinition(); | ||
|
||
define(definition); | ||
|
||
return definition.build(); | ||
} | ||
} | ||
|
||
// New ModelType superclass | ||
abstract class ModelType<T extends Model> { | ||
const ModelType(); | ||
|
||
T fromSerializedMap(Map<String, dynamic> serializedMap) { | ||
return fromJson(serializedMap['serializedData']); | ||
} | ||
|
||
T fromJson(Map<String, dynamic> jsonData); | ||
|
||
String modelName() { | ||
return T.toString(); | ||
} | ||
|
||
/// Perform [action] with [T] as type argument. | ||
R callWithType<R>(R Function<T>() action) => action<T>(); | ||
|
||
// Checks and casts. | ||
bool isInstance(Object o) => o is T; | ||
T cast(Object o) => o as T; | ||
T? safeCast(Object o) => o is T ? o : null; | ||
|
||
// Subtyping checks. | ||
bool operator >=(ModelType other) => other is ModelType<T>; | ||
bool operator <=(ModelType other) => other >= this; | ||
bool operator <(ModelType other) => other >= this && !(this >= other); | ||
bool operator >(ModelType other) => this >= other && !(other >= this); | ||
bool operator ==(Object other) => | ||
other is ModelType && this >= other && other >= this; | ||
int get hashCode => T.hashCode; | ||
} | ||
/* | ||
* Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
import 'model_schema.dart'; | ||
import 'model_schema_definition.dart'; | ||
|
||
abstract class Model { | ||
ModelType getInstanceType(); | ||
|
||
@Deprecated( | ||
'[getId] is being deprecated in favor of custom primary key feature. Use getter [modelIdentifier] to get model identifier.') | ||
String getId(); | ||
|
||
Map<String, dynamic> toJson(); | ||
|
||
ModelIdentifier get modelIdentifier; | ||
|
||
const Model(); | ||
|
||
static ModelSchema defineSchema( | ||
{required Function(ModelSchemaDefinition) define}) { | ||
var definition = ModelSchemaDefinition(); | ||
|
||
define(definition); | ||
|
||
return definition.build(); | ||
} | ||
} | ||
|
||
// New ModelType superclass | ||
abstract class ModelType<T extends Model> { | ||
const ModelType(); | ||
|
||
T fromSerializedMap(Map<String, dynamic> serializedMap) { | ||
return fromJson(serializedMap['serializedData']); | ||
} | ||
|
||
T fromJson(Map<String, dynamic> jsonData); | ||
|
||
String modelName() { | ||
return T.toString(); | ||
} | ||
|
||
/// Perform [action] with [T] as type argument. | ||
R callWithType<R>(R Function<T>() action) => action<T>(); | ||
|
||
// Checks and casts. | ||
bool isInstance(Object o) => o is T; | ||
T cast(Object o) => o as T; | ||
T? safeCast(Object o) => o is T ? o : null; | ||
|
||
// Subtyping checks. | ||
bool operator >=(ModelType other) => other is ModelType<T>; | ||
bool operator <=(ModelType other) => other >= this; | ||
bool operator <(ModelType other) => other >= this && !(this >= other); | ||
bool operator >(ModelType other) => this >= other && !(other >= this); | ||
bool operator ==(Object other) => | ||
other is ModelType && this >= other && other >= this; | ||
int get hashCode => T.hashCode; | ||
} | ||
|
||
/// Model identifier presentation | ||
abstract class ModelIdentifier<T extends Model> { | ||
const ModelIdentifier(); | ||
|
||
/// Serialize a model identifier as a map. | ||
Map<String, dynamic> serializeAsMap(); | ||
|
||
/// Serialize a model identifier as a list of key-value pairs. The order of | ||
/// key-value pairs presents primary key and sort keys. | ||
List<Map<String, dynamic>> serializeAsList(); | ||
|
||
/// Serialize a model identifier into a single string in format: | ||
/// <primaryKey>[#<sortKey>] | ||
String serializeAsString(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.