@@ -6,7 +6,7 @@ import 'dart:convert';
6
6
7
7
import 'package:collection/collection.dart' ;
8
8
9
- Map <Object , Model > _roots = Map .identity ();
9
+ Map <Object , Uris > _uris = Map .identity ();
10
10
Map <Object , String > _names = Map .identity ();
11
11
12
12
final class QualifiedName {
@@ -35,26 +35,9 @@ final class QualifiedName {
35
35
}
36
36
37
37
extension type Model .fromJson (Map <String , Object ?> node) {
38
- Model () : this .fromJson ({});
38
+ Model () : this .fromJson ({'uris' : < String , Object ? > {} });
39
39
40
- Iterable <String > get uris => node.keys;
41
- Iterable <Library > get libraries => node.values.cast ();
42
-
43
- Library ? library (String uri) => node[uri] as Library ? ;
44
- Scope ? scope (QualifiedName qualifiedName) =>
45
- library (qualifiedName.uri)? .scope (qualifiedName.name);
46
-
47
- void ensure (String name) {
48
- if (node.containsKey (name)) return ;
49
- add (name, Library ());
50
- }
51
-
52
- void add (String name, Library library) {
53
- if (node.containsKey (name)) throw ArgumentError ('Already present: $name ' );
54
- _names[library] = name;
55
- _roots[library] = this ;
56
- node[name] = library;
57
- }
40
+ Uris get uris => node['uris' ] as Uris ;
58
41
59
42
bool hasPath (Path path) {
60
43
if (path.path.length == 1 ) {
@@ -132,6 +115,27 @@ extension type Model.fromJson(Map<String, Object?> node) {
132
115
String prettyPrint () => const JsonEncoder .withIndent (' ' ).convert (node);
133
116
}
134
117
118
+ extension type Uris .fromJson (Map <String , Object ?> node) {
119
+ Iterable <String > get uris => node.keys;
120
+ Iterable <Library > get libraries => node.values.cast ();
121
+
122
+ Library ? library (String uri) => node[uri] as Library ? ;
123
+ Scope ? scope (QualifiedName qualifiedName) =>
124
+ library (qualifiedName.uri)? .scope (qualifiedName.name);
125
+
126
+ void ensure (String name) {
127
+ if (node.containsKey (name)) return ;
128
+ add (name, Library ());
129
+ }
130
+
131
+ void add (String name, Library library) {
132
+ if (node.containsKey (name)) throw ArgumentError ('Already present: $name ' );
133
+ _names[library] = name;
134
+ _uris[library] = this ;
135
+ node[name] = library;
136
+ }
137
+ }
138
+
135
139
extension type Library .fromJson (Map <String , Object ?> node) implements Object {
136
140
Library () : this .fromJson ({});
137
141
@@ -142,7 +146,7 @@ extension type Library.fromJson(Map<String, Object?> node) implements Object {
142
146
143
147
void add (String name, Scope scope) {
144
148
_names[scope] = name;
145
- _roots [scope] = _roots [this ]! ;
149
+ _uris [scope] = _uris [this ]! ;
146
150
node[name] = scope;
147
151
}
148
152
}
@@ -163,7 +167,10 @@ extension type Class.fromJson(Map<String, Object?> node) implements Scope {
163
167
Iterable <QualifiedName >? interfaces,
164
168
Map <String , Member >? members})
165
169
: this .fromJson ({
166
- 'properties' : ['class' , if (abstract == true ) 'abstract' ],
170
+ 'properties' : {
171
+ 'class' : true ,
172
+ if (abstract != null ) 'abstract' : abstract
173
+ },
167
174
if (annotations != null ) 'annotations' : annotations.toList (),
168
175
if (supertype != null ) 'supertype' : supertype.toString (),
169
176
if (interfaces != null )
@@ -177,14 +184,14 @@ extension type Class.fromJson(Map<String, Object?> node) implements Scope {
177
184
extension type Interface .fromJson (Map <String , Object ?> node) implements Scope {
178
185
String get name => _names[this ]! ;
179
186
180
- bool get isClass => (node['properties' ] as List ). contains ( 'class' ) ;
181
- bool get isAbstract => (node['properties' ] as List ). contains ( 'abstract' ) ;
187
+ bool get isClass => (node['properties' ] as Map )[ 'class' ] as bool ;
188
+ bool get isAbstract => (node['properties' ] as Map )[ 'abstract' ] as bool ;
182
189
183
190
Interface ? get supertype {
184
191
if (! node.containsKey ('supertype' )) return null ;
185
192
final name = QualifiedName .tryParse (node['supertype' ] as String );
186
193
if (name == null ) return null ;
187
- return _roots [this ]! .scope (name)? .asInterface;
194
+ return _uris [this ]! .scope (name)? .asInterface;
188
195
}
189
196
190
197
List <Annotation > get annotations => (node['annotations' ] as List ).cast ();
@@ -195,7 +202,7 @@ extension type Interface.fromJson(Map<String, Object?> node) implements Scope {
195
202
final result = < Interface > [];
196
203
for (final interface in (node['interfaces' ] as List ).cast <String >()) {
197
204
final name = QualifiedName .tryParse (interface )! ;
198
- result.add (_roots [this ]! .scope (name)! .asInterface! );
205
+ result.add (_uris [this ]! .scope (name)! .asInterface! );
199
206
}
200
207
return result;
201
208
}
@@ -218,24 +225,24 @@ extension type Member.fromJson(Map<String, Object?> node) {
218
225
required bool static ,
219
226
required bool synthetic})
220
227
: this .fromJson ({
221
- 'properties' : [
222
- if ( abstract ) 'abstract' ,
223
- if (getter) 'getter' ,
224
- if (method) 'method' ,
225
- if (field) 'field' ,
226
- if ( static ) 'static' ,
227
- if (synthetic) 'synthetic'
228
- ]
228
+ 'properties' : {
229
+ 'abstract' : abstract ,
230
+ 'getter' : getter ,
231
+ 'method' : method ,
232
+ 'field' : field ,
233
+ 'static' : static ,
234
+ 'synthetic' : synthetic,
235
+ }
229
236
});
230
237
231
238
String get name => _names[this ]! ;
232
239
233
- bool get isAbstract => (node['properties' ] as List ). contains ( 'abstract' ) ;
234
- bool get isField => (node['properties' ] as List ). contains ( 'field' ) ;
235
- bool get isGetter => (node['properties' ] as List ). contains ( 'getter' ) ;
236
- bool get isMethod => (node['properties' ] as List ). contains ( 'method' ) ;
237
- bool get isStatic => (node['properties' ] as List ). contains ( 'static' ) ;
238
- bool get isSynthetic => (node['properties' ] as List ). contains ( 'synthetic' ) ;
240
+ bool get isAbstract => (node['properties' ] as Map )[ 'abstract' ] as bool ;
241
+ bool get isField => (node['properties' ] as Map )[ 'field' ] as bool ;
242
+ bool get isGetter => (node['properties' ] as Map )[ 'getter' ] as bool ;
243
+ bool get isMethod => (node['properties' ] as Map )[ 'method' ] as bool ;
244
+ bool get isStatic => (node['properties' ] as Map )[ 'static' ] as bool ;
245
+ bool get isSynthetic => (node['properties' ] as Map )[ 'synthetic' ] as bool ;
239
246
}
240
247
241
248
extension type Annotation .fromJson (Map <String , Object ?> node) {
0 commit comments