Skip to content

Prepare for 0.1.0 #15

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 1 commit into from
Jul 20, 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
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ make it simple to serialize to and from JSON.
## Example

Given a library `example.dart` with an `Person` class annotated with
`@JsonSerializable`:
`@JsonSerializable()`:

```dart
library source_gen.example;
library json_serializable.example;

import 'package:source_gen/generators/json_serializable.dart';
import 'package:json_serializable/annotations.dart';
part 'example.g.dart';

@JsonSerializable()
Expand All @@ -30,14 +30,14 @@ class Person extends Object with _$PersonSerializerMixin {
`source_gen` creates the corresponding part `example.g.dart`:

```dart
part of source_gen.example;

Person _$PersonFromJson(Map json) => new Person(
json['firstName'], json['lastName'],
middleName: json['middleName'],
dateOfBirth: json['date-of-birth'] == null
? null
: DateTime.parse(json['date-of-birth']));
part of json_serializable.example;

Person _$PersonFromJson(Map json) =>
new Person(json['firstName'] as String, json['lastName'] as String,
middleName: json['middleName'] as String,
dateOfBirth: json['date-of-birth'] == null
? null
: DateTime.parse(json['date-of-birth']));

abstract class _$PersonSerializerMixin {
String get firstName;
Expand Down
3 changes: 1 addition & 2 deletions example/example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

library source_gen.example.example;
library json_serializable.example;

import 'package:json_serializable/annotations.dart';

part 'example.g.dart';

@JsonSerializable()
Expand Down
2 changes: 1 addition & 1 deletion example/example.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 pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: json_serializable
version: 0.1.0-dev
version: 0.1.0
author: Dart Team <misc@dartlang.org>
description: Generates utilities to aid in serializing to/from JSON.
homepage: https://github.com/dart-lang/json_serialization
Expand Down
7 changes: 3 additions & 4 deletions test/json_serializable_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// BSD-style license that can be found in the LICENSE file.

@TestOn('!browser')
library source_gen.test.json_generator_test;
library json_serializable.test.json_generator_test;

import 'dart:async';

Expand All @@ -12,9 +12,8 @@ import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/src/generated/source.dart';
import 'package:analyzer/src/generated/source_io.dart';
import 'package:analyzer/src/string_source.dart';
import 'package:json_serializable/json_serializable.dart';
import 'package:path/path.dart' as p;
import 'package:source_gen/generators/json_serializable_generator.dart';
import 'package:source_gen/src/utils.dart';
import 'package:test/test.dart';

import 'analysis_utils.dart';
Expand Down Expand Up @@ -141,7 +140,7 @@ Future<CompilationUnit> _getCompilationUnitForString(String projectPath) async {
CompilationUnit _compUnit;

const _testSource = r'''
import 'package:source_gen/generators/json_serializable.dart';
import 'package:json_serializable/json_serializable.dart';

@JsonSerializable()
const theAnswer = 42;
Expand Down
2 changes: 1 addition & 1 deletion test/test_files/json_test_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

library source_gen.test.example;
library json_serializable.test.example;

import 'dart:collection';

Expand Down
2 changes: 1 addition & 1 deletion 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.

29 changes: 29 additions & 0 deletions test/test_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

import 'dart:mirrors';

import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/standard_resolution_map.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:path/path.dart' as p;
import 'package:source_gen/source_gen.dart';
import 'package:test/test.dart';
Expand Down Expand Up @@ -36,3 +39,29 @@ class _InvalidGenerationSourceError extends TypeMatcher {
@override
bool matches(item, Map matchState) => item is InvalidGenerationSourceError;
}

/// Returns all of the declarations in [unit], including [unit] as the first
/// item.
Iterable<Element> getElementsFromLibraryElement(LibraryElement unit) sync* {
yield unit;
for (var cu in unit.units) {
for (var compUnitMember in cu.unit.declarations) {
yield* _getElements(compUnitMember);
}
}
}

Iterable<Element> _getElements(CompilationUnitMember member) {
if (member is TopLevelVariableDeclaration) {
return member.variables.variables
.map(resolutionMap.elementDeclaredByVariableDeclaration);
}
var element = resolutionMap.elementDeclaredByDeclaration(member);

if (element == null) {
print([member, member.runtimeType, member.element]);
throw new Exception('Could not find any elements for the provided unit.');
}

return [element];
}