@@ -56,27 +56,81 @@ void validateDynamicModule(
5656 }
5757}
5858
59+ extension on YamlMap {
60+ void verifyKeys (Set <String > allowedKeys) {
61+ for (dynamic k in keys) {
62+ if (! allowedKeys.contains (k.toString ())) {
63+ // Coverage-ignore-block(suite): Not run.
64+ throw 'Unexpected key "$k " in dynamic interface specification' ;
65+ }
66+ }
67+ }
68+ }
69+ 70+ /// Loaded contents of dynamic interface specification yaml file.
71+ class DynamicInterfaceYamlFile {
72+ final YamlNode _root;
73+ 74+ DynamicInterfaceYamlFile (String contents) : _root = loadYamlNode (contents) {
75+ if (! isEmpty) {
76+ sections.verifyKeys (const {
77+ 'extendable' ,
78+ 'can-be-overridden' ,
79+ 'callable' ,
80+ });
81+ }
82+ }
83+ 84+ bool get isEmpty => _root is ! YamlMap ;
85+ 86+ YamlMap get sections => _root as YamlMap ;
87+ 88+ YamlList ? get extendable => sections['extendable' ];
89+ YamlList ? get canBeOverridden => sections['can-be-overridden' ];
90+ YamlList ? get callable => sections['callable' ];
91+ 92+ // Coverage-ignore(suite): Not run.
93+ Set <String > get libraries => {
94+ for (YamlList section in [? extendable, ? canBeOverridden, ? callable])
95+ for (YamlNode item in section) (item as YamlMap )['library' ] as String ,
96+ };
97+ 98+ // Coverage-ignore(suite): Not run.
99+ Iterable <Uri > getUserLibraryUris (Uri baseUri) => libraries
100+ .where ((String lib) => ! lib.startsWith ('dart:' ))
101+ .map ((String lib) => baseUri.resolve (lib));
102+ }
103+ 59104/// Parsed dynamic interface specification yaml file.
60105class DynamicInterfaceSpecification {
61106 // Specified Library, Class and Member nodes.
62107 final Set <TreeNode > extendable = {};
63108 final Set <TreeNode > canBeOverridden = {};
64109 final Set <TreeNode > callable = {};
65110
66- DynamicInterfaceSpecification (
111+ factory DynamicInterfaceSpecification (
67112 String dynamicInterfaceSpecification,
68113 Uri baseUri,
69114 Component component,
115+ ) => new DynamicInterfaceSpecification .fromYamlFile (
116+ new DynamicInterfaceYamlFile (dynamicInterfaceSpecification),
117+ baseUri,
118+ component,
119+ );
120+ 121+ DynamicInterfaceSpecification .fromYamlFile (
122+ DynamicInterfaceYamlFile yamlFile,
123+ Uri baseUri,
124+ Component component,
70125 ) {
71- final YamlNode spec = loadYamlNode (dynamicInterfaceSpecification);
72- final LibraryIndex libraryIndex = new LibraryIndex .all (component);
126+ if (yamlFile.isEmpty) {
127+ return ;
128+ }
73129
74- // If the spec is empty, the result is a scalar and not a map.
75- if (spec is ! YamlMap ) return ;
76- _verifyKeys (spec, const {'extendable' , 'can-be-overridden' , 'callable' });
130+ final LibraryIndex libraryIndex = new LibraryIndex .all (component);
77131
78132 _parseList (
79- spec[ ' extendable' ] ,
133+ yamlFile. extendable,
80134 extendable,
81135 baseUri,
82136 component,
@@ -86,7 +140,7 @@ class DynamicInterfaceSpecification {
86140 );
87141
88142 _parseList (
89- spec[ 'can-be-overridden' ] ,
143+ yamlFile.canBeOverridden ,
90144 canBeOverridden,
91145 baseUri,
92146 component,
@@ -96,7 +150,7 @@ class DynamicInterfaceSpecification {
96150 );
97151
98152 _parseList (
99- spec[ ' callable' ] ,
153+ yamlFile. callable,
100154 callable,
101155 baseUri,
102156 component,
@@ -130,15 +184,6 @@ class DynamicInterfaceSpecification {
130184 }
131185 }
132186
133- void _verifyKeys (YamlMap map, Set <String > allowedKeys) {
134- for (dynamic k in map.keys) {
135- if (! allowedKeys.contains (k.toString ())) {
136- // Coverage-ignore-block(suite): Not run.
137- throw 'Unexpected key "$k " in dynamic interface specification' ;
138- }
139- }
140- }
141- 142187 void findNodes (
143188 YamlNode yamlNode,
144189 Set <TreeNode > result,
@@ -151,9 +196,9 @@ class DynamicInterfaceSpecification {
151196 final YamlMap yamlMap = yamlNode as YamlMap ;
152197 final bool allowMembers = allowStaticMembers || allowInstanceMembers;
153198 if (allowMembers) {
154- _verifyKeys ( yamlMap, const {'library' , 'member' });
199+ yamlMap. verifyKeys ( const {'library' , 'class' , 'member' });
155200 } else {
156- _verifyKeys ( yamlMap, const {'library' , 'class' });
201+ yamlMap. verifyKeys ( const {'library' , 'class' });
157202 }
158203
159204 final String librarySpec = yamlMap['library' ] as String ;
@@ -163,7 +208,7 @@ class DynamicInterfaceSpecification {
163208 final dynamic yamlClassNode = yamlMap['class' ];
164209 if (yamlClassNode is YamlList ) {
165210 // Coverage-ignore-block(suite): Not run.
166- _verifyKeys ( yamlMap, const {'library' , 'class' });
211+ yamlMap. verifyKeys ( const {'library' , 'class' });
167212 for (dynamic c in yamlClassNode) {
168213 result.add (libraryIndex.getClass (libraryUri, c as String ));
169214 }
0 commit comments