From 3eb6bb828977f765d070b462723cfcd7906a68b4 Mon Sep 17 00:00:00 2001 From: Mikhail Tavarez Date: Thu, 19 Sep 2024 11:24:10 -0500 Subject: [PATCH] fix examples (#50) --- .github/workflows/build.yml | 8 +++---- .github/workflows/test.yml | 2 +- README.md | 37 ++++++++++++++++--------------- examples/tcp/dial_client.mojo | 4 ++-- examples/tcp/get_request.mojo | 2 +- examples/tcp/listener_server.mojo | 2 +- examples/tcp/socket_client.mojo | 2 +- examples/tcp/socket_server.mojo | 2 +- examples/udp/dial_client.mojo | 6 ++--- examples/udp/listener_server.mojo | 4 ++-- examples/udp/socket_client.mojo | 4 ++-- examples/udp/socket_server.mojo | 8 +++---- test/test_get_addr.mojo | 8 +++---- 13 files changed, 44 insertions(+), 45 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 6b45a59..293db64 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -1,13 +1,11 @@ -name: Build all packages +name: build on: push: branches: - main - workflow_dispatch: - pull_request: - branches: - - main + paths: + - src/** # on: ["push"] jobs: diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 0b1f0e1..102564d 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -1,4 +1,4 @@ -name: Run Tests +name: tests on: pull_request: diff --git a/README.md b/README.md index 782b131..8707df4 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,13 @@ # gojo -Experiments in porting over Golang stdlib into Mojo and extra goodies that make use of it. It will not always be a 1:1 port, it's more so code inspired by the Golang stdlib and the Mojo community's code. This is not intended to be a full port, but rather a learning exercise and a way to experiment with Mojo's capabilities. Please feel free to contribute or use this as a starting point for your own projects! The codebase will remain in flux and will evolve with Mojo as future releases are created. +Experiments in porting over Golang stdlib into Mojo. + +It will not always be a 1:1 port, it's more so code inspired by the Golang stdlib and the Mojo community's code. This is not intended to be a full port, but rather a learning exercise and a way to experiment with Mojo's capabilities. Please feel free to contribute or use this as a starting point for your own projects! The codebase will remain in flux and will evolve with Mojo as future releases are created. + +![Mojo Version](https://img.shields.io/badge/Mojo%F0%9F%94%A5-24.5-orange) +![Build Status](https://github.com/thatstoasty/mist/actions/workflows/build.yml/badge.svg) +![Test Status](https://github.com/thatstoasty/mist/actions/workflows/test.yml/badge.svg) +[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) ## Installation @@ -8,25 +15,10 @@ Experiments in porting over Golang stdlib into Mojo and extra goodies that make 2. Next, add `gojo` to your project's dependencies by running `magic add gojo`. 3. Finally, run `magic install` to install in `gojo`. You should see the `.mojopkg` files in `$CONDA_PREFIX/lib/mojo/`. -## Projects that use Gojo - -### My projects - -- `weave`: A collection of (ANSI-sequence aware) text reflow operations & algorithms. [Link to the project.](https://github.com/thatstoasty/weave) -- `mog`: Terminal text styling library. [Link to the project.](https://github.com/thatstoasty/mog) -- `stump`: Bound Logger library. [Link to the project.](https://github.com/thatstoasty/stump) -- `prism`: CLI Library. [Link to the project.](https://github.com/thatstoasty/prism) - -### Community projects - -- `lightbug_http`: Simple and fast HTTP framework for Mojo! 🔥 [Link to the project.](https://github.com/saviorand/lightbug_http/tree/main) - ## What this includes All of these packages are partially implemented and do not support unicode characters until Mojo supports them. -### Gojo - - `bufio` - `Reader`: Buffered `io.Reader` - `Scanner`: Scanner interface to read data via tokens. @@ -54,6 +46,15 @@ All of these packages are partially implemented and do not support unicode chara Please check out the `test`, `examples`, and `benchmarks` directories for usage of the various packages! -## Sharp Edges & Bugs +## Projects that use Gojo -- Unicode characters are not supported until Mojo supports them. Sometimes it happens to work, but it's not guaranteed due to length discrepanices with ASCII and Unicode characters. If the character has a length of 2 or more, it probably will not work. +### My projects + +- `weave`: A collection of (ANSI-sequence aware) text reflow operations & algorithms. [Link to the project.](https://github.com/thatstoasty/weave) +- `mog`: Terminal text styling library. [Link to the project.](https://github.com/thatstoasty/mog) +- `stump`: Bound Logger library. [Link to the project.](https://github.com/thatstoasty/stump) +- `prism`: CLI Library. [Link to the project.](https://github.com/thatstoasty/prism) + +### Community projects + +- `lightbug_http`: Simple and fast HTTP framework for Mojo! 🔥 [Link to the project.](https://github.com/saviorand/lightbug_http/tree/main) diff --git a/examples/tcp/dial_client.mojo b/examples/tcp/dial_client.mojo index 65eacd5..f79975e 100644 --- a/examples/tcp/dial_client.mojo +++ b/examples/tcp/dial_client.mojo @@ -14,7 +14,7 @@ fn main() raises: var bytes_written: Int var err: Error bytes_written, err = connection.write( - String("GET / HTTP/1.1\r\nHost: www.example.com\r\nConnection: close\r\n\r\n").as_bytes() + String("GET / HTTP/1.1\r\nHost: www.example.com\r\nConnection: close\r\n\r\n").as_bytes_slice() ) if err: raise err @@ -27,7 +27,7 @@ fn main() raises: var response = List[UInt8, True](capacity=4096) var bytes_read: Int = 0 bytes_read, err = connection.read(response) - if err and str(err) != io.EOF: + if err and str(err) != str(io.EOF): raise err if bytes_read == 0: diff --git a/examples/tcp/get_request.mojo b/examples/tcp/get_request.mojo index 4ebb909..836db68 100644 --- a/examples/tcp/get_request.mojo +++ b/examples/tcp/get_request.mojo @@ -8,7 +8,7 @@ fn main() raises: var bytes_written: Int = 0 var err = Error() bytes_written, err = connection.write( - String("GET / HTTP/1.1\r\nHost: www.example.com\r\nConnection: close\r\n\r\n").as_bytes() + String("GET / HTTP/1.1\r\nHost: www.example.com\r\nConnection: close\r\n\r\n").as_bytes_slice() ) if err: raise err diff --git a/examples/tcp/listener_server.mojo b/examples/tcp/listener_server.mojo index efee547..76fdd4d 100644 --- a/examples/tcp/listener_server.mojo +++ b/examples/tcp/listener_server.mojo @@ -23,5 +23,5 @@ fn main() raises: # Send a response back to the client. var bytes_sent: Int - bytes_sent, err = connection.write(message.as_bytes()) + bytes_sent, err = connection.write(message.as_bytes_slice()) print("Message sent:", message, bytes_sent) diff --git a/examples/tcp/socket_client.mojo b/examples/tcp/socket_client.mojo index 26f2e58..806fb67 100644 --- a/examples/tcp/socket_client.mojo +++ b/examples/tcp/socket_client.mojo @@ -18,7 +18,7 @@ fn main() raises: if err: raise err var bytes_sent: Int - bytes_sent, err = socket.write(message.as_bytes()) + bytes_sent, err = socket.write(message.as_bytes_slice()) print("Message sent:", message) var bytes = List[UInt8, True](capacity=16) diff --git a/examples/tcp/socket_server.mojo b/examples/tcp/socket_server.mojo index de81b28..be9895a 100644 --- a/examples/tcp/socket_server.mojo +++ b/examples/tcp/socket_server.mojo @@ -33,7 +33,7 @@ fn main() raises: # Send a response back to the client. var bytes_sent: Int - bytes_sent, err = connection.write(message.as_bytes()) + bytes_sent, err = connection.write(message.as_bytes_slice()) print("Message sent:", message, bytes_sent) err = connection.close() if err: diff --git a/examples/udp/dial_client.mojo b/examples/udp/dial_client.mojo index 30f73e7..e1a03a7 100644 --- a/examples/udp/dial_client.mojo +++ b/examples/udp/dial_client.mojo @@ -14,14 +14,14 @@ fn main() raises: for _ in range(10): var bytes_sent: Int var err: Error - bytes_sent, err = udp.write_to(message.as_bytes(), host, port) + bytes_sent, err = udp.write_to(message.as_bytes_slice(), host, port) print("Message sent:", message, bytes_sent) - var bytes = List[UInt8](capacity=16) + var bytes = List[UInt8, True](capacity=16) var bytes_received: Int var remote: HostPort bytes_received, remote, err = udp.read_from(bytes) - if str(err) != io.EOF: + if str(err) != str(io.EOF): raise err bytes.append(0) diff --git a/examples/udp/listener_server.mojo b/examples/udp/listener_server.mojo index b62bcfd..20e21e4 100644 --- a/examples/udp/listener_server.mojo +++ b/examples/udp/listener_server.mojo @@ -6,7 +6,7 @@ fn main() raises: var listener = listen_udp("udp", UDPAddr("127.0.0.1", 12000)) while True: - var dest = List[UInt8](capacity=16) + var dest = List[UInt8, True](capacity=16) var bytes_read: Int var remote: HostPort var err: Error @@ -19,5 +19,5 @@ fn main() raises: print("Message received:", message) message = message.upper() var bytes_sent: Int - bytes_sent, err = listener.write_to(message.as_bytes(), UDPAddr(remote.host, remote.port)) + bytes_sent, err = listener.write_to(message.as_bytes_slice(), UDPAddr(remote.host, remote.port)) print("Message sent:", message) diff --git a/examples/udp/socket_client.mojo b/examples/udp/socket_client.mojo index 4cc89e6..15c1326 100644 --- a/examples/udp/socket_client.mojo +++ b/examples/udp/socket_client.mojo @@ -17,10 +17,10 @@ fn main() raises: bytes_sent, err = socket.send_to(message.as_bytes(), host, port) print("Message sent:", message) - var bytes: List[UInt8] + var bytes: List[UInt8, True] var remote: HostPort bytes, remote, err = socket.receive_from(1024) - if str(err) != io.EOF: + if str(err) != str(io.EOF): raise err bytes.append(0) diff --git a/examples/udp/socket_server.mojo b/examples/udp/socket_server.mojo index 0ea45ca..b2121c9 100644 --- a/examples/udp/socket_server.mojo +++ b/examples/udp/socket_server.mojo @@ -9,13 +9,13 @@ fn main() raises: alias port = 12000 socket.bind(host, port) - print("Listening on", socket.local_address_as_udp()) + print("Listening on", str(socket.local_address_as_udp())) while True: - var bytes: List[UInt8] + var bytes: List[UInt8, True] var remote: HostPort var err: Error bytes, remote, err = socket.receive_from(1024) - if str(err) != io.EOF: + if str(err) != str(io.EOF): raise err bytes.append(0) @@ -24,5 +24,5 @@ fn main() raises: message = message.upper() var bytes_sent: Int - bytes_sent, err = socket.send_to(message.as_bytes(), remote.host, remote.port) + bytes_sent, err = socket.send_to(message.as_bytes_slice(), remote.host, remote.port) print("Message sent:", message) diff --git a/test/test_get_addr.mojo b/test/test_get_addr.mojo index 1861b35..5c75028 100644 --- a/test/test_get_addr.mojo +++ b/test/test_get_addr.mojo @@ -80,7 +80,7 @@ # # socket.close() -# # def main(): -# # # test_dial() -# # # test_listener() -# # test_stuff() +# def main(): +# test_dial() +# # test_listener() +# # test_stuff()