fix: Deep-copy arrays correctly

This commit is contained in:
Sorunome 2021-01-17 15:38:09 +01:00
parent 8f1d35e0bc
commit 195d46b901
No known key found for this signature in database
GPG Key ID: B19471D07FC9BE9C
2 changed files with 36 additions and 14 deletions

View File

@ -1,17 +1,17 @@
/* MIT License
*
*
* Copyright (C) 2019, 2020, 2021 Famedly GmbH
*
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@ -22,16 +22,25 @@
*/
extension MapCopyExtension on Map<String, dynamic> {
dynamic _copyValue(dynamic value) {
if (value is Map<String, dynamic>) {
return value.copy();
}
if (value is List) {
final ret = [];
for (final val in value) {
ret.add(_copyValue(val));
}
return ret;
}
return value;
}
/// Deep-copies a given json map
Map<String, dynamic> copy() {
final copy = Map<String, dynamic>.from(this);
for (final entry in copy.entries) {
if (entry.value is Map<String, dynamic>) {
copy[entry.key] = (entry.value as Map<String, dynamic>).copy();
}
if (entry.value is List) {
copy[entry.key] = List.from(entry.value);
}
copy[entry.key] = _copyValue(entry.value);
}
return copy;
}

View File

@ -1,17 +1,17 @@
/* MIT License
*
*
* Copyright (C) 2019, 2020, 2021 Famedly GmbH
*
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@ -40,5 +40,18 @@ void main() {
original['child']['list'].add(3);
expect(copy['child']['list'], [1, 2]);
});
test('should do arrays', () {
final original = <String, dynamic>{
'arr': [
[1, 2],
{'beep': 'boop'},
],
};
final copy = original.copy();
original['arr'][0].add(3);
expect(copy['arr'][0], [1, 2]);
original['arr'][1]['beep'] = 'blargh';
expect(copy['arr'][1]['beep'], 'boop');
});
});
}