Skip to content

Commit

Permalink
feat(datastore): model identifier support in dart (#1430)
Browse files Browse the repository at this point in the history
* 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
HuiSF committed Apr 6, 2022
1 parent c4a910c commit 8a85689
Show file tree
Hide file tree
Showing 76 changed files with 2,899 additions and 2,075 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,23 @@ class MockModelType extends ModelType<Model> {
}
}

class _MockModelIdentifier implements ModelIdentifier {
final List<Map<String, dynamic>> emptyList = const [];
final Map<String, dynamic> emptyMap = const <String, dynamic>{};
final String emptyString = '';

const _MockModelIdentifier();

@override
List<Map<String, dynamic>> serializeAsList() => emptyList;

@override
Map<String, dynamic> serializeAsMap() => emptyMap;

@override
String serializeAsString() => emptyString;
}

class MockModel extends Model {
final String id;

Expand All @@ -32,6 +49,11 @@ class MockModel extends Model {
@override
String getId() => id;

@override
ModelIdentifier get modelIdentifier {
return const _MockModelIdentifier();
}

@override
ModelType<Model> getInstanceType() => const MockModelType();

Expand Down
156 changes: 88 additions & 68 deletions packages/amplify_core/lib/src/types/models/model.dart
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();
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
// ignore_for_file: public_member_api_docs, file_names, unnecessary_new, prefer_if_null_operators, prefer_const_constructors, slash_for_doc_comments, annotate_overrides, non_constant_identifier_names, unnecessary_string_interpolations, prefer_adjacent_string_concatenation, unnecessary_const, dead_code

import 'ModelProvider.dart';
import 'package:amplify_datastore_plugin_interface/amplify_datastore_plugin_interface.dart';
import 'package:amplify_core/amplify_core.dart';
import 'package:flutter/foundation.dart';

/// This is an auto generated class representing the BelongsToChildExplicit type in your schema.
Expand All @@ -36,9 +36,13 @@ class BelongsToChildExplicit extends Model {
@override
getInstanceType() => classType;

@Deprecated(
'[getId] is being deprecated in favor of custom primary key feature. Use getter [modelIdentifier] to get model identifier.')
@override
String getId() {
return id;
String getId() => id;

BelongsToChildExplicitModelIdentifier get modelIdentifier {
return BelongsToChildExplicitModelIdentifier(id: id);
}

String? get name {
Expand Down Expand Up @@ -109,9 +113,9 @@ class BelongsToChildExplicit extends Model {
}

BelongsToChildExplicit copyWith(
{String? id, String? name, BelongsToParent? belongsToParent}) {
{String? name, BelongsToParent? belongsToParent}) {
return BelongsToChildExplicit._internal(
id: id ?? this.id,
id: id,
name: name ?? this.name,
belongsToParent: belongsToParent ?? this.belongsToParent);
}
Expand Down Expand Up @@ -186,3 +190,38 @@ class _BelongsToChildExplicitModelType
return BelongsToChildExplicit.fromJson(jsonData);
}
}

/// This is an auto generated class representing the model identifier
/// of [BelongsToChildExplicit] in your schema.
@immutable
class BelongsToChildExplicitModelIdentifier
implements ModelIdentifier<BelongsToChildExplicit> {
final String id;

/// Create an instance of BelongsToChildExplicitModelIdentifier using [id] the primary key.
const BelongsToChildExplicitModelIdentifier({required this.id});

Map<String, dynamic> serializeAsMap() => (<String, dynamic>{'id': id});

List<Map<String, dynamic>> serializeAsList() => serializeAsMap()
.entries
.map((entry) => (<String, dynamic>{entry.key: entry.value}))
.toList();

String serializeAsString() => serializeAsMap().values.join('#');

@override
String toString() => 'BelongsToChildExplicitModelIdentifier(id: $id)';

@override
bool operator ==(Object other) {
if (identical(this, other)) {
return true;
}

return other is BelongsToChildExplicitModelIdentifier && id == other.id;
}

@override
int get hashCode => id.hashCode;
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
// ignore_for_file: public_member_api_docs, file_names, unnecessary_new, prefer_if_null_operators, prefer_const_constructors, slash_for_doc_comments, annotate_overrides, non_constant_identifier_names, unnecessary_string_interpolations, prefer_adjacent_string_concatenation, unnecessary_const, dead_code

import 'ModelProvider.dart';
import 'package:amplify_datastore_plugin_interface/amplify_datastore_plugin_interface.dart';
import 'package:amplify_core/amplify_core.dart';
import 'package:flutter/foundation.dart';

/// This is an auto generated class representing the BelongsToChildImplicit type in your schema.
Expand All @@ -36,9 +36,13 @@ class BelongsToChildImplicit extends Model {
@override
getInstanceType() => classType;

@Deprecated(
'[getId] is being deprecated in favor of custom primary key feature. Use getter [modelIdentifier] to get model identifier.')
@override
String getId() {
return id;
String getId() => id;

BelongsToChildImplicitModelIdentifier get modelIdentifier {
return BelongsToChildImplicitModelIdentifier(id: id);
}

String? get name {
Expand Down Expand Up @@ -109,9 +113,9 @@ class BelongsToChildImplicit extends Model {
}

BelongsToChildImplicit copyWith(
{String? id, String? name, BelongsToParent? belongsToParent}) {
{String? name, BelongsToParent? belongsToParent}) {
return BelongsToChildImplicit._internal(
id: id ?? this.id,
id: id,
name: name ?? this.name,
belongsToParent: belongsToParent ?? this.belongsToParent);
}
Expand Down Expand Up @@ -186,3 +190,38 @@ class _BelongsToChildImplicitModelType
return BelongsToChildImplicit.fromJson(jsonData);
}
}

/// This is an auto generated class representing the model identifier
/// of [BelongsToChildImplicit] in your schema.
@immutable
class BelongsToChildImplicitModelIdentifier
implements ModelIdentifier<BelongsToChildImplicit> {
final String id;

/// Create an instance of BelongsToChildImplicitModelIdentifier using [id] the primary key.
const BelongsToChildImplicitModelIdentifier({required this.id});

Map<String, dynamic> serializeAsMap() => (<String, dynamic>{'id': id});

List<Map<String, dynamic>> serializeAsList() => serializeAsMap()
.entries
.map((entry) => (<String, dynamic>{entry.key: entry.value}))
.toList();

String serializeAsString() => serializeAsMap().values.join('#');

@override
String toString() => 'BelongsToChildImplicitModelIdentifier(id: $id)';

@override
bool operator ==(Object other) {
if (identical(this, other)) {
return true;
}

return other is BelongsToChildImplicitModelIdentifier && id == other.id;
}

@override
int get hashCode => id.hashCode;
}
Loading

0 comments on commit 8a85689

Please sign in to comment.