OLD | NEW |
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 import 'dart:io'; | 5 import 'dart:io'; |
6 | 6 |
7 import 'package:boolean_selector/boolean_selector.dart'; | 7 import 'package:boolean_selector/boolean_selector.dart'; |
8 import 'package:glob/glob.dart'; | 8 import 'package:glob/glob.dart'; |
9 import 'package:path/path.dart' as p; | 9 import 'package:path/path.dart' as p; |
10 import 'package:source_span/source_span.dart'; | 10 import 'package:source_span/source_span.dart'; |
(...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
283 _getValue(field, "boolean", (value) => value is bool); | 283 _getValue(field, "boolean", (value) => value is bool); |
284 | 284 |
285 /// Asserts that [field] is a string and returns its value. | 285 /// Asserts that [field] is a string and returns its value. |
286 String _getString(String field) => | 286 String _getString(String field) => |
287 _getValue(field, "string", (value) => value is String); | 287 _getValue(field, "string", (value) => value is String); |
288 | 288 |
289 /// Asserts that [field] is a list and runs [forElement] for each element it | 289 /// Asserts that [field] is a list and runs [forElement] for each element it |
290 /// contains. | 290 /// contains. |
291 /// | 291 /// |
292 /// Returns a list of values returned by [forElement]. | 292 /// Returns a list of values returned by [forElement]. |
293 List _getList(String field, forElement(YamlNode elementNode)) { | 293 List/*<T>*/ _getList/*<T>*/(String field, |
294 var node = _getNode(field, "list", (value) => value is List); | 294 /*=T*/ forElement(YamlNode elementNode)) { |
| 295 var node = _getNode(field, "list", (value) => value is List) as YamlList; |
295 if (node == null) return []; | 296 if (node == null) return []; |
296 return node.nodes.map(forElement).toList(); | 297 return node.nodes.map(forElement).toList(); |
297 } | 298 } |
298 | 299 |
299 /// Asserts that [field] is a map and runs [key] and [value] for each pair. | 300 /// Asserts that [field] is a map and runs [key] and [value] for each pair. |
300 /// | 301 /// |
301 /// Returns a map with the keys and values returned by [key] and [value]. Each | 302 /// Returns a map with the keys and values returned by [key] and [value]. Each |
302 /// of these defaults to asserting that the value is a string. | 303 /// of these defaults to asserting that the value is a string. |
303 Map _getMap(String field, {key(YamlNode keyNode), | 304 Map/*<K, V>*/ _getMap/*<K, V>*/(String field, {/*=K*/ key(YamlNode keyNode), |
304 value(YamlNode valueNode)}) { | 305 /*=V*/ value(YamlNode valueNode)}) { |
305 var node = _getNode(field, "map", (value) => value is Map); | 306 var node = _getNode(field, "map", (value) => value is Map) as YamlMap; |
306 if (node == null) return {}; | 307 if (node == null) return {}; |
307 | 308 |
308 key ??= (keyNode) { | 309 key ??= (keyNode) { |
309 _validate(keyNode, "$field keys must be strings.", | 310 _validate(keyNode, "$field keys must be strings.", |
310 (value) => value is String); | 311 (value) => value is String); |
311 | 312 |
312 return keyNode.value; | 313 return keyNode.value as dynamic/*=K*/; |
313 }; | 314 }; |
314 | 315 |
315 value ??= (valueNode) { | 316 value ??= (valueNode) { |
316 _validate(valueNode, "$field values must be strings.", | 317 _validate(valueNode, "$field values must be strings.", |
317 (value) => value is String); | 318 (value) => value is String); |
318 | 319 |
319 return valueNode.value; | 320 return valueNode.value as dynamic/*=V*/; |
320 }; | 321 }; |
321 | 322 |
322 return mapMap(node.nodes, | 323 return mapMap(node.nodes, |
323 key: (keyNode, _) => key(keyNode), | 324 key: (keyNode, _) => key(keyNode), |
324 value: (_, valueNode) => value(valueNode)); | 325 value: (_, valueNode) => value(valueNode)); |
325 } | 326 } |
326 | 327 |
327 /// Verifies that [node]'s value is an optionally hyphenated Dart identifier, | 328 /// Verifies that [node]'s value is an optionally hyphenated Dart identifier, |
328 /// and returns it | 329 /// and returns it |
329 String _parseIdentifierLike(YamlNode node, String name) { | 330 String _parseIdentifierLike(YamlNode node, String name) { |
330 _validate(node, "$name must be a string.", (value) => value is String); | 331 _validate(node, "$name must be a string.", (value) => value is String); |
331 _validate( | 332 _validate( |
332 node, | 333 node, |
333 "$name must be an (optionally hyphenated) Dart identifier.", | 334 "$name must be an (optionally hyphenated) Dart identifier.", |
334 (value) => value.contains(anchoredHyphenatedIdentifier)); | 335 (value) => value.contains(anchoredHyphenatedIdentifier)); |
335 return node.value; | 336 return node.value; |
336 } | 337 } |
337 | 338 |
338 /// Parses [node]'s value as a boolean selector. | 339 /// Parses [node]'s value as a boolean selector. |
339 BooleanSelector _parseBooleanSelector(String name) => | 340 BooleanSelector _parseBooleanSelector(String name) => |
340 _parseValue(name, (value) => new BooleanSelector.parse(value)); | 341 _parseValue(name, (value) => new BooleanSelector.parse(value)); |
341 | 342 |
342 /// Asserts that [node] is a string, passes its value to [parse], and returns | 343 /// Asserts that [node] is a string, passes its value to [parse], and returns |
343 /// the result. | 344 /// the result. |
344 /// | 345 /// |
345 /// If [parse] throws a [FormatException], it's wrapped to include [node]'s | 346 /// If [parse] throws a [FormatException], it's wrapped to include [node]'s |
346 /// span. | 347 /// span. |
347 _parseNode(YamlNode node, String name, parse(String value)) { | 348 /*=T*/ _parseNode/*<T>*/(YamlNode node, String name, |
| 349 /*=T*/ parse(String value)) { |
348 _validate(node, "$name must be a string.", (value) => value is String); | 350 _validate(node, "$name must be a string.", (value) => value is String); |
349 | 351 |
350 try { | 352 try { |
351 return parse(node.value); | 353 return parse(node.value); |
352 } on FormatException catch (error) { | 354 } on FormatException catch (error) { |
353 throw new SourceSpanFormatException( | 355 throw new SourceSpanFormatException( |
354 'Invalid $name: ${error.message}', node.span, _source); | 356 'Invalid $name: ${error.message}', node.span, _source); |
355 } | 357 } |
356 } | 358 } |
357 | 359 |
358 /// Asserts that [field] is a string, passes it to [parse], and returns the | 360 /// Asserts that [field] is a string, passes it to [parse], and returns the |
359 /// result. | 361 /// result. |
360 /// | 362 /// |
361 /// If [parse] throws a [FormatException], it's wrapped to include [field]'s | 363 /// If [parse] throws a [FormatException], it's wrapped to include [field]'s |
362 /// span. | 364 /// span. |
363 _parseValue(String field, parse(String value)) { | 365 /*=T*/ _parseValue/*<T>*/(String field, /*=T*/ parse(String value)) { |
364 var node = _document.nodes[field]; | 366 var node = _document.nodes[field]; |
365 if (node == null) return null; | 367 if (node == null) return null; |
366 return _parseNode(node, field, parse); | 368 return _parseNode(node, field, parse); |
367 } | 369 } |
368 | 370 |
369 /// Parses a nested configuration document. | 371 /// Parses a nested configuration document. |
370 /// | 372 /// |
371 /// [name] is the name of the field, which is used for error-handling. | 373 /// [name] is the name of the field, which is used for error-handling. |
372 /// [runnerConfig] controls whether runner configuration is allowed in the | 374 /// [runnerConfig] controls whether runner configuration is allowed in the |
373 /// nested configuration. It defaults to [_runnerConfig]. | 375 /// nested configuration. It defaults to [_runnerConfig]. |
(...skipping 18 matching lines...) Expand all Loading... |
392 _document.nodes.keys.firstWhere((key) => key.value == field).span, | 394 _document.nodes.keys.firstWhere((key) => key.value == field).span, |
393 _source); | 395 _source); |
394 } | 396 } |
395 | 397 |
396 /// Throws a [SourceSpanFormatException] with [message] about [field]. | 398 /// Throws a [SourceSpanFormatException] with [message] about [field]. |
397 void _error(String message, String field) { | 399 void _error(String message, String field) { |
398 throw new SourceSpanFormatException( | 400 throw new SourceSpanFormatException( |
399 message, _document.nodes[field].span, _source); | 401 message, _document.nodes[field].span, _source); |
400 } | 402 } |
401 } | 403 } |
OLD | NEW |