41 lines
1.4 KiB
Dart
41 lines
1.4 KiB
Dart
|
import 'get_value.dart';
|
||
|
|
||
|
/// Parses a URI-encoded string into real data! **Wow!**
|
||
|
///
|
||
|
/// Whichever map you provide will be automatically populated from the urlencoded body string you provide.
|
||
|
buildMapFromUri(Map map, String body) {
|
||
|
RegExp parseArrayRgx = new RegExp(r'^(.+)\[\]$');
|
||
|
|
||
|
for (String keyValuePair in body.split('&')) {
|
||
|
if (keyValuePair.contains('=')) {
|
||
|
List<String> split = keyValuePair.split('=');
|
||
|
String key = Uri.decodeQueryComponent(split[0]);
|
||
|
String value = Uri.decodeQueryComponent(split[1]);
|
||
|
|
||
|
if (parseArrayRgx.hasMatch(key)) {
|
||
|
Match queryMatch = parseArrayRgx.firstMatch(key);
|
||
|
key = queryMatch.group(1);
|
||
|
if (!(map[key] is List)) {
|
||
|
map[key] = [];
|
||
|
}
|
||
|
|
||
|
map[key].add(getValue(value));
|
||
|
} else if (key.contains('.')) {
|
||
|
// i.e. map.foo.bar => [map, foo, bar]
|
||
|
List<String> keys = key.split('.');
|
||
|
|
||
|
Map targetMap = map[keys[0]] ?? {};
|
||
|
map[keys[0]] = targetMap;
|
||
|
for (int i = 1; i < keys.length; i++) {
|
||
|
if (i < keys.length - 1) {
|
||
|
targetMap[keys[i]] = targetMap[keys[i]] ?? {};
|
||
|
targetMap = targetMap[keys[i]];
|
||
|
} else {
|
||
|
targetMap[keys[i]] = getValue(value);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
else map[key] = getValue(value);
|
||
|
} else map[Uri.decodeQueryComponent(keyValuePair)] = true;
|
||
|
}
|
||
|
}
|