Skip to content

Commit 2ad6827

Browse files
committed
Updated docs and examples.
1 parent 7ae5fba commit 2ad6827

File tree

4 files changed

+42
-58
lines changed

4 files changed

+42
-58
lines changed

README.md

Lines changed: 39 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -28,27 +28,25 @@ To use the package [`generic_reader`][generic_reader] the following steps are re
2828
your pubspec.yaml file.
2929

3030
2. Register a [Decoder][Decoder] object for each *user defined*
31-
data-type `T` that is going to be read.
31+
data-type `T` that is going to be read. <br/>
3232
Note: The following type are supported out-of-the-box and do *not* require a decoder:
3333
* `bool`, `double`, `int`, `num`,`String`, `Type`, `Symbol`,
3434
* `List<bool>`, `List<double>`, `List<int>`, `List<num>`,
35-
`List<String>`,`List<Symbol`,`List<Type`,
36-
* `Set<bool`, `Set<double`, `Set<int`, `Set<num`, `Set<String`, `Set<Symbol`,
37-
`Set<Type`,
38-
* `Iterable<bool`,`Iterable<double`,`Iterable<int`,`Iterable<num`,`Iterable<String`,
39-
`Iterable<Symbol`,`Iterable<Type`.
35+
`List<String>`,`List<Symbol>`,`List<Type>`,
36+
* `Set<bool>`, `Set<double>`, `Set<int>`, `Set<num>`, `Set<String>`, `Set<Symbol>`,
37+
`Set<Type>`,
38+
* `Iterable<bool>`,`Iterable<double>`,`Iterable<int>`,`Iterable<num>`,`Iterable<String>`,
39+
`Iterable<Symbol>`,`Iterable<Type>`.
4040

4141
3. Use Dart's static [`analyzer`][analyzer] to read a library, get
4242
the relevant [`VariableElement`][VariableElement], and calculate the constant
43-
expression represented by a [`DartObject`][DartObject] using the method [`computeConstantValue()`][computeConstantValue()].
43+
expression represented by a [`DartObject`][DartObject]
44+
using the method [`computeConstantValue()`][computeConstantValue()].
4445

4546
4. Read the compile-time constant values using the extension method: [`read<T>`][read]. <br/>
4647

