Skip to content

Use log from pkg:build, tiny refactor #142

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 2 commits into from
Apr 21, 2018
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
2 changes: 2 additions & 0 deletions json_serializable/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## 0.5.1

* Use `log` exposed by `package:build`. This requires end-users to have at least
`package:build_runner` `^0.8.2`.
* Updated minimum `package:source_gen` dependency to `0.8.1` which includes
improved error messages.

Expand Down
21 changes: 10 additions & 11 deletions json_serializable/lib/builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,9 @@
library json_serializable.builder;

import 'package:build/build.dart';
import 'package:logging/logging.dart';

import 'src/json_part_builder.dart';

// TODO: until we can use `log` here - github.com/dart-lang/build/issues/1223
final _logger = new Logger('json_serializable');

/// Supports `package:build_runner` creation and configuration of `build_cli`.
///
/// Not meant to be invoked by hand-authored code.
Expand All @@ -28,13 +24,16 @@ Builder jsonSerializable(BuilderOptions options) {
// elsewhere.
var optionsMap = new Map<String, dynamic>.from(options.config);

try {
return jsonPartBuilder(
header: optionsMap.remove('header') as String,
useWrappers: optionsMap.remove('use_wrappers') as bool);
} finally {
if (optionsMap.isNotEmpty) {
_logger.warning('These options were ignored: `$optionsMap`.');
var builder = jsonPartBuilder(
header: optionsMap.remove('header') as String,
useWrappers: optionsMap.remove('use_wrappers') as bool);

if (optionsMap.isNotEmpty) {
if (log == null) {
throw new StateError('Upgrade `build_runner` to at least 0.8.2.');
} else {
log.warning('These options were ignored: `$optionsMap`.');
}
}
return builder;
}
10 changes: 5 additions & 5 deletions json_serializable/lib/src/generator_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class _GeneratorHelper {
// We do this now, since we have a final field list after any pruning done
// by `_writeCtor`.
accessibleFields.fold(new Set<String>(), (Set<String> set, fe) {
var jsonKey = jsonKeyFor(fe).name ?? fe.name;
var jsonKey = _nameAccess(fe);
if (!set.add(jsonKey)) {
throw new InvalidGenerationSourceError(
'More than one field has the JSON key `$jsonKey`.',
Expand Down Expand Up @@ -321,10 +321,10 @@ void $toJsonMapHelperName(String key, dynamic value) {
jsonKeyFor(field).nullable ?? _annotation.nullable;
}

String _safeNameAccess(FieldElement field) {
var name = jsonKeyFor(field).name ?? field.name;
return escapeDartString(name);
}
String _nameAccess(FieldElement field) => jsonKeyFor(field).name ?? field.name;

String _safeNameAccess(FieldElement field) =>
escapeDartString(_nameAccess(field));

JsonSerializable _valueForAnnotation(ConstantReader annotation) =>
new JsonSerializable(
Expand Down
1 change: 0 additions & 1 deletion json_serializable/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ dependencies:
# Use a tight version constraint to ensure that a constraint on
# `json_annotation`. Properly constrains all features it provides.
json_annotation: '>=0.2.3 <0.2.4'
logging: ^0.11.3
path: ^1.3.2
source_gen: '>=0.8.1 <0.9.0'
dev_dependencies:
Expand Down