Create a Fields
class
This commit is contained in:
parent
ea9322a930
commit
039f0b3443
11 changed files with 163 additions and 41 deletions
7
.idea/runConfigurations/watch_dart.xml
Normal file
7
.idea/runConfigurations/watch_dart.xml
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
<component name="ProjectRunConfigurationManager">
|
||||||
|
<configuration default="false" name="watch.dart" type="DartCommandLineRunConfigurationType" factoryName="Dart Command Line Application" singleton="true" nameIsGenerated="true">
|
||||||
|
<option name="filePath" value="$PROJECT_DIR$/angel_serialize_generator/tool/watch.dart" />
|
||||||
|
<option name="workingDirectory" value="$PROJECT_DIR$/angel_serialize_generator" />
|
||||||
|
<method />
|
||||||
|
</configuration>
|
||||||
|
</component>
|
12
README.md
12
README.md
|
@ -84,6 +84,18 @@ myFunction() {
|
||||||
// For compatibility with `JSON.encode`, a `toJson` method
|
// For compatibility with `JSON.encode`, a `toJson` method
|
||||||
// is included that forwards to `BookSerializer.toMap`:
|
// is included that forwards to `BookSerializer.toMap`:
|
||||||
expect(book.toJson(), map);
|
expect(book.toJson(), map);
|
||||||
|
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
As of `2.0.2`, the generated output also includes information
|
||||||
|
about the serialized names of keys on your model class.
|
||||||
|
|
||||||
|
```dart
|
||||||
|
myOtherFunction() {
|
||||||
|
// Relying on the serialized key of a field? No worries.
|
||||||
|
map[BookFields.author] = 'Zora Neale Hurston';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
## Customizing Serialization
|
## Customizing Serialization
|
||||||
|
|
|
@ -1,3 +1,7 @@
|
||||||
|
# 2.0.2
|
||||||
|
* Generates an `XFields` class with the serialized names of
|
||||||
|
all fields in a model class `X`.
|
||||||
|
|
||||||
# 2.0.1
|
# 2.0.1
|
||||||
* Ensured that `List` is only transformed if
|
* Ensured that `List` is only transformed if
|
||||||
it generically references a `Model`.
|
it generically references a `Model`.
|
|
@ -25,6 +25,7 @@ class SerializerGenerator extends GeneratorForAnnotation<Serializable> {
|
||||||
|
|
||||||
var lib = new Library((b) {
|
var lib = new Library((b) {
|
||||||
generateClass(serializers.map((s) => s.toIntValue()).toList(), ctx, b);
|
generateClass(serializers.map((s) => s.toIntValue()).toList(), ctx, b);
|
||||||
|
generateFieldsClass(ctx, b);
|
||||||
});
|
});
|
||||||
|
|
||||||
var buf = lib.accept(new DartEmitter());
|
var buf = lib.accept(new DartEmitter());
|
||||||
|
@ -183,4 +184,23 @@ class SerializerGenerator extends GeneratorForAnnotation<Serializable> {
|
||||||
method.body = new Code(buf.toString());
|
method.body = new Code(buf.toString());
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void generateFieldsClass(BuildContext ctx, LibraryBuilder file) {
|
||||||
|
file.body.add(new Class((clazz) {
|
||||||
|
clazz
|
||||||
|
..abstract = true
|
||||||
|
..name = '${ctx.modelClassNameRecase.pascalCase}Fields';
|
||||||
|
|
||||||
|
for (var field in ctx.fields) {
|
||||||
|
clazz.fields.add(new Field((b) {
|
||||||
|
b
|
||||||
|
..static = true
|
||||||
|
..modifier = FieldModifier.constant
|
||||||
|
..type = new Reference('String')
|
||||||
|
..name = field.name
|
||||||
|
..assignment = new Code("'${ctx.resolveFieldName(field.name)}'");
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
name: angel_serialize_generator
|
name: angel_serialize_generator
|
||||||
version: 2.0.1
|
version: 2.0.2
|
||||||
description: Model serialization generators, designed for use with Angel. Combine with angel_serialize for flexible modeling.
|
description: Model serialization generators, designed for use with Angel. Combine with angel_serialize for flexible modeling.
|
||||||
author: Tobe O <thosakwe@gmail.com>
|
author: Tobe O <thosakwe@gmail.com>
|
||||||
homepage: https://github.com/angel-dart/serialize
|
homepage: https://github.com/angel-dart/serialize
|
||||||
|
|
|
@ -48,6 +48,26 @@ abstract class AuthorSerializer {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
abstract class AuthorFields {
|
||||||
|
static const String id = 'id';
|
||||||
|
|
||||||
|
static const String name = 'name';
|
||||||
|
|
||||||
|
static const String age = 'age';
|
||||||
|
|
||||||
|
static const String books = 'books';
|
||||||
|
|
||||||
|
static const String newestBook = 'newest_book';
|
||||||
|
|
||||||
|
static const String secret = 'secret';
|
||||||
|
|
||||||
|
static const String obscured = 'obscured';
|
||||||
|
|
||||||
|
static const String createdAt = 'created_at';
|
||||||
|
|
||||||
|
static const String updatedAt = 'updated_at';
|
||||||
|
}
|
||||||
|
|
||||||
abstract class LibrarySerializer {
|
abstract class LibrarySerializer {
|
||||||
static Library fromMap(Map map,
|
static Library fromMap(Map map,
|
||||||
{String id,
|
{String id,
|
||||||
|
@ -81,3 +101,13 @@ abstract class LibrarySerializer {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
abstract class LibraryFields {
|
||||||
|
static const String id = 'id';
|
||||||
|
|
||||||
|
static const String collection = 'collection';
|
||||||
|
|
||||||
|
static const String createdAt = 'created_at';
|
||||||
|
|
||||||
|
static const String updatedAt = 'updated_at';
|
||||||
|
}
|
||||||
|
|
|
@ -44,3 +44,21 @@ abstract class BookSerializer {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
abstract class BookFields {
|
||||||
|
static const String id = 'id';
|
||||||
|
|
||||||
|
static const String author = 'author';
|
||||||
|
|
||||||
|
static const String title = 'title';
|
||||||
|
|
||||||
|
static const String description = 'description';
|
||||||
|
|
||||||
|
static const String pageCount = 'page_count';
|
||||||
|
|
||||||
|
static const String notModels = 'not_models';
|
||||||
|
|
||||||
|
static const String createdAt = 'created_at';
|
||||||
|
|
||||||
|
static const String updatedAt = 'updated_at';
|
||||||
|
}
|
||||||
|
|
|
@ -1,32 +0,0 @@
|
||||||
/*
|
|
||||||
import 'package:build_runner/build_runner.dart';
|
|
||||||
import 'package:source_gen/source_gen.dart';
|
|
||||||
import 'package:angel_serialize_generator/angel_serialize_generator.dart';
|
|
||||||
|
|
||||||
final List<BuildAction> actions = [
|
|
||||||
jsonModel(const ['test/models/book.dart']),
|
|
||||||
jsonModel(const ['test/models/author.dart']),
|
|
||||||
angelSerialize(const ['test/models/book.dart']),
|
|
||||||
angelSerialize(const ['test/models/author.dart']),
|
|
||||||
];
|
|
||||||
|
|
||||||
BuildAction jsonModel(List<String> inputs) {
|
|
||||||
return new BuildAction(
|
|
||||||
new PartBuilder([const JsonModelGenerator()]),
|
|
||||||
'angel_serialize_generator',
|
|
||||||
inputs: inputs,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
BuildAction angelSerialize(List<String> inputs) {
|
|
||||||
return new BuildAction(
|
|
||||||
new PartBuilder(
|
|
||||||
[const SerializerGenerator()],
|
|
||||||
generatedExtension: '.serializer.g.dart',
|
|
||||||
),
|
|
||||||
'angel_serialize_generator',
|
|
||||||
inputs: inputs,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
*/
|
|
65
angel_serialize_generator/tool/applications.dart
Normal file
65
angel_serialize_generator/tool/applications.dart
Normal file
|
@ -0,0 +1,65 @@
|
||||||
|
import 'package:angel_serialize_generator/angel_serialize_generator.dart';
|
||||||
|
import 'package:build_config/build_config.dart';
|
||||||
|
import 'package:build_runner/build_runner.dart';
|
||||||
|
import 'package:source_gen/source_gen.dart';
|
||||||
|
|
||||||
|
const InputSet standalone =
|
||||||
|
const InputSet(include: const ['test/models/book.dart']);
|
||||||
|
const InputSet dependent =
|
||||||
|
const InputSet(include: const ['test/models/author.dart']);
|
||||||
|
const InputSet all = const InputSet(include: const ['test/models/*.dart']);
|
||||||
|
|
||||||
|
final List<BuilderApplication> applications = [
|
||||||
|
applyToRoot(
|
||||||
|
new PartBuilder([
|
||||||
|
const JsonModelGenerator(),
|
||||||
|
]),
|
||||||
|
generateFor: standalone,
|
||||||
|
),
|
||||||
|
applyToRoot(
|
||||||
|
new PartBuilder([
|
||||||
|
const JsonModelGenerator(),
|
||||||
|
]),
|
||||||
|
generateFor: dependent,
|
||||||
|
),
|
||||||
|
applyToRoot(
|
||||||
|
new PartBuilder(
|
||||||
|
[const SerializerGenerator()],
|
||||||
|
generatedExtension: '.serializer.g.dart',
|
||||||
|
),
|
||||||
|
generateFor: all,
|
||||||
|
),
|
||||||
|
];
|
||||||
|
|
||||||
|
/*
|
||||||
|
import 'package:build_runner/build_runner.dart';
|
||||||
|
import 'package:source_gen/source_gen.dart';
|
||||||
|
import 'package:angel_serialize_generator/angel_serialize_generator.dart';
|
||||||
|
|
||||||
|
final List<BuildAction> actions = [
|
||||||
|
jsonModel(const ['test/models/book.dart']),
|
||||||
|
jsonModel(const ['test/models/author.dart']),
|
||||||
|
angelSerialize(const ['test/models/book.dart']),
|
||||||
|
angelSerialize(const ['test/models/author.dart']),
|
||||||
|
];
|
||||||
|
|
||||||
|
BuildAction jsonModel(List<String> inputs) {
|
||||||
|
return new BuildAction(
|
||||||
|
new PartBuilder([const JsonModelGenerator()]),
|
||||||
|
'angel_serialize_generator',
|
||||||
|
inputs: inputs,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
BuildAction angelSerialize(List<String> inputs) {
|
||||||
|
return new BuildAction(
|
||||||
|
new PartBuilder(
|
||||||
|
[const SerializerGenerator()],
|
||||||
|
generatedExtension: '.serializer.g.dart',
|
||||||
|
),
|
||||||
|
'angel_serialize_generator',
|
||||||
|
inputs: inputs,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
*/
|
|
@ -1,5 +1,4 @@
|
||||||
/*import 'package:build_runner/build_runner.dart';
|
import 'package:build_runner/build_runner.dart';
|
||||||
import 'actions.dart';
|
import 'applications.dart';
|
||||||
|
|
||||||
main() => build(actions, deleteFilesByDefault: true);
|
main() => build(applications, deleteFilesByDefault: true, verbose: false);
|
||||||
*/
|
|
|
@ -1,5 +1,4 @@
|
||||||
/*import 'package:build_runner/build_runner.dart';
|
import 'package:build_runner/build_runner.dart';
|
||||||
import 'actions.dart';
|
import 'applications.dart';
|
||||||
|
|
||||||
main() => watch(actions, deleteFilesByDefault: true);
|
main() => watch(applications, deleteFilesByDefault: true, verbose: false);
|
||||||
*/
|
|
||||||
|
|
Loading…
Reference in a new issue