From 11d638a9088ed988981a7677222752455e4790e2 Mon Sep 17 00:00:00 2001 From: Glenn Lewis <6598971+gmlewis@users.noreply.github.com> Date: Tue, 20 Aug 2024 09:06:20 -0400 Subject: [PATCH] Completely remove gmlewis/jsonutil dependency Signed-off-by: Glenn Lewis <6598971+gmlewis@users.noreply.github.com> --- README.md | 21 +++++-------------- examples/arrays/all-three.mbt | 18 +++++++++------- examples/arrays/moon.pkg.json | 1 - examples/arrays/plugin-functions.mbt | 8 +++---- examples/count-vowels/count-vowels.mbt | 14 +++++++------ examples/count-vowels/count-vowels_wbtest.mbt | 8 +++---- examples/count-vowels/moon.pkg.json | 1 - moon.mod.json | 6 ++---- pdk/host/host.mbt | 2 +- pdk/host/memory.mbt | 2 +- pdk/host/moon.pkg.json | 1 - pdk/http/header.mbt | 9 ++------ pdk/http/http.mbt | 12 ++++++----- pdk/http/method.mbt | 4 ++-- pdk/http/moon.pkg.json | 1 - 15 files changed, 47 insertions(+), 61 deletions(-) diff --git a/README.md b/README.md index e5ff0ce..e480011 100644 --- a/README.md +++ b/README.md @@ -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 { @@ -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": { @@ -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: diff --git a/examples/arrays/all-three.mbt b/examples/arrays/all-three.mbt index ee9185f..adf06d8 100644 --- a/examples/arrays/all-three.mbt +++ b/examples/arrays/all-three.mbt @@ -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. diff --git a/examples/arrays/moon.pkg.json b/examples/arrays/moon.pkg.json index a39237b..4a2ad86 100644 --- a/examples/arrays/moon.pkg.json +++ b/examples/arrays/moon.pkg.json @@ -1,6 +1,5 @@ { "import": [ - "gmlewis/jsonutil", "gmlewis/moonbit-pdk/pdk/config", "gmlewis/moonbit-pdk/pdk/host", "gmlewis/moonbit-pdk/pdk/var" diff --git a/examples/arrays/plugin-functions.mbt b/examples/arrays/plugin-functions.mbt index 5da9390..5b0c461 100644 --- a/examples/arrays/plugin-functions.mbt +++ b/examples/arrays/plugin-functions.mbt @@ -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 } @@ -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 } @@ -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 } @@ -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 } diff --git a/examples/count-vowels/count-vowels.mbt b/examples/count-vowels/count-vowels.mbt index 99eec66..2033b8e 100644 --- a/examples/count-vowels/count-vowels.mbt +++ b/examples/count-vowels/count-vowels.mbt @@ -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 { @@ -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 } diff --git a/examples/count-vowels/count-vowels_wbtest.mbt b/examples/count-vowels/count-vowels_wbtest.mbt index 1f545c4..e837bcb 100644 --- a/examples/count-vowels/count-vowels_wbtest.mbt +++ b/examples/count-vowels/count-vowels_wbtest.mbt @@ -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) } diff --git a/examples/count-vowels/moon.pkg.json b/examples/count-vowels/moon.pkg.json index 774f99d..231aec0 100644 --- a/examples/count-vowels/moon.pkg.json +++ b/examples/count-vowels/moon.pkg.json @@ -1,6 +1,5 @@ { "import": [ - "gmlewis/jsonutil", "gmlewis/moonbit-pdk/pdk/config", "gmlewis/moonbit-pdk/pdk/host", "gmlewis/moonbit-pdk/pdk/var" diff --git a/moon.mod.json b/moon.mod.json index f84245a..8750c09 100644 --- a/moon.mod.json +++ b/moon.mod.json @@ -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", diff --git a/pdk/host/host.mbt b/pdk/host/host.mbt index 6a789f1..3420c78 100644 --- a/pdk/host/host.mbt +++ b/pdk/host/host.mbt @@ -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 { diff --git a/pdk/host/memory.mbt b/pdk/host/memory.mbt index 6392931..f683c45 100644 --- a/pdk/host/memory.mbt +++ b/pdk/host/memory.mbt @@ -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 diff --git a/pdk/host/moon.pkg.json b/pdk/host/moon.pkg.json index a772ff1..da54105 100644 --- a/pdk/host/moon.pkg.json +++ b/pdk/host/moon.pkg.json @@ -1,6 +1,5 @@ { "import": [ - "gmlewis/jsonutil", "gmlewis/moonbit-pdk/pdk", "gmlewis/moonbit-pdk/pdk/extism" ] diff --git a/pdk/http/header.mbt b/pdk/http/header.mbt index d062875..45853ec 100644 --- a/pdk/http/header.mbt +++ b/pdk/http/header.mbt @@ -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. diff --git a/pdk/http/http.mbt b/pdk/http/http.mbt index de0d93d..54f66b9 100644 --- a/pdk/http/http.mbt +++ b/pdk/http/http.mbt @@ -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. @@ -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 diff --git a/pdk/http/method.mbt b/pdk/http/method.mbt index 7472daf..81e228d 100644 --- a/pdk/http/method.mbt +++ b/pdk/http/method.mbt @@ -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() } diff --git a/pdk/http/moon.pkg.json b/pdk/http/moon.pkg.json index 62ad46b..0afc12e 100644 --- a/pdk/http/moon.pkg.json +++ b/pdk/http/moon.pkg.json @@ -1,6 +1,5 @@ { "import": [ - "gmlewis/jsonutil", "gmlewis/moonbit-pdk/pdk/extism", "gmlewis/moonbit-pdk/pdk/host" ]