Skip to content

Commit 835dce1

Browse files
committed
Updated examples.
1 parent e9c06b7 commit 835dce1

File tree

3 files changed

+97
-9
lines changed

3 files changed

+97
-9
lines changed

example/bin/decoder_example.dart

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,17 @@ class DecoderForClassA extends Decoder<A> {
2929
@override
3030
A read(DartObject obj) {
3131
final id = obj.read<int>(fieldName: 'id');
32-
final names = obj.readSet<String>(fieldName: 'names');
33-
final numbers = obj.readList<num>(fieldName: 'numbers');
32+
final names = obj.read<Set<String>>(fieldName: 'names');
33+
final numbers = obj.read<List<num>>(fieldName: 'numbers');
3434
return A(id: id, names: names, numbers: numbers);
3535
}
3636
}
3737

3838
const decoderForA = DecoderForClassA();
3939

4040
Future<void> main() async {
41-
final library = await resolveSource(
41+
print('\nReading library <example>\n');
42+
final lib = await resolveSource(
4243
r'''
4344
library example;
4445
@@ -54,8 +55,9 @@ Future<void> main() async {
5455
final int id = 124;
5556
final Map<String, int> score = {'Adam': 4, 'Moira': 7};
5657
final isValid = true;
57-
58+
final List<num> numbers = [7, 77.7];
5859
final a = const A(id: 42, names: {'Andy', 'Eva'}, numbers: [42, 3.14]);
60+
final num number = 3;
5961
}
6062
6163
''',
@@ -64,9 +66,13 @@ Future<void> main() async {
6466
);
6567

6668
/// Reading libraries.
67-
print('\nReading library <example>\n');
6869
69-
final lib = library!;
70+
if (lib == null) {
71+
print('Could not read library!');
72+
return;
73+
}
74+
75+
print(Reader.info);
7076

7177
final idObj = lib.classes[1].fields[0].computeConstantValue();
7278
final id = idObj?.read<int>();
@@ -85,11 +91,25 @@ Future<void> main() async {
8591
'Reading a ${'bool'.style(Ansi.green)}: $isValid ${isValid.runtimeType}\n',
8692
);
8793

94+
final numbersObj = lib.classes[1].fields[3].computeConstantValue();
95+
final numbers = numbersObj?.read<List<num>>();
96+
print(
97+
'Reading a ${'List<num>'.style(Ansi.green)}: $numbers ${numbers.runtimeType}\n',
98+
);
99+
88100
/// Adding a decoder:
89101
Reader.addDecoder(decoderForA);
90102

91-
final aObj = lib.classes[1].fields[3].computeConstantValue();
103+
final aObj = lib.classes[1].fields[4].computeConstantValue();
92104
final a = aObj?.read<A>();
93105

94-
print('Reading a constant with type ${'A'.style(Ansi.green)}: $a');
106+
print('Reading a constant with type ${'A'.style(Ansi.green)}: $a\n');
107+
108+
final numberObj = lib.classes[1].fields[5].computeConstantValue();
109+
final number = numberObj?.read();
110+
111+
print('Reading a constant with type ${'num'.style(Ansi.green)}: $number\n');
112+
113+
print(Reader.info);
114+
return;
95115
}

example/bin/enum_example.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import 'package:generic_reader/generic_reader.dart';
1212
enum Order { asc, desc }
1313

1414
Future<void> main() async {
15-
print('\nReading library: example\n');
15+
print('Done importing libaries\n');
16+
print('Reading library: example\n');
1617

1718
final library = await resolveSource(
1819
r'''

example/bin/record_example.dart

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import 'package:analyzer/dart/constant/value.dart' show DartObject;
2+
import 'package:ansi_modifier/ansi_modifier.dart';
3+
import 'package:build_test/build_test.dart' show resolveSource;
4+
import 'package:generic_reader/generic_reader.dart';
5+
6+
/// To run this program navigate to the root folder
7+
/// in your local copy the package `generic_reader` and
8+
/// use the command:
9+
///
10+
/// # dart example/bin/record_example.dart
11+
12+
/// Demonstrates how to use [Reader] to read an [Record].
13+
14+
/// A record with one positional and one named entry.
15+
typedef Info = (int age, {String firstName});
16+
17+
class A {
18+
const A();
19+
final Info info = const (32, firstName: 'Alana');
20+
}
21+
22+
Future<void> main() async {
23+
print('\nReading library: example\n');
24+
25+
final lib = await resolveSource(
26+
r'''
27+
library example;
28+
29+
typedef Info = (int age, {String name});
30+
31+
class A {
32+
const A();
33+
final Info info = const (32, firstName: 'Alana');
34+
}
35+
''',
36+
(resolver) => resolver.findLibraryByName('example'),
37+
readAllSourcesFromFilesystem: false,
38+
);
39+
40+
if (lib == null) return;
41+
42+
// Record factory:
43+
Info infoFactory({
44+
required Map<String, DartObject> named,
45+
required List<DartObject> positional,
46+
}) {
47+
if (!named.containsKey('firstName') || positional.isEmpty) {
48+
throw RecordDecoder.readRecordError();
49+
} else {
50+
final age = positional.first.read<int>();
51+
final firstName = named['firstName']!.read<String>();
52+
return (age, firstName: firstName);
53+
}
54+
}
55+
56+
// Add Record Decoder
57+
Reader.addDecoder<Info>(RecordDecoder<Info>(infoFactory));
58+
59+
final recordObj = lib.classes[0].fields[0].computeConstantValue();
60+
61+
final info = recordObj?.read<Info>();
62+
63+
print(
64+
'\n Reading a record of type ${'$Info'.style(Ansi.green)}: '
65+
'$info\n',
66+
);
67+
}

0 commit comments

Comments
 (0)