Skip to content

Commit

Permalink
Completely remove gmlewis/jsonutil dependency
Browse files Browse the repository at this point in the history
Signed-off-by: Glenn Lewis <6598971+gmlewis@users.noreply.github.com>
  • Loading branch information
gmlewis committed Aug 20, 2024
1 parent 1863738 commit 11d638a
Show file tree
Hide file tree
Showing 15 changed files with 47 additions and 61 deletions.
21 changes: 5 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,6 @@ struct Sum {
sum : Int
}

pub impl @jsonutil.ToJson for Sum with to_json(self) {
@jsonutil.from_entries([("sum", self.sum)])
}

pub fn add() -> Int {
let input = @host.input_string()
let params = try {
Expand All @@ -219,26 +215,19 @@ pub fn add() -> Int {
}
//
let sum = { sum: params.a + params.b }
let json_value = @jsonutil.to_json(sum)
let json_value = sum.to_json()
@host.output_json_value(json_value)
0 // success
}
```

Add the `gmlewis/json` package to your project:

```bash
moon add gmlewis/json
```

And import it into `main/moon.pkg.json` as `jsonutil`, remembering also to
export your `add` function in `main/moon.pkg.json`:
Export your `add` function in `main/moon.pkg.json`:

```json
{
"is-main": true,
"import": [
"gmlewis/jsonutil",
"gmlewis/moonbit-pdk/pdk/host"
],
"link": {
Expand Down Expand Up @@ -542,9 +531,9 @@ The code has been updated to support compiler:

```bash
$ moon version --all
moon 0.1.20240813 (8b14470 2024-08-13) ~/.moon/bin/moon
moonc v0.1.20240813+4025fe3de ~/.moon/bin/moonc
moonrun 0.1.20240716 (08bce9c 2024-07-16) ~/.moon/bin/moonrun
moon 0.1.20240819 (284058b 2024-08-19) ~/.moon/bin/moon
moonc v0.1.20240820+85e9a0dc8 ~/.moon/bin/moonc
moonrun 0.1.20240820 (ecf5abc 2024-08-20) ~/.moon/bin/moonrun
```

Use [`moonup`] to manage `moon` compiler versions:
Expand Down
18 changes: 11 additions & 7 deletions examples/arrays/all-three.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@ pub struct AllThree {
strings : Array[String]
} derive(Eq, Show)

pub impl @jsonutil.ToJson for AllThree with to_json(self) {
let fields : Array[(String, @jsonutil.ToJson)] = [
("ints", self.ints),
("floats", self.floats),
("strings", self.strings),
]
@jsonutil.from_entries(fields)
fn array_to_json[T](arr : Array[T], ~f : (T) -> Json) -> Json {
Array(arr.map(f))
}

fn to_json(self : AllThree) -> Json {
let { ints, floats, strings } = self
{
"ints": ints |> array_to_json(f=fn { x => Number(x.to_double()) }),
"floats": floats |> array_to_json(f=fn { x => Number(x) }),
"strings": strings |> array_to_json(f=fn { x => String(x) }),
}
}

/// `process_all_three` processes all three array types.
Expand Down
1 change: 0 additions & 1 deletion examples/arrays/moon.pkg.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"import": [
"gmlewis/jsonutil",
"gmlewis/moonbit-pdk/pdk/config",
"gmlewis/moonbit-pdk/pdk/host",
"gmlewis/moonbit-pdk/pdk/var"
Expand Down
8 changes: 4 additions & 4 deletions examples/arrays/plugin-functions.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub fn progressive_sum_ints() -> Int {
//
let result = process_ints(ints)
//
let jv = @jsonutil.to_json(result)
let jv = result.to_json()
@host.output_json_value(jv)
0 // success
}
Expand All @@ -47,7 +47,7 @@ pub fn progressive_sum_floats() -> Int {
//
let result = process_floats(floats)
//
let jv = @jsonutil.to_json(result)
let jv = result.to_json()
@host.output_json_value(jv)
0 // success
}
Expand All @@ -74,7 +74,7 @@ pub fn progressive_concat_strings() -> Int {
//
let result = process_strings(strings)
//
let jv = @jsonutil.to_json(result)
let jv = result.to_json()
@host.output_json_value(jv)
0 // success
}
Expand Down Expand Up @@ -121,7 +121,7 @@ pub fn all_three_object() -> Int {
//
let result = process_all_three(all_three)
//
let jv = @jsonutil.to_json(result)
let jv = result.to_json()
@host.output_json_value(jv)
0 // success
}
14 changes: 8 additions & 6 deletions examples/count-vowels/count-vowels.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ pub struct VowelReport {
count : Int
total : Int
vowels : String
}
} derive(Show, Eq)

impl @jsonutil.ToJson for VowelReport with to_json(self) {
@jsonutil.from_entries(
[("count", self.count), ("total", self.total), ("vowels", self.vowels)],
)
pub fn to_json(self : VowelReport) -> Json {
{
"count": self.count.to_json(),
"total": self.total.to_json(),
"vowels": self.vowels.to_json(),
}
}

fn get_total() -> Int {
Expand Down Expand Up @@ -49,6 +51,6 @@ pub fn count_vowels() -> Int {
let total = get_total() + count
store_total(total)
//
{ count, total, vowels } |> @jsonutil.to_json() |> @host.output_json_value()
{ count, total, vowels }.to_json() |> @host.output_json_value()
0 // success
}
8 changes: 4 additions & 4 deletions examples/count-vowels/count-vowels_wbtest.mbt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
test "to_json" {
let got = { count: 1, total: 2, vowels: "some string" }
|> @jsonutil.to_json()
|> @jsonutil.stringify(spaces=0, newline=false)
let got = { count: 1, total: 2, vowels: "some string" }.to_json().stringify()
// See: https://github.com/moonbitlang/core/issues/878
// for trailing ".0" on the integers.
let want =
#|{"count":1,"total":2,"vowels":"some string"}
#|{"count":1.0,"total":2.0,"vowels":"some string"}
assert_eq!(got, want)
}
1 change: 0 additions & 1 deletion examples/count-vowels/moon.pkg.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"import": [
"gmlewis/jsonutil",
"gmlewis/moonbit-pdk/pdk/config",
"gmlewis/moonbit-pdk/pdk/host",
"gmlewis/moonbit-pdk/pdk/var"
Expand Down
6 changes: 2 additions & 4 deletions moon.mod.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
{
"name": "gmlewis/moonbit-pdk",
"version": "0.34.0",
"deps": {
"gmlewis/jsonutil": "0.22.0"
},
"version": "0.35.0",
"deps": {},
"readme": "README.md",
"repository": "https://github.com/gmlewis/moonbit-pdk",
"license": "Apache-2.0",
Expand Down
2 changes: 1 addition & 1 deletion pdk/host/host.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub fn output_string(s : String) -> Unit {
/// `output_json_value` converts a MoonBit @json.JsonValue to an Extism JSON string
/// and sends it to the host.
pub fn output_json_value(j : @json.JsonValue) -> Unit {
@jsonutil.stringify(j, spaces=0, newline=false) |> output_string()
j.stringify() |> output_string()
}

fn set_error_bytes(b : Bytes) -> Unit {
Expand Down
2 changes: 1 addition & 1 deletion pdk/host/memory.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub fn Memory::allocate_string(s : String) -> Memory {
/// `Memory::allocate_json_value` allocates and initializes a UTF-8 string
/// in host memory that is converted from this `@json.JsonValue`.
pub fn Memory::allocate_json_value(j : @json.JsonValue) -> Memory {
@jsonutil.stringify(j, spaces=0, newline=false) |> Memory::allocate_string()
j.stringify() |> Memory::allocate_string()
}

/// `to_string` reads and converts the UTF-8 string residing in the host memory
Expand Down
1 change: 0 additions & 1 deletion pdk/host/moon.pkg.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"import": [
"gmlewis/jsonutil",
"gmlewis/moonbit-pdk/pdk",
"gmlewis/moonbit-pdk/pdk/extism"
]
Expand Down
9 changes: 2 additions & 7 deletions pdk/http/header.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,8 @@ pub fn Header::new() -> Header {
Header(Map::new())
}

impl @jsonutil.ToJson for Header with to_json(self) {
let pairs = self.0
.iter()
.map(fn { (k, v) => (k, @json.JsonValue::String(v)) })
.collect()
|> Map::from_array()
@json.JsonValue::Object(pairs)
pub fn to_json(self : Header) -> Json {
self.0.to_json()
}

/// `add` adds a value to a named (by `key`) header field.
Expand Down
12 changes: 7 additions & 5 deletions pdk/http/http.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ pub struct Request {
url : String
}

impl @jsonutil.ToJson for Request with to_json(self) {
@jsonutil.from_entries(
[("method", self.method), ("header", self.header), ("url", self.url)],
)
pub fn to_json(self : Request) -> Json {
{
"method": self.method.to_json(),
"header": self.header.to_json(),
"url": self.url.to_json(),
}
}

/// `Response` represents an HTTP response from the Extism host.
Expand All @@ -28,7 +30,7 @@ pub fn new_request(method : Method, url : String) -> Request {
/// and returns it to the caller.
/// Note that the (optional) `body` is freed by this call.
pub fn send(self : Request, ~body : @host.Memory? = None) -> Response {
let meta_mem = self |> @jsonutil.to_json() |> @host.allocate_json_value()
let meta_mem = self.to_json() |> @host.allocate_json_value()
let body_memory_offset = match body {
Some(v) => v.offset
None => 0L
Expand Down
4 changes: 2 additions & 2 deletions pdk/http/method.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ pub enum Method {
PATCH
} derive(Show)

impl @jsonutil.ToJson for Method with to_json(self) {
@json.JsonValue::String(self.to_string())
pub fn to_json(self : Method) -> Json {
self.to_string().to_json()
}
1 change: 0 additions & 1 deletion pdk/http/moon.pkg.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"import": [
"gmlewis/jsonutil",
"gmlewis/moonbit-pdk/pdk/extism",
"gmlewis/moonbit-pdk/pdk/host"
]
Expand Down

0 comments on commit 11d638a

Please sign in to comment.