Skip to content

Commit

Permalink
fix: Mixing flattened maps
Browse files Browse the repository at this point in the history
  • Loading branch information
danilofuchs committed Nov 7, 2023
1 parent 3042249 commit be591ac
Show file tree
Hide file tree
Showing 4 changed files with 235 additions and 190 deletions.
7 changes: 6 additions & 1 deletion lib/src/unflatten.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ Map<String, dynamic> unflatten(

for (int i = 0; i < keys.length; i++) {
final k = keys[i];
if (i == keys.length - 1) {
if (value is Map) {
for (final entry in value.entries) {
current[k] ??= <String, dynamic>{};
current[k][entry.key] = entry.value;
}
} else if (i == keys.length - 1) {
if (_isInteger(k)) {
// This means that we have to do with a list instead
(current as List).add(value);
Expand Down
162 changes: 0 additions & 162 deletions test/flat_test.dart

This file was deleted.

165 changes: 165 additions & 0 deletions test/flatten_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
import 'dart:convert';

import 'package:flat/flat.dart';
import 'package:test/test.dart';

void main() {
group('Flatten', () {
test('Flattens nested Map', () {
const obj = {
"a": 1,
"b": {"c": 2}
};

const expected = {"a": 1, "b.c": 2};

final result = flatten(obj);

expect(result, expected);
});

test('Preserves empty Map', () {
const obj = {"a": {}};

const expected = {"a": {}};

final result = flatten(obj);

expect(result, expected);
});

test('Preserves null', () {
const obj = {"a": null};

const expected = {"a": null};

final result = flatten(obj);

expect(result, expected);
});

test('Preserves deep null', () {
const obj = {
"a": {"b": null}
};

const expected = {"a.b": null};

final result = flatten(obj);

expect(result, expected);
});

test('Flattens List', () {
const obj = {
"a": "item",
"b": [0, 1]
};

const expected = {"a": "item", "b.0": 0, "b.1": 1};

final result = flatten(obj);

expect(result, expected);
});

test('Flattens complex Map', () {
const obj = {
"a": 1,
"b": {
"c": 2,
"d": "asd",
"a": {
"a": [
{"where": "a"}
]
}
}
};

const expected = {"a": 1, "b.c": 2, "b.d": "asd", "b.a.a.0.where": "a"};

final result = flatten(obj);

expect(result, expected);
});

test('Should delimit with optional delimiter parameter', () {
const obj = {
"a": 1,
"b": {"c": 2}
};

const expected = {"a": 1, "b_c": 2};

final result = flatten(obj, delimiter: "_");

expect(result, expected);
});

test('Should not flatten array when in safe mode', () {
const obj = {
"a": 1,
"b": {
"c": [
{"d": 2}
]
}
};

const expected = {
"a": 1,
"b.c": [
{"d": 2}
]
};

final result = flatten(obj, safe: true);

expect(result, expected);
});

test('Should limit depth by maxDepth', () {
const obj = {
"a": {
"b": {
"c": {
"d": {"e": 1}
}
}
}
};

const maxDepth = 3;

const expected = {
"a.b.c": {
"d": {"e": 1}
}
};

final result = flatten(obj, maxDepth: maxDepth);

expect(result, expected);
});

test('flattens decoded json with lists', () {
// https://github.com/danilofuchs/flat/pull/6
const obj = {
"mediaData": {
"resources": [
{"uri": "spotify://", "mimeType": "audio/unknown"}
]
}
};

final result =
flatten(jsonDecode(jsonEncode(obj)) as Map<String, dynamic>);

expect(result, {
"mediaData.resources.0.uri": "spotify://",
"mediaData.resources.0.mimeType": "audio/unknown"
});
});
});
}
Loading

0 comments on commit be591ac

Please sign in to comment.