From 0010bdefafa27b8ebe73bb784e3fdb129d14c8c6 Mon Sep 17 00:00:00 2001 From: Glenn Lewis <6598971+gmlewis@users.noreply.github.com> Date: Wed, 21 Aug 2024 23:07:44 -0400 Subject: [PATCH] Update README.md and examples Signed-off-by: Glenn Lewis <6598971+gmlewis@users.noreply.github.com> --- README.md | 45 ++++++++++++++++----------- examples/add/README.md | 10 ++++++ examples/add/add.mbt | 63 ++++++++++++++++++++++++++++++++++++++ examples/add/moon.pkg.json | 13 ++++++++ moon.mod.json | 2 +- scripts/add.sh | 2 ++ 6 files changed, 116 insertions(+), 19 deletions(-) create mode 100644 examples/add/README.md create mode 100644 examples/add/add.mbt create mode 100644 examples/add/moon.pkg.json create mode 100644 scripts/add.sh diff --git a/README.md b/README.md index d0b4bfc..9dfed5a 100644 --- a/README.md +++ b/README.md @@ -34,11 +34,15 @@ You can find the reference documentation for this library on [mooncakes.io]: Examples can also be found there: +* [extism/moonbit-pdk/examples/add] +* [extism/moonbit-pdk/examples/arrays] * [extism/moonbit-pdk/examples/count-vowels] * [extism/moonbit-pdk/examples/greet] * [extism/moonbit-pdk/examples/http-get] * [extism/moonbit-pdk/examples/kitchen-sink] +[extism/moonbit-pdk/examples/add]: https://mooncakes.io/docs/#/extism/moonbit-pdk/examples/add/members +[extism/moonbit-pdk/examples/arrays]: https://mooncakes.io/docs/#/extism/moonbit-pdk/examples/arrays/members [extism/moonbit-pdk/examples/count-vowels]: https://mooncakes.io/docs/#/extism/moonbit-pdk/examples/count-vowels/members [extism/moonbit-pdk/examples/greet]: https://mooncakes.io/docs/#/extism/moonbit-pdk/examples/greet/members [extism/moonbit-pdk/examples/http-get]: https://mooncakes.io/docs/#/extism/moonbit-pdk/examples/http-get/members @@ -91,7 +95,6 @@ and include the `@host` import into your plugin: ```json { - "is-main": true, "import": [ "extism/moonbit-pdk/pdk/host" ], @@ -175,27 +178,36 @@ struct Add { } pub fn Add::from_json(value : @json.JsonValue) -> Add? { - let value = value.as_object()? - let a = value.get("a")?.as_number() - let b = value.get("b")?.as_number() + let value = match value.as_object() { + Some(v) => v + _ => return None + } + let a = match value.get("a") { + Some(v) => v + _ => return None + } + let a = a.as_number() + let b = match value.get("b") { + Some(v) => v + _ => return None + } + let b = b.as_number() match (a, b) { (Some(a), Some(b)) => Some({ a: a.to_int(), b: b.to_int() }) _ => None } } -pub fn Add::parse(s : String) -> Add!String { - match @json.parse(s)!! { +type! ParseError String derive(Show) + +pub fn Add::parse(s : String) -> Add!ParseError { + match @json.parse?(s) { Ok(jv) => match Add::from_json(jv) { Some(value) => value - None => { - raise "unable to parse Add \{s}" - } + None => raise ParseError("unable to parse Add \{s}") } - Err(e) => { - raise "unable to parse Add \{s}: \{e}" - } + Err(e) => raise ParseError("unable to parse Add \{s}: \{e}") } } @@ -203,16 +215,16 @@ struct Sum { sum : Int } -pub fn to_json(self: Sum) -> Json { +pub fn to_json(self : Sum) -> Json { { "sum": self.sum.to_json() } } pub fn add() -> Int { let input = @host.input_string() let params = try { - Add::parse(input)! + Add::parse!(input) } catch { - e => { + ParseError(e) => { @host.set_error(e) return 1 } @@ -230,7 +242,6 @@ Export your `add` function in `main/moon.pkg.json`: ```json { - "is-main": true, "import": [ "extism/moonbit-pdk/pdk/host" ], @@ -281,7 +292,6 @@ export your function: ```json { - "is-main": true, "import": [ "extism/moonbit-pdk/pdk/config", "extism/moonbit-pdk/pdk/host" @@ -335,7 +345,6 @@ export your function: ```json { - "is-main": true, "import": [ "extism/moonbit-pdk/pdk/host", "extism/moonbit-pdk/pdk/var" diff --git a/examples/add/README.md b/examples/add/README.md new file mode 100644 index 0000000..43c53ca --- /dev/null +++ b/examples/add/README.md @@ -0,0 +1,10 @@ +# examples/add + +The `add.wasm` plugin can be run from the top-level of the repo by +typing: + +```bash +$ ./build.sh +$ ./scripts/add.sh '{"a": 20, "b": 21}' +# => {"sum":41} +``` diff --git a/examples/add/add.mbt b/examples/add/add.mbt new file mode 100644 index 0000000..44f9ca4 --- /dev/null +++ b/examples/add/add.mbt @@ -0,0 +1,63 @@ +struct Add { + a : Int + b : Int +} + +pub fn Add::from_json(value : @json.JsonValue) -> Add? { + let value = match value.as_object() { + Some(v) => v + _ => return None + } + let a = match value.get("a") { + Some(v) => v + _ => return None + } + let a = a.as_number() + let b = match value.get("b") { + Some(v) => v + _ => return None + } + let b = b.as_number() + match (a, b) { + (Some(a), Some(b)) => Some({ a: a.to_int(), b: b.to_int() }) + _ => None + } +} + +type! ParseError String derive(Show) + +pub fn Add::parse(s : String) -> Add!ParseError { + match @json.parse?(s) { + Ok(jv) => + match Add::from_json(jv) { + Some(value) => value + None => raise ParseError("unable to parse Add \{s}") + } + Err(e) => raise ParseError("unable to parse Add \{s}: \{e}") + } +} + +struct Sum { + sum : Int +} + +pub fn to_json(self : Sum) -> Json { + { "sum": self.sum.to_json() } +} + +pub fn add() -> Int { + let input = @host.input_string() + let params = try { + Add::parse!(input) + } catch { + ParseError(e) => { + @host.set_error(e) + return 1 + } + } + // + let sum = { sum: params.a + params.b } + let json_value = sum.to_json() + @host.output_json_value(json_value) + 0 // success +} diff --git a/examples/add/moon.pkg.json b/examples/add/moon.pkg.json new file mode 100644 index 0000000..ef23840 --- /dev/null +++ b/examples/add/moon.pkg.json @@ -0,0 +1,13 @@ +{ + "import": [ + "extism/moonbit-pdk/pdk/host" + ], + "link": { + "wasm": { + "exports": [ + "add" + ], + "export-memory-name": "memory" + } + } +} \ No newline at end of file diff --git a/moon.mod.json b/moon.mod.json index 31a16b3..bcbe904 100644 --- a/moon.mod.json +++ b/moon.mod.json @@ -1,6 +1,6 @@ { "name": "extism/moonbit-pdk", - "version": "0.36.0", + "version": "0.37.0", "deps": {}, "readme": "README.md", "repository": "https://github.com/extism/moonbit-pdk", diff --git a/scripts/add.sh b/scripts/add.sh new file mode 100644 index 0000000..bff4de1 --- /dev/null +++ b/scripts/add.sh @@ -0,0 +1,2 @@ +#!/bin/bash -ex +extism call target/wasm/release/build/examples/add/add.wasm add --wasi --input "$@"