47-
To read constant of a user-defined type `U`, add a suitable `Decoder<U>`:
48-
```Dart
49-
Reader.addDecoder<U>(decoder);
50-
51-
```
48+
To read a constant of a user-defined type `U`, add a suitable `Decoder<U`
49+
(see section [Custom Decoders](#custom-decoders));
5250

5351
To read a constant representing a *collection* of a *user-defined* type `U`
5452
use the convenience methods [`readList<U>`][readList],
@@ -60,7 +58,7 @@ expression represented by a [`DartObject`][DartObject] using the method [`comput
6058
5. Use the constant values to generate the source-code and complete the building
6159
process.
6260

63-
## Decoder
61+
## Custom Decoders
6462

6563
The extension [`Reader`][Reader] provides a systematic method of
6664
retrieving constants of
@@ -69,13 +67,13 @@ arbitrary data-types by allowing users to register `Decoder` objects.
6967
`Decoder<T>` is an abstract
7068
parameterized class with a method `T read<T>(DartObject obj)`
7169
that attempt to read a variable of type `T` from `obj` and return the result.
72-
The example below demonstrates how to create a custom decode for the
70+
The example below demonstrates how to create a custom decoder for the
7371
sample class `Annotation` and register an instance of the decoder with
7472
the extension [`Reader`][Reader].
7573

7674
```Dart
7775
import 'package:generic_reader/generic_reader.dart';
78-
76+
// An annotation with a const constructor
7977
class Annotation {
8078
const A({required this.id, required this.names,);
8179
final int id;
@@ -91,16 +89,18 @@ class AnnotationDecoder extends Decoder<Annotation> {
9189
@override
9290
Annotation read(DartObject obj) {
9391
final id = obj.read<int>(fieldName: 'id');
94-
final names = obj.readSet<String>(fieldName: 'names');
92+
final names = obj.read<Set<String>>(fieldName: 'names');
9593
return A(id: id, names: names);
9694
}
9795
}
98-
99-
Read.addDecoder(const AnnotationDecoder());
96+
// Registering the decoder with the reader
97+
Reader.addDecoder(const AnnotationDecoder());
10098
```
10199

102-
The example below show how to register a decoder for a Dart `Enum` and read
103-
an instance of the enumeration. In this case, instead of creating a custom
100+
## Reading an Enumeration
101+
102+
The example below show how to register and read an instance of an enumeration.
103+
In this case, instead of creating a custom
104104
decoder class we register an instance of the already defined generic class
105105
[`EnumDecoder`][EnumDecoder]:
106106

@@ -155,46 +155,40 @@ Future<void> main() async {
155155
</details>
156156

157157

158-
<br/>
158+
## Reading a Nested List
159159

160-
The program listed below show how to read a constant of type
160+
The program listed below is available in the folder [example][example] and
161+
shows how to read a constant of type
161162
`List<List<String>>`:
162163

163-
<details> <summary> Click to show source-code. </summary>
164-
165164
```Dart
166165
import 'package:ansi_modifier/ansi_modifier.dart';
167166
import 'package:build_test/build_test.dart' show resolveSource;
168167
import 'package:generic_reader/generic_reader.dart';
169168
170169
/// Demonstrates how to use [Reader] to read a nested list.
171-
Future<void> main() async {
172-
print('\nReading library: example\n');
173-
174-
final lib = await resolveSource(
175-
r'''
170+
final libraryString = r'''
176171
library example;
177172
178173
class A {
179174
const A();
180175
final nestedList = List<List<String>> [['a'], ['b']];
181176
}
182-
''',
177+
'''
178+
179+
Future<void> main() async {
180+
print('\nReading library: example\n');
181+
182+
final lib = await resolveSource(libraryString,
183183
(resolver) => resolver.findLibraryByName('example'),
184184
readAllSourcesFromFilesystem: false,
185185
);
186186
187187
if (lib == null) return;
188188
189-
final listOfString = 'List<String>'.style(Ansi.green);
190-
final listOfListOfString = 'List<List<String>>'.style(Ansi.green);
191-
192-
print('\nAdding decoder for $listOfString and $listOfListOfString\n');
193-
Reader.addDecoder(const ListDecoder<String>());
189+
print('\nAdding decoder for List<List<String>>\n');
194190
Reader.addDecoder(const ListDecoder<List<String>>());
195191
196-
print(Reader.info);
197-
198192
final listObj = lib.classes[0].fields[0].computeConstantValue();
199193
final list1 = listObj?.read<List<List<String>>>();
200194
final list2 = listObj?.read();
@@ -207,12 +201,6 @@ Future<void> main() async {
207201
print('\nlistObj.readList<$listOfString>(): $list3\n');
208202
}
209203
```
210-
211-
</details>
212-
213-
214-
<br/>
215-
216204
The program above produces the following terminal output:
217205

218206

@@ -228,14 +216,7 @@ Reading library: example
228216
1s _ResolveSourceBuilder<LibraryElement?> on 5 inputs: 5 no-op
229217
Built with build_runner in 1s; wrote 0 outputs.
230218
231-
Adding decoder for List<String> and List<List<String>>
232-
233-
234-
Reader:
235-
Decodable types: [bool, double, int, num, String, Symbol, Type,
236-
List<bool>, ..., Iterable<Type>, List<List<String>>]
237-
Resolved types: {}
238-
219+
Adding decoder for List<List<String>>
239220
240221
listObj.read<List<List<String>>>: [[a], [b]]
241222
@@ -246,11 +227,15 @@ listObj.readList<List<String>>(): [[a], [b]]
246227
```
247228
</details>
248229

230+
249231
## Limitations
250232

251-
1) Constants retrievable with [`Reader`][Reader] must have
252-
a built-in Dart type, a type made available by depending on
253-
a package, or a type defined in the file being read.
233+
1) When using the type `dynamic` the static type of the [DartObject][DartObject]
234+
is used to determine the correct type of the runtime object. If a suitable
235+
decoder is registered with the [Reader][Reader] on can omit the type parameter
236+
when using e.g. [`read`][read]. For example, the variable `list2` in section
237+
[Reading a Nested List](#reading-a-nested-list) is calculated using
238+
[read][read].
254239

255240
2) Defining decoder functions for each data-type has its obvious limitiations when it comes to *generic types*.
256241
In practice, however, generic classes are often designed in such a manner

example/bin/enum_example.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ Future<void> main() async {
3737
final enum0 = enumObj?.read<Order>();
3838

3939
print(
40-
'\n mroe Reading an enum of type ${'Order'.style(Ansi.green)}: '
40+
'\n Reading an enum with type ${'Order'.style(Ansi.green)}: '
4141
'$enum0\n',
4242
);
4343
}

example/bin/list_example.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ Future<void> main() async {
3030
final listOfString = 'List<String>'.style(Ansi.green);
3131
final listOfListOfString = 'List<List<String>>'.style(Ansi.green);
3232

33-
print('\nAdding decoder for $listOfString and $listOfListOfString\n');
34-
Reader.addDecoder(const ListDecoder<String>());
33+
print('\nAdding decoder for $listOfListOfString\n');
3534
Reader.addDecoder(const ListDecoder<List<String>>());
3635

3736
print(Reader.info);

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@ dependencies:
2121

2222
dev_dependencies:
2323
ansi_modifier: ^0.1.5
24-
build_test: ^3.4.1
24+
build_test: ^3.5.0
2525
lints: ^6.0.0
2626
test: ^1.26.3

0 commit comments

Comments
 (0)