Skip to content

Commit

Permalink
Update to moonc v0.1.20240823+c622a5ab0
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 23, 2024
1 parent 585cd0c commit df2968b
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 57 deletions.
27 changes: 7 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,23 +177,10 @@ struct Add {
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() })
pub fn Add::from_json(value : Json) -> Add? {
// From: https://github.com/moonbitlang/core/issues/892#issuecomment-2306068783
match value {
{ "a": Number(a), "b": Number(b) } => Some({ a: a.to_int(), b: b.to_int() })
_ => None
}
}
Expand Down Expand Up @@ -540,9 +527,9 @@ The code has been updated to support compiler:

```bash
$ moon version --all
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
moon 0.1.20240823 (f608aa3 2024-08-23) ~/.moon/bin/moon
moonc v0.1.20240823+c622a5ab0 ~/.moon/bin/moonc
moonrun 0.1.20240822 (efda7a5 2024-08-22) ~/.moon/bin/moonrun
```

Use `moonup` to manage `moon` compiler versions:
Expand Down
25 changes: 5 additions & 20 deletions examples/add/add.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,10 @@ struct Add {
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() })
pub fn Add::from_json(value : Json) -> Add? {
// From: https://github.com/moonbitlang/core/issues/892#issuecomment-2306068783
match value {
{ "a": Number(a), "b": Number(b) } => Some({ a: a.to_int(), b: b.to_int() })
_ => None
}
}
Expand Down Expand Up @@ -61,9 +48,7 @@ pub fn add() -> Int {
test "Sum::to_json works as expected" {
let sum = { sum: 42 }
let got = sum.to_json().stringify()
// See: https://github.com/moonbitlang/core/issues/878
// for trailing ".0" on the integers.
let want =
#|{"sum":42.0}
#|{"sum":42}
assert_eq!(got, want)
}
4 changes: 1 addition & 3 deletions examples/arrays/all-three.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ pub fn process_all_three(all3 : AllThree) -> AllThree {
test "AllThree::to_json works as expected" {
let all3 = { ints: [1, 2, 3], floats: [1, 2, 3], strings: ["1", "2", "3"] }
let got = all3.to_json().stringify()
// See: https://github.com/moonbitlang/core/issues/878
// for trailing ".0" on the integers.
let want =
#|{"ints":[1.0,2.0,3.0],"floats":[1.0,2.0,3.0],"strings":["1","2","3"]}
#|{"ints":[1,2,3],"floats":[1,2,3],"strings":["1","2","3"]}
assert_eq!(got, want)
}
4 changes: 1 addition & 3 deletions examples/count-vowels/count-vowels.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ pub fn count_vowels() -> Int {
test "VowelReport::to_json works as expected" {
let vowel_report = { count: 1, total: 2, vowels: "some string" }
let got = vowel_report.to_json().stringify()
// See: https://github.com/moonbitlang/core/issues/878
// for trailing ".0" on the integers.
let want =
#|{"count":1.0,"total":2.0,"vowels":"some string"}
#|{"count":1,"total":2,"vowels":"some string"}
assert_eq!(got, want)
}
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.38.0",
"version": "0.39.0",
"deps": {},
"readme": "README.md",
"repository": "https://github.com/extism/moonbit-pdk",
Expand Down
4 changes: 2 additions & 2 deletions pdk/host/host.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ pub fn output_string(s : String) -> Unit {
@pdk.ToUtf8::to_utf8(s) |> output()
}

/// `output_json_value` converts a MoonBit @json.JsonValue to an Extism JSON string
/// `output_json_value` converts a MoonBit `Json` to an Extism JSON string
/// and sends it to the host.
pub fn output_json_value(j : @json.JsonValue) -> Unit {
pub fn output_json_value(j : Json) -> Unit {
j.stringify() |> output_string()
}

Expand Down
4 changes: 2 additions & 2 deletions pdk/host/memory.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ 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 {
/// in host memory that is converted from this `Json`.
pub fn Memory::allocate_json_value(j : Json) -> Memory {
j.stringify() |> Memory::allocate_string()
}

Expand Down
10 changes: 4 additions & 6 deletions pdk/http/http.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,16 @@ test "Request::to_json works as expected" {
header: { "key1": "one", "key2": "two" },
url: "https://example.com",
}
let got = request.to_json().stringify()
// See: https://github.com/moonbitlang/core/issues/893
let got = request.to_json().stringify(escape_slash=false)
let want =
#|{"method":"GET","header":{"key1":"one","key2":"two"},"url":"https:\/\/example.com"}
#|{"method":"GET","header":{"key1":"one","key2":"two"},"url":"https://example.com"}
assert_eq!(got, want)
}

test "Json::stringify works on strings" {
let url = "https://example.com"
let got = url.to_json().stringify()
// See: https://github.com/moonbitlang/core/issues/893
let got = url.to_json().stringify(escape_slash=false)
let want =
#|"https:\/\/example.com"
#|"https://example.com"
assert_eq!(got, want)
}

0 comments on commit df2968b

Please sign in to comment.