Skip to content

Port over implementation #1

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
Jul 19, 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
38 changes: 38 additions & 0 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
analyzer:
strong-mode: true
errors:
unused_element: error
unused_import: error
unused_local_variable: error
dead_code: error
linter:
rules:
# Errors
- avoid_empty_else
- comment_references
- control_flow_in_finally
- empty_statements
#- hash_and_equals
- implementation_imports
- test_types_in_equals
- throw_in_finally
- unrelated_type_equality_checks
- valid_regexps

# Style
#- annotate_overrides
- avoid_init_to_null
- avoid_return_types_on_setters
- await_only_futures
- camel_case_types
- directives_ordering
- empty_catches
- empty_constructor_bodies
- library_names
- library_prefixes
- non_constant_identifier_names
- only_throw_errors
- prefer_final_fields
- prefer_is_not_empty
- slash_for_doc_comments
- type_init_formals
9 changes: 9 additions & 0 deletions lib/annotations.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
// 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.

// A separate library to allow using the annotations without transitive
// imports of the analyzer etc.

export 'src/json_literal.dart';
export 'src/json_serializable.dart';
9 changes: 9 additions & 0 deletions lib/json_serializable.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
// 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.

export 'src/json_literal.dart';
export 'src/json_literal_generator.dart';
export 'src/json_serializable.dart';
export 'src/json_serializable_generator.dart';
export 'src/type_helper.dart';
10 changes: 10 additions & 0 deletions lib/src/json_literal.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
// 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.

// TODO: option to make the literal a const
class JsonLiteral {
final String path;

const JsonLiteral(this.path);
}
41 changes: 41 additions & 0 deletions lib/src/json_literal_generator.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
// 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.

import 'dart:async';
import 'dart:convert';

import 'package:analyzer/dart/element/element.dart';
import 'package:build/build.dart';
import 'package:path/path.dart' as p;
import 'package:source_gen/source_gen.dart';

import 'json_literal.dart';

class JsonLiteralGenerator extends GeneratorForAnnotation<JsonLiteral> {
const JsonLiteralGenerator();

@override
Future<String> generateForAnnotatedElement(
Element element, JsonLiteral annotation, BuildStep buildStep) async {
if (p.isAbsolute(annotation.path)) {
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 content = JSON.decode(await buildStep.readAsString(fileId));

var thing = JSON.encode(content);

var marked = _isConstType(content) ? 'const' : 'final';

return '$marked _\$${element.displayName}JsonLiteral = $thing;';
}
}

bool _isConstType(value) {
return value == null || value is String || value is num || value is bool;
}
21 changes: 21 additions & 0 deletions lib/src/json_serializable.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
// 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.

class JsonSerializable {
final bool createFactory;
final bool createToJson;

const JsonSerializable({bool createFactory: true, bool createToJson: true})
: this.createFactory = createFactory,
this.createToJson = createToJson;
}

/// Customizes the name of the JSON key for a field.
///
/// If omitted, the resulting JSON key will be the
/// name of the field defined on the class.
class JsonKey {
final String jsonName;
const JsonKey(this.jsonName);
}
Loading