Skip to content

Commit 0c314e7

Browse files
committed
fix up util script
1 parent ea73f94 commit 0c314e7

File tree

6 files changed

+15
-65
lines changed

6 files changed

+15
-65
lines changed

floki/body.mojo

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,46 +14,25 @@ struct Body(Copyable, Movable, Sized):
1414
self.body = List[Byte](body)
1515
self._json_cache = None
1616

17-
fn __init__(out self, var body: List[Byte]):
18-
self.body = body^
19-
self._json_cache = None
20-
21-
fn __init__(out self):
22-
self.body = List[Byte]()
23-
self._json_cache = None
24-
25-
fn __init__(out self, var data: emberjson.Object):
26-
"""Initializes the body from a dictionary, converting it to a form-encoded string."""
27-
self.body = List[Byte](emberjson.to_string(data^).as_bytes())
28-
self._json_cache = None
29-
3017
fn __len__(self) -> Int:
3118
return len(self.body)
3219

33-
fn __iadd__(mut self, var other: Body):
34-
self.body += other.body^
35-
other.body = List[Byte]()
36-
37-
fn __iadd__(mut self, other: Span[Byte]):
38-
self.body.extend(other)
39-
4020
fn as_bytes(self) -> Span[Byte, origin_of(self.body)]:
4121
return Span(self.body)
4222

4323
fn as_string_slice(self) -> StringSlice[origin_of(self.body)]:
4424
return StringSlice(unsafe_from_utf8=Span(self.body))
4525

46-
fn as_json(mut self) raises -> ref [origin_of(self._json_cache.value()._data)] emberjson.Object:
26+
fn as_json(mut self) raises -> ref [origin_of(self._json_cache._value)] emberjson.JSON:
4727
"""Converts the response body to a JSON object."""
4828
if not self.body:
4929
raise Error("Body is empty; cannot parse as JSON.")
5030

5131
if self._json_cache:
52-
return self._json_cache.value().object()
32+
return self._json_cache.value()
5333

54-
var parser = emberjson.Parser(StringSlice(unsafe_from_utf8=self.body))
55-
self._json_cache = parser.parse()
56-
return self._json_cache.value().object()
34+
self._json_cache = emberjson.parse(StringSlice(unsafe_from_utf8=self.body))
35+
return self._json_cache.value()
5736

5837
fn write_to[W: Writer, //](self, mut writer: W):
5938
"""Writes the body to a writer.

floki/response.mojo

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,39 +7,6 @@ from floki._logger import LOGGER
77
from floki.body import Body
88

99

10-
fn parse_response_headers(mut r: ByteReader) raises -> Tuple[Protocol, String, String]:
11-
if not r.peek():
12-
raise Error("parse_response_headers: Failed to read first byte from response header")
13-
14-
var first = r.read_word()
15-
r.increment()
16-
var second = r.read_word()
17-
r.increment()
18-
var third = r.read_line()
19-
# var cookies = List[String]()
20-
21-
# while not is_newline(r.peek()):
22-
# var key = r.read_until(BytesConstant.COLON)
23-
# r.increment()
24-
# if is_space(r.peek()):
25-
# r.increment()
26-
27-
# # TODO (bgreni): Handle possible trailing whitespace
28-
# var value = r.read_line()
29-
# var k = StringSlice(unsafe_from_utf8=key).lower()
30-
# if k == "set-cookie":
31-
# cookies.append(String(bytes=value))
32-
# continue
33-
34-
# headers[k] = String(bytes=value)
35-
return (
36-
Protocol.from_string(StringSlice(unsafe_from_utf8=first)),
37-
String(bytes=second),
38-
String(bytes=third),
39-
# cookies^,
40-
)
41-
42-
4310
@fieldwise_init
4411
struct HTTPResponse(Movable):
4512
var headers: Dict[String, String]

pixi.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ build_and_publish = [{ task = "build" }, { task = "publish" }]
3434

3535
[package]
3636
name = "floki"
37-
version = "25.7.1"
37+
version = "25.7.2"
3838

3939
[package.build]
4040
backend = { name = "pixi-build-mojo", version = "0.*", channel = "https://prefix.dev/pixi-build-backends" }

scripts/util.mojo

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ fn run_files(directory: Path, path: Optional[Path] = None) raises -> None:
1010
print("Path does not exist: ", directory)
1111
return
1212

13-
var files = [path.value()] if path else directory.listdir()
13+
var files: List[Path]
14+
if path:
15+
files = [path.value()]
16+
else:
17+
files = [path for path in directory.listdir() if path.suffix() == ".mojo"]
1418
for file in files:
1519
print("\nRunning file:", file)
1620
print(subprocess.run(String("mojo -D ASSERT=all -I . ", directory / file)))

test/test_free_functions.mojo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ fn test_post() raises -> None:
3434
# print(response.body.as_string_slice())
3535
assert_equal(response.status_code, StatusCode.CREATED)
3636
print("Body:")
37-
for node in response.body.as_json().items():
37+
for node in response.body.as_json().object().items():
3838
print(String(node.key, ": ", node.data))
3939

4040
print(response.body.as_json()["key2"]["subkey"])

test/test_session.mojo

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ fn test_post() raises -> None:
5757
# print(response.body.as_string_slice())
5858
assert_equal(response.status_code, StatusCode.CREATED)
5959
print("Body:")
60-
for node in response.body.as_json().items():
60+
for node in response.body.as_json().object().items():
6161
print(String(node.key, ": ", node.data))
6262

6363
print(response.body.as_json()["key2"]["subkey"])
@@ -78,7 +78,7 @@ fn test_post_file() raises -> None:
7878
)
7979
assert_equal(response.status_code, StatusCode.CREATED)
8080
print("Body:")
81-
for node in response.body.as_json().items():
81+
for node in response.body.as_json().object().items():
8282
print(String(node.key, ": ", node.data))
8383

8484
print(response.body.as_json()["content"]["recently_edited"])
@@ -115,7 +115,7 @@ fn test_put_file() raises -> None:
115115
)
116116
assert_equal(response.status_code, StatusCode.OK)
117117
print("Body:")
118-
for node in response.body.as_json().items():
118+
for node in response.body.as_json().object().items():
119119
print(String(node.key, ": ", node.data))
120120

121121

@@ -150,7 +150,7 @@ fn test_patch_file() raises -> None:
150150
)
151151
assert_equal(response.status_code, StatusCode.OK)
152152
print("Body:")
153-
for node in response.body.as_json().items():
153+
for node in response.body.as_json().object().items():
154154
print(String(node.key, ": ", node.data))
155155

156156

0 commit comments

Comments
 (0)