mirror of
https://github.com/flutter/packages.git
synced 2025-07-01 07:08:10 +08:00
mergeAttributes: deepcopy RHS Map if LHS isn't Map (#23)
mergeAttributes: deepcopy RHS Map if LHS isn't Map
This commit is contained in:
@ -12,8 +12,13 @@ void mergeAttributes(Map<String, dynamic> attributes,
|
|||||||
{@required Map<String, dynamic> into}) {
|
{@required Map<String, dynamic> into}) {
|
||||||
assert(attributes != null && into != null);
|
assert(attributes != null && into != null);
|
||||||
attributes.forEach((String name, dynamic value) {
|
attributes.forEach((String name, dynamic value) {
|
||||||
final dynamic targetValue = into[name];
|
dynamic targetValue = into[name];
|
||||||
if (value is Map && targetValue is Map) {
|
if (value is Map) {
|
||||||
|
if (targetValue is! Map) {
|
||||||
|
// Let mergeAttributes make a deep copy, because assigning a reference
|
||||||
|
// of 'value' will expose 'value' to be mutated by further merges.
|
||||||
|
into[name] = targetValue = <String, dynamic>{};
|
||||||
|
}
|
||||||
mergeAttributes(value, into: targetValue);
|
mergeAttributes(value, into: targetValue);
|
||||||
} else {
|
} else {
|
||||||
into[name] = value;
|
into[name] = value;
|
||||||
|
@ -35,6 +35,25 @@ void main() {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('does not allow overriding original maps', () {
|
||||||
|
final environment = <String, dynamic>{
|
||||||
|
'extra': {
|
||||||
|
'device': 'Pixel 2',
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
final event = <String, dynamic>{
|
||||||
|
'extra': {
|
||||||
|
'widget': 'Scaffold',
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
final target = <String, dynamic>{};
|
||||||
|
mergeAttributes(environment, into: target);
|
||||||
|
mergeAttributes(event, into: target);
|
||||||
|
expect(environment['extra'], {'device': 'Pixel 2'});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
group('formatDateAsIso8601WithSecondPrecision', () {
|
group('formatDateAsIso8601WithSecondPrecision', () {
|
||||||
|
Reference in New Issue
Block a user