Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(140)

Side by Side Diff: packages/analyzer/lib/src/util/yaml.dart

Issue 2990843002: Removed fixed dependencies (Closed)
Patch Set: Created 3 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
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.
4
5 library analyzer.src.util.yaml;
6
7 import 'dart:collection';
8
9 import 'package:yaml/yaml.dart';
10
11 /// If all of the elements of [list] are strings, return a list of strings
12 /// containing the same elements. Otherwise, return `null`.
13 List<String> toStringList(YamlList list) {
14 if (list == null) {
15 return null;
16 }
17 List<String> stringList = <String>[];
18 for (var element in list) {
19 if (element is String) {
20 stringList.add(element);
21 } else {
22 return null;
23 }
24 }
25 return stringList;
26 }
27
28 /// Merges two maps (of yaml) with simple override semantics, suitable for
29 /// merging two maps where one map defines default values that are added to
30 /// (and possibly overridden) by an overriding map.
31 class Merger {
32 /// Merges a default [o1] with an overriding object [o2].
33 ///
34 /// * lists are merged (without duplicates).
35 /// * lists of scalar values can be promoted to simple maps when merged with
36 /// maps of strings to booleans (e.g., ['opt1', 'opt2'] becomes
37 /// {'opt1': true, 'opt2': true}.
38 /// * maps are merged recursively.
39 /// * if map values cannot be merged, the overriding value is taken.
40 ///
41 Object merge(Object o1, Object o2) {
42 // Handle promotion first.
43 if (o1 is List && isMapToBools(o2)) {
44 o1 = new Map.fromIterable(o1, key: (item) => item, value: (item) => true);
45 } else if (isMapToBools(o1) && o2 is List) {
46 o2 = new Map.fromIterable(o2, key: (item) => item, value: (item) => true);
47 }
48
49 if (o1 is Map && o2 is Map) {
50 return mergeMap(o1, o2);
51 }
52 if (o1 is List && o2 is List) {
53 return mergeList(o1, o2);
54 }
55 // Default to override.
56 return o2;
57 }
58
59 /// Merge lists, avoiding duplicates.
60 List mergeList(List l1, List l2) =>
61 new List()..addAll(l1)..addAll(l2.where((item) => !l1.contains(item)));
62
63 /// Merge maps (recursively).
64 Map mergeMap(Map m1, Map m2) {
65 Map merged = new HashMap()..addAll(m1);
66 m2.forEach((k, v) {
67 merged[k] = merge(merged[k], v);
68 });
69 return merged;
70 }
71
72 static bool isMapToBools(Object o) =>
73 o is Map && o.values.every((v) => v is bool);
74 }
OLDNEW
« no previous file with comments | « packages/analyzer/lib/src/util/utilities_timing.dart ('k') | packages/analyzer/lib/task/dart.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698