Skip to content

Commit 4d0c863

Browse files
authored
fix issue with arrays (#8)
* fix issue with arrays * fix test formatting
1 parent 1e8d6a9 commit 4d0c863

File tree

4 files changed

+77
-16
lines changed

4 files changed

+77
-16
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

benchmarks.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from urllib.parse import urlencode
88

99
import pyperf
10+
1011
from fast_query_parsers import parse_query_string, parse_url_encoded_dict
1112

1213
url_encoded_query = urlencode(

src/query_string.rs

Lines changed: 73 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,26 +36,29 @@ fn decode_value(json_str: String) -> Value {
3636
}
3737
if json_str.starts_with('[') && json_str.ends_with(']') {
3838
let values_array: Value = serde_json::from_str(json_str.as_str()).unwrap_or_default();
39-
let vector_values = values_array
40-
.as_array()
41-
.unwrap()
42-
.iter()
43-
.map(|el| decode_value(el.to_string()))
44-
.collect::<Vec<Value>>();
39+
let vector_values = match values_array.as_array() {
40+
Some(arr) => arr
41+
.iter()
42+
.map(|el| decode_value(el.to_string()))
43+
.collect::<Vec<Value>>(),
44+
None => Vec::new(),
45+
};
46+
4547
return Value::from(vector_values);
4648
}
49+
let normalized = json_str.replace('"', "");
4750

48-
let json_integer = json_str.parse::<i64>();
49-
let json_float = json_str.parse::<f64>();
50-
let json_boolean = json_str.parse::<bool>();
51-
let json_null = Ok::<_, Infallible>(json_str == "null");
51+
let json_integer = normalized.parse::<i64>();
52+
let json_float = normalized.parse::<f64>();
53+
let json_boolean = normalized.parse::<bool>();
54+
let json_null = Ok::<_, Infallible>(normalized == "null");
5255

5356
match (json_integer, json_float, json_boolean, json_null) {
5457
(Ok(json_integer), _, _, _) => Value::from(json_integer),
5558
(_, Ok(json_float), _, _) => Value::from(json_float),
5659
(_, _, Ok(json_boolean), _) => Value::from(json_boolean),
5760
(_, _, _, Ok(true)) => Value::Null,
58-
_ => Value::from(json_str.replace('"', "")),
61+
_ => Value::from(normalized),
5962
}
6063
}
6164

@@ -65,14 +68,24 @@ pub fn parse_query_string_to_json(bs: &[u8]) -> Value {
6568
let mut array_map: FxHashMap<String, Vec<String>> = FxHashMap::default();
6669

6770
for (key, value) in parse_query_string(bs, '&') {
68-
array_map.entry(key).or_default().push(value)
71+
match array_map.get_mut(&key) {
72+
Some(entry) => {
73+
entry.push(value);
74+
}
75+
None => {
76+
array_map.insert(key, vec![value]);
77+
}
78+
}
6979
}
7080

71-
for (key, value) in array_map.into_iter() {
81+
for (key, value) in array_map {
7282
if value.len() == 1 {
7383
values_map.insert(key, decode_value(value[0].to_owned()));
7484
} else {
75-
values_map.insert(key, decode_value(format!("[{}]", value.join(","))));
85+
values_map.insert(
86+
key,
87+
decode_value(serde_json::to_string(&value).unwrap_or_default()),
88+
);
7689
}
7790
}
7891

@@ -220,4 +233,50 @@ mod tests {
220233
json!({ "a": [1,2,3] })
221234
);
222235
}
236+
237+
#[test]
238+
fn it_parses_random_values() {
239+
let result = parse_query_string_to_json(b"_id=637ca2c6a8178b1d6aab4140&index=0&guid=92d50031-11ee-4756-af59-cd47a45082e7&isActive=false&balance=%242%2C627.33&picture=http%3A%2F%2Fplacehold.it%2F32x32&age=36&eyeColor=blue&name=Colette+Suarez&gender=female&company=ZENTILITY&email=colettesuarez%40zentility.com&phone=%2B1+%28841%29+509-2669&address=400+Polar+Street%2C+Emory%2C+Palau%2C+3376&about=Deserunt+nostrud+quis+enim+fugiat+labore+labore+sint+deserunt+aliquip+est+fugiat+mollit+commodo.+Labore+pariatur+laboris+ut+irure+voluptate+aliqua+non+ex+enim.+Dolor+ea+mollit+dolore+anim+eu+velit+labore+aliquip+laborum+irure+duis+aliqua+sunt+sint.+Ex+elit+ea+irure+nisi+qui+exercitation+ullamco+occaecat+eu+culpa+magna+quis+dolor+dolor.+Officia+nostrud+consectetur+exercitation+consequat+qui+est+dolore+cillum+dolor+minim+tempor.%0D%0A&registered=2015-12-11T05%3A34%3A25+-01%3A00&latitude=-14.326509&longitude=-32.417451&tags=qui&tags=occaecat&tags=quis&tags=minim&tags=aliquip&tags=sunt&tags=pariatur&friends=%7B%27id%27%3A+0%2C+%27name%27%3A+%27Flora+Phelps%27%7D&friends=%7B%27id%27%3A+1%2C+%27name%27%3A+%27Coffey+Warner%27%7D&friends=%7B%27id%27%3A+2%2C+%27name%27%3A+%27Lyons+Mccall%27%7D&greeting=Hello%2C+Colette+Suarez%21+You+have+4+unread+messages.&favoriteFruit=banana");
240+
assert_eq!(
241+
result,
242+
json!(
243+
{
244+
"_id": "637ca2c6a8178b1d6aab4140",
245+
"about": "Deserunt nostrud quis enim fugiat labore labore sint deserunt aliquip est fugiat mollit commodo. Labore pariatur laboris ut irure voluptate aliqua non ex enim. Dolor ea mollit dolore anim eu velit labore aliquip laborum irure duis aliqua sunt sint. Ex elit ea irure nisi qui exercitation ullamco occaecat eu culpa magna quis dolor dolor. Officia nostrud consectetur exercitation consequat qui est dolore cillum dolor minim tempor.\r\n",
246+
"address": "400 Polar Street, Emory, Palau, 3376",
247+
"age": 36,
248+
"balance": "$2,627.33",
249+
"company": "ZENTILITY",
250+
"email": "colettesuarez@zentility.com",
251+
"eyeColor": "blue",
252+
"favoriteFruit": "banana",
253+
"friends": [
254+
"{'id': 0, 'name': 'Flora Phelps'}",
255+
"{'id': 1, 'name': 'Coffey Warner'}",
256+
"{'id': 2, 'name': 'Lyons Mccall'}"
257+
],
258+
"gender": "female",
259+
"greeting": "Hello, Colette Suarez! You have 4 unread messages.",
260+
"guid": "92d50031-11ee-4756-af59-cd47a45082e7",
261+
"index": 0,
262+
"isActive": false,
263+
"latitude": -14.326509,
264+
"longitude": -32.417451,
265+
"name": "Colette Suarez",
266+
"phone": "+1 (841) 509-2669",
267+
"picture": "http://placehold.it/32x32",
268+
"registered": "2015-12-11T05:34:25 -01:00",
269+
"tags": [
270+
"qui",
271+
"occaecat",
272+
"quis",
273+
"minim",
274+
"aliquip",
275+
"sunt",
276+
"pariatur"
277+
]
278+
}
279+
)
280+
)
281+
}
223282
}

tests/test_parse_qsl.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from urllib.parse import parse_qsl, urlencode
33

44
import pytest
5+
56
from fast_query_parsers import parse_query_string
67

78

0 commit comments

Comments
 (0)