From 0deeebb9d8c71804dc70cbb48bdff30fa2f190ac Mon Sep 17 00:00:00 2001 From: Justin Andresen Date: Sun, 1 Sep 2024 12:45:53 +0200 Subject: [PATCH] Convert `TomlEncodableValue` and `TomlEncodableKey` to pure interfaces --- CHANGELOG.md | 7 +++++++ lib/src/encoder/encodable.dart | 26 ++++++++++++-------------- test/encoder/ast_builder_test.dart | 4 ++-- 3 files changed, 21 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f35f06c..58f2881 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Changelog +## Unreleased + +- `TomlEncodableValue` and `TomlEncodableKey` are now `interface class`es and must not be extended anymore. + - If you have previously extended `TomlEncodableValue`, you must now use the `implements` keyword instead of `extends`. + - If you have previously extended `TomlEncodableKey` and overwrote the `toTomlKey` method, you must now use the `implements` keyword instead of `extends`. + - If you have previously extended `TomlEncodableKey` and used the default implementation of the `toTomlKey` method, you must now mixin `TomlEncodableKeyMixin` instead of extending `TomlEncodableKey`. + ## 0.16.0 - Parsing an invalid local date, local time, local date-time or offset date-time now throws a `TomlInvalidDateTimeException` instead of an `ArgumentError`. diff --git a/lib/src/encoder/encodable.dart b/lib/src/encoder/encodable.dart index 466bf19..552793d 100644 --- a/lib/src/encoder/encodable.dart +++ b/lib/src/encoder/encodable.dart @@ -1,11 +1,7 @@ library toml.src.encoder.encodable; /// Interface for an object which can be encoded as a TOML value or table. -abstract class TomlEncodableValue { - /// Constant default constructor to allow subclasses with `const` - /// constructors. - const TomlEncodableValue(); - +abstract interface class TomlEncodableValue { /// Converts this object to an object which can natively be represented as /// a TOML value or table. /// @@ -20,16 +16,18 @@ abstract class TomlEncodableValue { /// that can be encoded as a TOML key, can always also be encoded as a TOML /// value since keys are strings and strings are TOML values. /// -/// An object can have to different representations depending on whether it is -/// used as a key or a value. By default the object is converted using -/// [toTomlValue] in both cases. You have to `extend` or `mixin` this interface -/// in order to use the default implementation of [toTomlKey]. -abstract class TomlEncodableKey extends TomlEncodableValue { - /// Constant default constructor to allow subclasses with `const` - /// constructors. - const TomlEncodableKey(); - +/// An object can have two different representations depending on whether it is +/// used as a key or a value. Use [TomlEncodableKeyMixin] if the key +/// representation is the same as the value representation. +abstract interface class TomlEncodableKey extends TomlEncodableValue { /// Like [toTomlValue] but is invoked when the object is used as a key /// of a `Map` instead of as a value. + dynamic toTomlKey(); +} + +/// Mixin for [TomlEncodableKey]s whose key representation is the same as +/// their value representation. +mixin TomlEncodableKeyMixin implements TomlEncodableKey { + @override dynamic toTomlKey() => toTomlValue(); } diff --git a/test/encoder/ast_builder_test.dart b/test/encoder/ast_builder_test.dart index 3f8b4df..5f01622 100644 --- a/test/encoder/ast_builder_test.dart +++ b/test/encoder/ast_builder_test.dart @@ -5,7 +5,7 @@ import 'package:toml/toml.dart'; /// A class that is used to test encoding of [TomlEncodableKey] and /// [TomlEncodableValue] objects. -class TomlEncodableWrapper extends TomlEncodableKey { +class TomlEncodableWrapper with TomlEncodableKeyMixin { /// The wrapped value. final dynamic value; @@ -13,7 +13,7 @@ class TomlEncodableWrapper extends TomlEncodableKey { final dynamic key; /// Creates a new wrapper for the given value. - TomlEncodableWrapper(this.value, [this.key]); + const TomlEncodableWrapper(this.value, [this.key]); @override dynamic toTomlValue() => value;