Skip to content

Upgrade source_gen to 0.7.0 #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jul 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.2.1

* Upgrade to `package:source_gen` v0.7.0

## 0.2.0+1

* When serializing classes that implement their own `fromJson` constructor,
Expand Down
12 changes: 0 additions & 12 deletions example/example.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions lib/src/json_literal_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ class JsonLiteralGenerator extends GeneratorForAnnotation<JsonLiteral> {

@override
Future<String> generateForAnnotatedElement(
Element element, JsonLiteral annotation, BuildStep buildStep) async {
if (p.isAbsolute(annotation.path)) {
Element element, ConstantReader annotation, BuildStep buildStep) async {
if (p.isAbsolute(annotation.read('path').stringValue)) {
throw new ArgumentError(
'`annotation.path` must be relative path to the source file.');
}

var sourcePathDir = p.dirname(buildStep.inputId.path);
var fileId = new AssetId(
buildStep.inputId.package, p.join(sourcePathDir, annotation.path));
var fileId = new AssetId(buildStep.inputId.package,
p.join(sourcePathDir, annotation.read('path').stringValue));
var content = JSON.decode(await buildStep.readAsString(fileId));

var thing = JSON.encode(content);
Expand Down
13 changes: 7 additions & 6 deletions lib/src/json_serializable_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class JsonSerializableGenerator

@override
Future<String> generateForAnnotatedElement(
Element element, JsonSerializable annotation, _) async {
Element element, ConstantReader annotation, _) async {
if (element is! ClassElement) {
var friendlyName = friendlyNameForElement(element);
throw new InvalidGenerationSourceError(
Expand Down Expand Up @@ -104,7 +104,7 @@ class JsonSerializableGenerator

var buffer = new StringBuffer();

if (annotation.createFactory) {
if (annotation.read('createFactory').boolValue) {
var toSkip = _writeFactory(buffer, classElement, fields, prefix);

// If there are fields that are final – that are not set via the generated
Expand All @@ -114,7 +114,7 @@ class JsonSerializableGenerator
}
}

if (annotation.createToJson) {
if (annotation.read('createToJson').boolValue) {
//
// Generate the mixin class
//
Expand All @@ -127,14 +127,15 @@ class JsonSerializableGenerator
buffer.writeln(' ${field.type} get $name;');
});

var includeIfNull = annotation.read('includeIfNull').boolValue;

buffer.writeln(' Map<String, dynamic> toJson() ');
if (fieldsList
.every((e) => _includeIfNull(e, annotation.includeIfNull))) {
if (fieldsList.every((e) => _includeIfNull(e, includeIfNull))) {
// write simple `toJson` method that includes all keys...
_writeToJsonSimple(buffer, fields);
} else {
// At least one field should be excluded if null
_writeToJsonWithNullChecks(buffer, fields, annotation.includeIfNull);
_writeToJsonWithNullChecks(buffer, fields, includeIfNull);
}

// end of the mixin class
Expand Down
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: json_serializable
version: 0.2.0+1
version: 0.2.1
author: Dart Team <misc@dartlang.org>
description: Generates utilities to aid in serializing to/from JSON.
homepage: https://github.com/dart-lang/json_serializable
Expand All @@ -10,7 +10,7 @@ dependencies:
build: ^0.9.0
cli_util: ^0.1.0
path: ^1.3.2
source_gen: ^0.6.1+1
source_gen: ^0.7.0
dev_dependencies:
build_runner: ^0.3.2
build_test: ^0.6.0
Expand Down
21 changes: 7 additions & 14 deletions test/json_serializable_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import 'package:analyzer/src/string_source.dart';
import 'package:json_serializable/generators.dart';
import 'package:json_serializable/src/utils.dart';
import 'package:path/path.dart' as p;
import 'package:source_gen/source_gen.dart';
import 'package:test/test.dart';

import 'analysis_utils.dart';
Expand Down Expand Up @@ -97,12 +98,6 @@ void main() {
expect(generateResult, contains("Map<String, dynamic> toJson()"));
});

test('unannotated classes no-op', () async {
var output = await _runForElementNamed('NoAnnotation');

expect(output, isNull);
});

group('valid inputs', () {
test('class with no fields', () async {
var output = await _runForElementNamed('Person');
Expand Down Expand Up @@ -172,11 +167,12 @@ void main() {

const _generator = const JsonSerializableGenerator();

Future<String> _runForElementNamed(String name) {
var library = _compUnit.element.library;
var element =
getElementsFromLibraryElement(library).singleWhere((e) => e.name == name);
return _generator.generate(element, null);
Future<String> _runForElementNamed(String name) async {
var library = new LibraryReader(_compUnit.element.library);
var element = library.allElements.singleWhere((e) => e.name == name);
var annotation = _generator.typeChecker.firstAnnotationOf(element);
return _generator.generateForAnnotatedElement(
element, new ConstantReader(annotation), null);
}

Future<CompilationUnit> _getCompilationUnitForString(String projectPath) async {
Expand Down Expand Up @@ -222,9 +218,6 @@ class Order {
Order(this.height, String firstName, [this.lastName]);
}

class NoAnnotation {
}

@JsonSerializable()
class FinalFields {
final int a;
Expand Down
1 change: 0 additions & 1 deletion test/test_files/bathtub.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 0 additions & 11 deletions test/test_files/json_test_example.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion test/test_files/kitchen_sink.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tool/phases.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import 'package:json_serializable/generators.dart';
import 'package:source_gen/source_gen.dart';

final PhaseGroup phases = new PhaseGroup.singleAction(
new GeneratorBuilder(const [
new PartBuilder(const [
const JsonSerializableGenerator(),
const JsonLiteralGenerator()
]),
Expand Down