Skip to content

Commit

Permalink
Merge pull request #264 from aws-amplify/non-model-flutter
Browse files Browse the repository at this point in the history
Tag release for read only fields in dart
  • Loading branch information
AaronZyLee authored Oct 15, 2021
2 parents a8e3a70 + 87064c5 commit 8b5549a
Show file tree
Hide file tree
Showing 4 changed files with 329 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5661,3 +5661,283 @@ class _TestModelModelType extends ModelType<TestModel> {
}
}"
`;

exports[`AppSync Dart Visitor Read-only Field Tests should generate the read-only timestamp fields when isTimestampFields is true 1`] = `
"/*
* 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.
*/

// ignore_for_file: public_member_api_docs

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

/** This is an auto generated class representing the SimpleModel type in your schema. */
@immutable
class SimpleModel extends Model {
static const classType = const _SimpleModelModelType();
final String id;
final String name;
final TemporalDateTime createdAt;
final TemporalDateTime updatedAt;

@override
getInstanceType() => classType;

@override
String getId() {
return id;
}

const SimpleModel._internal(
{@required this.id, this.name, this.createdAt, this.updatedAt});

factory SimpleModel({String id, String name}) {
return SimpleModel._internal(
id: id == null ? UUID.getUUID() : id, name: name);
}

bool equals(Object other) {
return this == other;
}

@override
bool operator ==(Object other) {
if (identical(other, this)) return true;
return other is SimpleModel && id == other.id && name == other.name;
}

@override
int get hashCode => toString().hashCode;

@override
String toString() {
var buffer = new StringBuffer();

buffer.write(\\"SimpleModel {\\");
buffer.write(\\"id=\\" + \\"$id\\" + \\", \\");
buffer.write(\\"name=\\" + \\"$name\\" + \\", \\");
buffer.write(\\"createdAt=\\" +
(createdAt != null ? createdAt.format() : \\"null\\") +
\\", \\");
buffer.write(
\\"updatedAt=\\" + (updatedAt != null ? updatedAt.format() : \\"null\\"));
buffer.write(\\"}\\");

return buffer.toString();
}

SimpleModel copyWith({String id, String name}) {
return SimpleModel._internal(id: id ?? this.id, name: name ?? this.name);
}

SimpleModel.fromJson(Map<String, dynamic> json)
: id = json['id'],
name = json['name'],
createdAt = json['createdAt'] != null
? TemporalDateTime.fromString(json['createdAt'])
: null,
updatedAt = json['updatedAt'] != null
? TemporalDateTime.fromString(json['updatedAt'])
: null;

Map<String, dynamic> toJson() => {
'id': id,
'name': name,
'createdAt': createdAt?.format(),
'updatedAt': updatedAt?.format()
};

static final QueryField ID = QueryField(fieldName: \\"simpleModel.id\\");
static final QueryField NAME = QueryField(fieldName: \\"name\\");
static var schema =
Model.defineSchema(define: (ModelSchemaDefinition modelSchemaDefinition) {
modelSchemaDefinition.name = \\"SimpleModel\\";
modelSchemaDefinition.pluralName = \\"SimpleModels\\";

modelSchemaDefinition.addField(ModelFieldDefinition.id());

modelSchemaDefinition.addField(ModelFieldDefinition.field(
key: SimpleModel.NAME,
isRequired: false,
ofType: ModelFieldType(ModelFieldTypeEnum.string)));

modelSchemaDefinition.addField(ModelFieldDefinition.nonQueryField(
fieldName: 'createdAt',
isRequired: false,
isReadOnly: true,
ofType: ModelFieldType(ModelFieldTypeEnum.dateTime)));

modelSchemaDefinition.addField(ModelFieldDefinition.nonQueryField(
fieldName: 'updatedAt',
isRequired: false,
isReadOnly: true,
ofType: ModelFieldType(ModelFieldTypeEnum.dateTime)));
});
}

class _SimpleModelModelType extends ModelType<SimpleModel> {
const _SimpleModelModelType();

@override
SimpleModel fromJson(Map<String, dynamic> jsonData) {
return SimpleModel.fromJson(jsonData);
}
}
"
`;

exports[`AppSync Dart Visitor Read-only and Null Safety Combined Tests should generate the read-only timestamp fields when isTimestampFields is true and with null safety 1`] = `
"/*
* 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.
*/

// ignore_for_file: public_member_api_docs

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


