Skip to content

Commit

Permalink
Update README.md and examples
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 22, 2024
1 parent 155a6d6 commit 0010bde
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 19 deletions.
45 changes: 27 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -91,7 +95,6 @@ and include the `@host` import into your plugin:

```json
{
"is-main": true,
"import": [
"extism/moonbit-pdk/pdk/host"
],
Expand Down Expand Up @@ -175,44 +178,53 @@ 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}")
}
}

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
}
Expand All @@ -230,7 +242,6 @@ Export your `add` function in `main/moon.pkg.json`:

```json
{
"is-main": true,
"import": [
"extism/moonbit-pdk/pdk/host"
],
Expand Down Expand Up @@ -281,7 +292,6 @@ export your function:

```json
{
"is-main": true,
"import": [
"extism/moonbit-pdk/pdk/config",
"extism/moonbit-pdk/pdk/host"
Expand Down Expand Up @@ -335,7 +345,6 @@ export your function:

```json
{
"is-main": true,
"import": [
"extism/moonbit-pdk/pdk/host",
"extism/moonbit-pdk/pdk/var"
Expand Down
10 changes: 10 additions & 0 deletions examples/add/README.md
Original file line number Diff line number Diff line change
@@ -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}
```
63 changes: 63 additions & 0 deletions examples/add/add.mbt
Original file line number Diff line number Diff line change
@@ -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
}
13 changes: 13 additions & 0 deletions examples/add/moon.pkg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"import": [
"extism/moonbit-pdk/pdk/host"
],
"link": {
"wasm": {
"exports": [
"add"
],
"export-memory-name": "memory"
}
}
}
2 changes: 1 addition & 1 deletion moon.mod.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 2 additions & 0 deletions scripts/add.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash -ex
extism call target/wasm/release/build/examples/add/add.wasm add --wasi --input "$@"

0 comments on commit 0010bde

Please sign in to comment.