Skip to content

Commit

Permalink
Convert TomlEncodableValue and TomlEncodableKey to pure interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
just95 committed Sep 1, 2024
1 parent 51c5fdd commit 0deeebb
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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`.
Expand Down
26 changes: 12 additions & 14 deletions lib/src/encoder/encodable.dart
Original file line number Diff line number Diff line change
@@ -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.
///
Expand All @@ -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();
}
4 changes: 2 additions & 2 deletions test/encoder/ast_builder_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ 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;

/// The wrapped key or `null` if it does not differ from the [value].
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;
Expand Down

0 comments on commit 0deeebb

Please # to comment.