Skip to content

Commit c41449d

Browse files
authored
Upgrade source_gen to 0.7.0 (#17)
- Update for breaking changes. - Switch tests to call by library instead of element.
1 parent 07c9136 commit c41449d

10 files changed

+25
-52
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.2.1
2+
3+
* Upgrade to `package:source_gen` v0.7.0
4+
15
## 0.2.0+1
26

37
* When serializing classes that implement their own `fromJson` constructor,

example/example.g.dart

Lines changed: 0 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/src/json_literal_generator.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ class JsonLiteralGenerator extends GeneratorForAnnotation<JsonLiteral> {
1717

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

2626
var sourcePathDir = p.dirname(buildStep.inputId.path);
27-
var fileId = new AssetId(
28-
buildStep.inputId.package, p.join(sourcePathDir, annotation.path));
27+
var fileId = new AssetId(buildStep.inputId.package,
28+
p.join(sourcePathDir, annotation.read('path').stringValue));
2929
var content = JSON.decode(await buildStep.readAsString(fileId));
3030

3131
var thing = JSON.encode(content);

lib/src/json_serializable_generator.dart

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class JsonSerializableGenerator
5353

5454
@override
5555
Future<String> generateForAnnotatedElement(
56-
Element element, JsonSerializable annotation, _) async {
56+
Element element, ConstantReader annotation, _) async {
5757
if (element is! ClassElement) {
5858
var friendlyName = friendlyNameForElement(element);
5959
throw new InvalidGenerationSourceError(
@@ -104,7 +104,7 @@ class JsonSerializableGenerator
104104

105105
var buffer = new StringBuffer();
106106

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

110110
// If there are fields that are final – that are not set via the generated
@@ -114,7 +114,7 @@ class JsonSerializableGenerator
114114
}
115115
}
116116

117-
if (annotation.createToJson) {
117+
if (annotation.read('createToJson').boolValue) {
118118
//
119119
// Generate the mixin class
120120
//
@@ -127,14 +127,15 @@ class JsonSerializableGenerator
127127
buffer.writeln(' ${field.type} get $name;');
128128
});
129129

130+
var includeIfNull = annotation.read('includeIfNull').boolValue;
131+
130132
buffer.writeln(' Map<String, dynamic> toJson() ');
131-
if (fieldsList
132-
.every((e) => _includeIfNull(e, annotation.includeIfNull))) {
133+
if (fieldsList.every((e) => _includeIfNull(e, includeIfNull))) {
133134
// write simple `toJson` method that includes all keys...
134135
_writeToJsonSimple(buffer, fields);
135136
} else {
136137
// At least one field should be excluded if null
137-
_writeToJsonWithNullChecks(buffer, fields, annotation.includeIfNull);
138+
_writeToJsonWithNullChecks(buffer, fields, includeIfNull);
138139
}
139140

140141
// end of the mixin class

pubspec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: json_serializable
2-
version: 0.2.0+1
2+
version: 0.2.1
33
author: Dart Team <misc@dartlang.org>
44
description: Generates utilities to aid in serializing to/from JSON.
55
homepage: https://github.com/dart-lang/json_serializable
@@ -10,7 +10,7 @@ dependencies:
1010
build: ^0.9.0
1111
cli_util: ^0.1.0
1212
path: ^1.3.2
13-
source_gen: ^0.6.1+1
13+
source_gen: ^0.7.0
1414
dev_dependencies:
1515
build_runner: ^0.3.2
1616
build_test: ^0.6.0

test/json_serializable_test.dart

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import 'package:analyzer/src/string_source.dart';
1212
import 'package:json_serializable/generators.dart';
1313
import 'package:json_serializable/src/utils.dart';
1414
import 'package:path/path.dart' as p;
15+
import 'package:source_gen/source_gen.dart';
1516
import 'package:test/test.dart';
1617

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

100-
test('unannotated classes no-op', () async {
101-
var output = await _runForElementNamed('NoAnnotation');
102-
103-
expect(output, isNull);
104-
});
105-
106101
group('valid inputs', () {
107102
test('class with no fields', () async {
108103
var output = await _runForElementNamed('Person');
@@ -172,11 +167,12 @@ void main() {
172167

173168
const _generator = const JsonSerializableGenerator();
174169

175-
Future<String> _runForElementNamed(String name) {
176-
var library = _compUnit.element.library;
177-
var element =
178-
getElementsFromLibraryElement(library).singleWhere((e) => e.name == name);
179-
return _generator.generate(element, null);
170+
Future<String> _runForElementNamed(String name) async {
171+
var library = new LibraryReader(_compUnit.element.library);
172+
var element = library.allElements.singleWhere((e) => e.name == name);
173+
var annotation = _generator.typeChecker.firstAnnotationOf(element);
174+
return _generator.generateForAnnotatedElement(
175+
element, new ConstantReader(annotation), null);
180176
}
181177

182178
Future<CompilationUnit> _getCompilationUnitForString(String projectPath) async {
@@ -222,9 +218,6 @@ class Order {
222218
Order(this.height, String firstName, [this.lastName]);
223219
}
224220
225-
class NoAnnotation {
226-
}
227-
228221
@JsonSerializable()
229222
class FinalFields {
230223
final int a;

test/test_files/bathtub.g.dart

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/test_files/json_test_example.g.dart

Lines changed: 0 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/test_files/kitchen_sink.g.dart

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tool/phases.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import 'package:json_serializable/generators.dart';
77
import 'package:source_gen/source_gen.dart';
88

99
final PhaseGroup phases = new PhaseGroup.singleAction(
10-
new GeneratorBuilder(const [
10+
new PartBuilder(const [
1111
const JsonSerializableGenerator(),
1212
const JsonLiteralGenerator()
1313
]),

0 commit comments

Comments
 (0)