Skip to content

Commit 2cda966

Browse files
committed
Implement escaped integers
1 parent 7bf3737 commit 2cda966

File tree

2 files changed

+54
-6
lines changed

2 files changed

+54
-6
lines changed

httpie/cli/nested_json.py

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,19 @@ class Token(NamedTuple):
6161

6262

6363
def assert_cant_happen() -> NoReturn:
64-
raise ValueError("Unexpected value")
64+
raise ValueError('Unexpected value')
65+
66+
67+
def check_escaped_int(value: str) -> str:
68+
if not value.startswith('\\'):
69+
raise ValueError('Not an escaped int')
70+
71+
try:
72+
int(value[1:])
73+
except ValueError as exc:
74+
raise ValueError('Not an escaped int') from exc
75+
else:
76+
return value[1:]
6577

6678

6779
def tokenize(source: str) -> Iterator[Token]:
@@ -75,12 +87,18 @@ def send_buffer() -> Iterator[Token]:
7587
return None
7688

7789
value = ''.join(buffer)
78-
try:
79-
value = int(value)
80-
except ValueError:
81-
kind = TokenKind.TEXT
90+
for variation, kind in [
91+
(int, TokenKind.NUMBER),
92+
(check_escaped_int, TokenKind.TEXT),
93+
]:
94+
try:
95+
value = variation(value)
96+
except ValueError:
97+
continue
98+
else:
99+
break
82100
else:
83-
kind = TokenKind.NUMBER
101+
kind = TokenKind.TEXT
84102

85103
yield Token(
86104
kind, value, start=cursor - (len(buffer) + backslashes), end=cursor

tests/test_json.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,28 @@ def test_complex_json_arguments_with_non_json(httpbin, request_type, value):
376376
'people': {'known_ids': [None, 1000, None, None, None, 5000]},
377377
},
378378
),
379+
(
380+
[
381+
r'foo[\1][type]=migration',
382+
r'foo[\2][type]=migration',
383+
r'foo[\dates]:=[2012, 2013]',
384+
r'foo[\dates][0]:=2014',
385+
r'foo[\2012 bleh]:=2013',
386+
r'foo[bleh \2012]:=2014',
387+
r'\2012[x]:=2',
388+
r'\2012[\[3\]]:=4',
389+
],
390+
{
391+
'foo': {
392+
'1': {'type': 'migration'},
393+
'2': {'type': 'migration'},
394+
'\\dates': [2014, 2013],
395+
'\\2012 bleh': 2013,
396+
'bleh \\2012': 2014,
397+
},
398+
'2012': {'x': 2, '[3]': 4},
399+
},
400+
),
379401
],
380402
)
381403
def test_nested_json_syntax(input_json, expected_json, httpbin):
@@ -465,6 +487,14 @@ def test_nested_json_syntax(input_json, expected_json, httpbin):
465487
['foo[-10]:=[1,2]'],
466488
'HTTPie Value Error: Negative indexes are not supported.\nfoo[-10]\n ^^^',
467489
),
490+
(
491+
['foo[0]:=1', 'foo[]:=2', 'foo[\\2]:=3'],
492+
"HTTPie Type Error: Can't perform 'key' based access on 'foo' which has a type of 'array' but this operation requires a type of 'object'.\nfoo[\\2]\n ^^^^",
493+
),
494+
(
495+
['foo[\\1]:=2', 'foo[5]:=3'],
496+
"HTTPie Type Error: Can't perform 'index' based access on 'foo' which has a type of 'object' but this operation requires a type of 'array'.\nfoo[5]\n ^^^",
497+
),
468498
],
469499
)
470500
def test_nested_json_errors(input_json, expected_error, httpbin):

0 commit comments

Comments
 (0)