From babf09625d8077618c86450c4b103755e76d8346 Mon Sep 17 00:00:00 2001 From: Eric Schneller Date: Thu, 20 Sep 2018 08:56:36 +0200 Subject: [PATCH] Refactor missing exception and prepare for `1.0.0-alpha+3` --- changelog.md => CHANGELOG.md | 2 +- README.md | 3 ++- lib/src/exceptions.dart | 18 ++++++++++++++++++ lib/src/generator/entity_generator.dart | 4 ++-- pubspec.yaml | 2 +- 5 files changed, 24 insertions(+), 5 deletions(-) rename changelog.md => CHANGELOG.md (98%) diff --git a/changelog.md b/CHANGELOG.md similarity index 98% rename from changelog.md rename to CHANGELOG.md index 59e393b..998fc07 100644 --- a/changelog.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ # Changelog -## 1.0.0-alpha+3 +## 1.0.0-alpha+3 (09/20/2018) - Add support for final properties - Add support to skip private properties diff --git a/README.md b/README.md index 7564ead..1927081 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,8 @@ [![Build Status](https://travis-ci.org/eredo/dartson.svg?branch=master)](https://travis-ci.org/eredo/dartson) [![Coverage Status](https://coveralls.io/repos/github/eredo/dartson/badge.svg)](https://coveralls.io/github/eredo/dartson) -**Dartson 1.0.0 is currently in alpha. The public API might be subject to change.** +**Dartson 1.0.0 is currently in alpha. The public API might be subject to change. For further details of +potential breaks and a roadmap take a look at [project 1.0.0](https://github.com/eredo/dartson/projects/1).** Dartson is a dart library which converts Dart Objects into their JSON representation. It helps you keep your code clean of `fromJSON` and `toJSON` functions by providing a builder which generates the serialization methods. diff --git a/lib/src/exceptions.dart b/lib/src/exceptions.dart index e607ab6..a124628 100644 --- a/lib/src/exceptions.dart +++ b/lib/src/exceptions.dart @@ -1,3 +1,5 @@ +import 'package:analyzer/dart/element/element.dart'; + /// Thrown when encode or decode method is called with a dart object that /// is not available within the entities list of the serializer. class UnknownEntityException implements Exception { @@ -38,3 +40,19 @@ class MissingDecodeMethodException implements Exception { 'TypeTransformers: $type. Please make sure to implement TypeTransformer ' 'and overwrite the interface methods.'; } + +/// Thrown by the generator in case a not optional argument within an entity +/// constructor is marked as ignored and therefore the generator cannot fill in +/// a value. +class NotOptionalArgumentIgnoredException implements Exception { + final Element field; + final ClassElement type; + + NotOptionalArgumentIgnoredException(this.field, this.type); + + @override + String toString() => + 'NotOptionalArgumentIgnoredException: The field ${field.name} in ' + '${type.name} is annotated with @Property(ignore:true) but is a not ' + 'optional argument of the constructor.'; +} diff --git a/lib/src/generator/entity_generator.dart b/lib/src/generator/entity_generator.dart index 24f53dd..d0fd888 100644 --- a/lib/src/generator/entity_generator.dart +++ b/lib/src/generator/entity_generator.dart @@ -7,6 +7,7 @@ import 'package:json_serializable/src/type_helpers/iterable_helper.dart'; import 'package:json_serializable/src/type_helpers/map_helper.dart'; import '../annotations.dart'; +import '../exceptions.dart'; import 'entity_type_helper.dart'; import 'field_context.dart'; import 'identifier.dart'; @@ -100,8 +101,7 @@ class EntityGenerator { } if (fieldProperty.ignore && field.isNotOptional) { - // TODO: Throw proper error. - throw 'NotOptional marked as ignored'; + throw NotOptionalArgumentIgnoredException(field, _element); } if (fieldProperty.ignore) { diff --git a/pubspec.yaml b/pubspec.yaml index fa660a0..8d5463a 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,5 +1,5 @@ name: dartson -version: 1.0.0-alpha+2 +version: 1.0.0-alpha+3 author: Eric Schneller description: Dartson is a Dart library that can be used to serialize Dart objects into a JSON string and vice versa. It uses the new builder infrastructure.