Skip to content

Commit 2493b68

Browse files
committed
Removed type_methods/utils.
1 parent d3523f8 commit 2493b68

File tree

7 files changed

+44
-260
lines changed

7 files changed

+44
-260
lines changed

lib/generic_reader.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
export 'src/decoder/core_decoder.dart';
22
export 'src/decoder/decoder.dart';
33
export 'src/reader.dart';
4-
export 'src/extension/type_methods.dart';
54
export 'src/type/decoder_not_found.dart';
65
export 'src/type/invalid_type_argument.dart';

lib/src/decoder/core_decoder.dart

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import 'package:analyzer/dart/constant/value.dart' show DartObject;
2-
import 'package:analyzer/dart/element/type.dart';
32
import 'package:exception_templates/exception_templates.dart' show ErrorOf;
43

5-
import '../extension/type_methods.dart';
64
import 'decoder.dart';
75

86
class BoolDecoder extends Decoder<bool> {
@@ -40,9 +38,9 @@ class NumDecoder extends Decoder<num> {
4038
const NumDecoder();
4139
@override
4240
num read(DartObject obj) {
43-
if (obj.isInt) {
41+
if (obj.type?.isDartCoreInt ?? false) {
4442
return intDecoder.read(obj);
45-
} else if (obj.isDouble) {
43+
} else if (obj.type?.isDartCoreDouble ?? false) {
4644
return doubleDecoder.read(obj);
4745
} else {
4846
throw readError(obj);
@@ -106,35 +104,42 @@ class EnumDecoder<E extends Enum> extends Decoder<E> {
106104
};
107105
}
108106

109-
/// A callback which returns a [Record] given the [positional] and [named]
110-
/// record fields.
111-
typedef RecordFactory<T extends Record> =
112-
T Function({
107+
/// A callback which returns a [Record] of shape/type [R]
108+
/// given the [positional] and [named]
109+
/// record fields as [DartObject]s.
110+
typedef RecordFactory<R extends Record> =
111+
R Function({
113112
required List<DartObject> positional,
114113
required Map<String, DartObject> named,
115114
});
116115

117-
class RecordDecoder<T extends Record> extends Decoder<T> {
116+
/// Typedef representing the [Record] shape returned by
117+
/// the method [DartObject.toRecordValue].
118+
typedef RecordObj = ({
119+
Map<String, DartObject> named,
120+
List<DartObject> positional,
121+
});
122+
123+
/// A decoder that can decode a record of shape/type [R].
124+
class RecordDecoder<R extends Record> extends Decoder<R> {
118125
const RecordDecoder(this.recordFactory);
119126

120127
/// A callback which returns a [Record] given the positional and named
121128
/// record fields.
122-
final RecordFactory<T> recordFactory;
129+
final RecordFactory<R> recordFactory;
123130

124131
@override
125-
/// Override this method and return a [Record] with shape [T]. <br/>
132+
/// Override this method and return a [Record] with shape [R]. <br/>
126133
/// Tip: Use the
127134
/// helper methods [positionalFields] and [namedFields].
128-
T read(DartObject obj) {
129-
if (obj.type is RecordType) {
130-
final recordObject = obj.toRecordValue();
131-
return recordFactory(
132-
positional: recordObject?.positional ?? [],
133-
named: recordObject?.named ?? {},
134-
);
135-
} else {
136-
throw readError(obj);
137-
}
135+
R read(DartObject obj) {
136+
return switch (obj.toRecordValue()) {
137+
RecordObj recordObj => recordFactory(
138+
positional: recordObj.positional,
139+
named: recordObj.named,
140+
),
141+
_ => throw readError(obj),
142+
};
138143
}
139144

140145
static ErrorOf<RecordDecoder<T>> readRecordError<T extends Record>() =>

lib/src/decoder/decoder.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import 'package:analyzer/dart/constant/value.dart';
1+
import 'package:analyzer/dart/constant/value.dart' show DartObject;
22
import 'package:exception_templates/exception_templates.dart' show ErrorOf;
33

44
abstract class Decoder<T> {

lib/src/extension/type_methods.dart

Lines changed: 0 additions & 200 deletions
This file was deleted.

lib/src/reader.dart

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import 'decoder/core_decoder.dart';
88
import 'decoder/decoder.dart';
99
import 'type/decoder_not_found.dart';
1010
import 'type/invalid_field_name.dart';
11-
import 'type/invalid_type_argument.dart' show InvalidTypeArgument;
11+
import 'type/invalid_type_argument.dart';
1212

1313
part 'decoder/collection_decoder.dart';
1414

@@ -262,12 +262,12 @@ extension Reader on DartObject {
262262
/// [ErrorOfType] with type argument [InvalidFieldName] if the field does
263263
/// not exist.
264264
/// * Throws [ErrorOf] with type argument [Decoder] with type argument
265-
/// [List] with type argument [T] if a list can not be constructed.
266-
List<T> readList<T>({String fieldName = ''}) {
267-
if (isNotCoreType<List<T>>()) {
268-
addDecoder<List<T>>(ListDecoder<T>());
265+
/// [List] with type argument [E] if a list can not be constructed.
266+
List<E> readList<E>({String fieldName = ''}) {
267+
if (isNotCoreType<List<E>>()) {
268+
addDecoder<List<E>>(ListDecoder<E>());
269269
}
270-
return read<List<T>>(fieldName: fieldName);
270+
return read<List<E>>(fieldName: fieldName);
271271
}
272272

273273
/// Reads the [DartObject] instance and returns an instance of `List<T>`.
@@ -276,12 +276,12 @@ extension Reader on DartObject {
276276
/// [ErrorOfType] with type argument [InvalidFieldName] if the field does
277277
/// not exist.
278278
/// * Throws [ErrorOf] with type argument [Decoder] with type argument
279-
/// [Iterable] with type argument [T] if an iterable can not be constructed.
280-
Iterable<T> readIterable<T>({String fieldName = ''}) {
281-
if (isNotCoreType<Iterable<T>>()) {
282-
addDecoder<Iterable<T>>(IterableDecoder<T>());
279+
/// [Iterable] with type argument [E] if an iterable can not be constructed.
280+
Iterable<E> readIterable<E>({String fieldName = ''}) {
281+
if (isNotCoreType<Iterable<E>>()) {
282+
addDecoder<Iterable<E>>(IterableDecoder<E>());
283283
}
284-
return read<Iterable<T>>(fieldName: fieldName);
284+
return read<Iterable<E>>(fieldName: fieldName);
285285
}
286286

287287
/// Reads the `dartObject` instance and returns an object of type `Set<T>`.
@@ -290,12 +290,12 @@ extension Reader on DartObject {
290290
/// [ErrorOfType] with type argument [InvalidFieldName] if the field does
291291
/// not exist.
292292
/// * Throws [ErrorOf] with type argument [Decoder] with type argument
293-
/// [Iterable] with type argument [T] if an iterable can not be constructed.
294-
Set<T> readSet<T>({String fieldName = ''}) {
295-
if (isNotCoreType<Set<T>>()) {
296-
addDecoder<Set<T>>(SetDecoder<T>());
293+
/// [Iterable] with type argument [E] if an iterable can not be constructed.
294+
Set<E> readSet<E>({String fieldName = ''}) {
295+
if (isNotCoreType<Set<E>>()) {
296+
addDecoder<Set<E>>(SetDecoder<E>());
297297
}
298-
return read<Set<T>>(fieldName: fieldName);
298+
return read<Set<E>>(fieldName: fieldName);
299299
}
300300

301301
/// Reads the [DartObject] and returns an object of type `Map<K, V>`.

lib/src/type/type_utils.dart

Lines changed: 0 additions & 20 deletions
This file was deleted.

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name: generic_reader
33
description: Methods for the systematic reading of
44
compile-time constant expressions.
55

6-
version: 0.5.1
6+
version: 0.5.2
77

88
homepage: https://github.com/simphotonics/generic_reader
99

0 commit comments

Comments
 (0)