Skip to content

Commit

Permalink
fix examples (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
thatstoasty authored Sep 19, 2024
1 parent 8ce56e8 commit 3eb6bb8
Show file tree
Hide file tree
Showing 13 changed files with 44 additions and 45 deletions.
8 changes: 3 additions & 5 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Run Tests
name: tests

on:
pull_request:
Expand Down
37 changes: 19 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,32 +1,24 @@
# 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

1. First, you'll need to configure your `mojoproject.toml` file to include my Conda channel. Add `"https://repo.prefix.dev/mojo-community"` to the list of channels.
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.
Expand Down Expand Up @@ -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)
4 changes: 2 additions & 2 deletions examples/tcp/dial_client.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion examples/tcp/get_request.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion examples/tcp/listener_server.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 1 addition & 1 deletion examples/tcp/socket_client.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion examples/tcp/socket_server.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 3 additions & 3 deletions examples/udp/dial_client.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions examples/udp/listener_server.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
4 changes: 2 additions & 2 deletions examples/udp/socket_client.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions examples/udp/socket_server.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
8 changes: 4 additions & 4 deletions test/test_get_addr.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
# # socket.close()


# # def main():
# # # test_dial()
# # # test_listener()
# # test_stuff()
# def main():
# test_dial()
# # test_listener()
# # test_stuff()

0 comments on commit 3eb6bb8

Please sign in to comment.