/** This is an auto generated class representing the SimpleModel type in your schema. */
@immutable
class SimpleModel extends Model {
static const classType = const _SimpleModelModelType();
final String id;
final String? _name;
final TemporalDateTime? _createdAt;
final TemporalDateTime? _updatedAt;

@override
getInstanceType() => classType;

@override
String getId() {
return id;
}

String? get name {
return _name;
}

TemporalDateTime? get createdAt {
return _createdAt;
}

TemporalDateTime? get updatedAt {
return _updatedAt;
}

const SimpleModel._internal({required this.id, name, createdAt, updatedAt}): _name = name, _createdAt = createdAt, _updatedAt = updatedAt;

factory SimpleModel({String? id, String? name}) {
return SimpleModel._internal(
id: id == null ? UUID.getUUID() : id,
name: name);
}

bool equals(Object other) {
return this == other;
}

@override
bool operator ==(Object other) {
if (identical(other, this)) return true;
return other is SimpleModel &&
id == other.id &&
_name == other._name;
}

@override
int get hashCode => toString().hashCode;

@override
String toString() {
var buffer = new StringBuffer();

buffer.write(\\"SimpleModel {\\");
buffer.write(\\"id=\\" + \\"$id\\" + \\", \\");
buffer.write(\\"name=\\" + \\"$_name\\" + \\", \\");
buffer.write(\\"createdAt=\\" + (_createdAt != null ? _createdAt!.format() : \\"null\\") + \\", \\");
buffer.write(\\"updatedAt=\\" + (_updatedAt != null ? _updatedAt!.format() : \\"null\\"));
buffer.write(\\"}\\");

return buffer.toString();
}

SimpleModel copyWith({String? id, String? name}) {
return SimpleModel._internal(
id: id ?? this.id,
name: name ?? this.name);
}

SimpleModel.fromJson(Map<String, dynamic> json)
: id = json['id'],
_name = json['name'],
_createdAt = json['createdAt'] != null ? TemporalDateTime.fromString(json['createdAt']) : null,
_updatedAt = json['updatedAt'] != null ? TemporalDateTime.fromString(json['updatedAt']) : null;

Map<String, dynamic> toJson() => {
'id': id, 'name': _name, 'createdAt': _createdAt?.format(), 'updatedAt': _updatedAt?.format()
};

static final QueryField ID = QueryField(fieldName: \\"simpleModel.id\\");
static final QueryField NAME = QueryField(fieldName: \\"name\\");
static var schema = Model.defineSchema(define: (ModelSchemaDefinition modelSchemaDefinition) {
modelSchemaDefinition.name = \\"SimpleModel\\";
modelSchemaDefinition.pluralName = \\"SimpleModels\\";

modelSchemaDefinition.addField(ModelFieldDefinition.id());

modelSchemaDefinition.addField(ModelFieldDefinition.field(
key: SimpleModel.NAME,
isRequired: false,
ofType: ModelFieldType(ModelFieldTypeEnum.string)
));

modelSchemaDefinition.addField(ModelFieldDefinition.nonQueryField(
fieldName: 'createdAt',
isRequired: false,
isReadOnly: true,
ofType: ModelFieldType(ModelFieldTypeEnum.dateTime)
));

modelSchemaDefinition.addField(ModelFieldDefinition.nonQueryField(
fieldName: 'updatedAt',
isRequired: false,
isReadOnly: true,
ofType: ModelFieldType(ModelFieldTypeEnum.dateTime)
));
});
}

class _SimpleModelModelType extends ModelType<SimpleModel> {
const _SimpleModelModelType();

@override
SimpleModel fromJson(Map<String, dynamic> jsonData) {
return SimpleModel.fromJson(jsonData);
}
}"
`;
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ const getVisitor = (
selectedType?: string,
generate: CodeGenGenerateEnum = CodeGenGenerateEnum.code,
enableDartNullSafety: boolean = false,
enableDartNonModelGeneration: boolean = true
enableDartNonModelGeneration: boolean = true,
isTimestampFieldsAdded: boolean = false,
) => {
const ast = parse(schema);
const builtSchema = buildSchemaWithDirectives(schema);
const visitor = new AppSyncModelDartVisitor(
builtSchema,
{ directives, target: 'dart', scalars: DART_SCALAR_MAP, enableDartNullSafety, enableDartNonModelGeneration },
{ directives, target: 'dart', scalars: DART_SCALAR_MAP, enableDartNullSafety, enableDartNonModelGeneration, isTimestampFieldsAdded },
{ selectedType, generate },
);
visit(ast, { leave: visitor });
Expand Down Expand Up @@ -518,4 +519,32 @@ describe('AppSync Dart Visitor', () => {
expect(generatedCode).toMatchSnapshot();
});
});

describe('Read-only Field Tests', () => {
it('should generate the read-only timestamp fields when isTimestampFields is true', () => {
const schema = /* GraphQL */ `
type SimpleModel @model {
id: ID!
name: String
}
`;
const visitor = getVisitor(schema, undefined, CodeGenGenerateEnum.code, false, false, true);
const generatedCode = visitor.generate();
expect(generatedCode).toMatchSnapshot();
});
});

describe('Read-only and Null Safety Combined Tests', () => {
it('should generate the read-only timestamp fields when isTimestampFields is true and with null safety', () => {
const schema = /* GraphQL */ `
type SimpleModel @model {
id: ID!
name: String
}
`;
const visitor = getVisitor(schema, undefined, CodeGenGenerateEnum.code, true, false, true);
const generatedCode = visitor.generate();
expect(generatedCode).toMatchSnapshot();
});
});
});
Loading

0 comments on commit 8b5549a

Please sign in to comment.