Skip to content

Commit

Permalink
improve examples using newer syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
lukewilliamboswell committed Jan 23, 2025
1 parent 9cbfe2a commit 7c9b772
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 38 deletions.
4 changes: 3 additions & 1 deletion examples/http-get-json.roc
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ main! = |_args|
{ foo } = Http.get!("http://localhost:8000", Json.utf8)?
# If you want to see an example of the server side, see basic-cli/ci/rust_http_server/src/main.rs

Stdout.line!("The json I received was: { foo: \"${foo}\" }")
Stdout.line!("The json I received was: { foo: \"${foo}\" }")?

Ok({})
30 changes: 18 additions & 12 deletions examples/optional.roc
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,33 @@ import json.OptionOrNull exposing [OptionOrNull]
Object : { first_name : Str, last_name : OptionOrNull Str }

main! = |_args|

# Demonstrate encoding an object with an Optional field
none_obj : Object
none_obj = { first_name: "Luke", last_name: OptionOrNull.none({}) }
null_obj : Object
null_obj = { first_name: "Luke", last_name: OptionOrNull.null({}) }
some_obj : Object
some_obj = { first_name: "Luke", last_name: OptionOrNull.some("Boswell") }

# noneJson == {"first_name":"Luke",}
# none_json == {"first_name":"Luke",}
none_json = Encode.to_bytes(none_obj, Json.utf8_with({ empty_encode_as_null: Json.encode_as_null_option({ record: Bool.false }) }))
try(Stdout.line!((none_json |> Str.from_utf8 |> Result.with_default("Failed to encode JSON"))))
Stdout.line!(Str.from_utf8(none_json) ?? "Failed to encode JSON")?

# nullNoneJson == {"first_name":"Luke","last_name":null}
# Demonstrate encoding an object with an Nullable field
null_obj : Object
null_obj = { first_name: "Luke", last_name: OptionOrNull.null({}) }

# null_none_json == {"first_name":"Luke","last_name":null}
null_none_json = Encode.to_bytes(none_obj, Json.utf8_with({ empty_encode_as_null: Json.encode_as_null_option({ record: Bool.true }) }))
try(Stdout.line!((null_none_json |> Str.from_utf8 |> Result.with_default("Failed to encode JSON"))))
Stdout.line!(Str.from_utf8(null_none_json) ?? "Failed to encode JSON")?

# nullJson == {"first_name":"Luke","last_name":null}
# null_json == {"first_name":"Luke","last_name":null}
null_json = Encode.to_bytes(null_obj, Json.utf8)
try(Stdout.line!((null_json |> Str.from_utf8 |> Result.with_default("Failed to encode JSON"))))
Stdout.line!(Str.from_utf8(null_json) ?? "Failed to encode JSON")?

# Demonstrate encoding an object with a Some field
some_obj : Object
some_obj = { first_name: "Luke", last_name: OptionOrNull.some("Boswell") }

# someJson == {"first_name":"Luke","last_name":"Boswell"}
# some_json == {"first_name":"Luke","last_name":"Boswell"}
some_json = Encode.to_bytes(some_obj, Json.utf8)
try(Stdout.line!((some_json |> Str.from_utf8 |> Result.with_default("Failed to encode JSON"))))
Stdout.line!(Str.from_utf8(some_json) ?? "Failed to encode JSON")?

Ok({})
10 changes: 5 additions & 5 deletions examples/simple1.roc
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ main! = |_args|

decoder = Json.utf8_with({ field_name_mapping: PascalCase })

decoded : Decode.DecodeResult ImageRequest
decoded = Decode.from_bytes_partial(request_body, decoder)
record : ImageRequest
record = Decode.from_bytes(request_body, decoder)?

when decoded.result is
Ok(record) -> Stdout.line!("Successfully decoded image, title:\"${record.image.title}\"")
Err(_) -> crash("Error, failed to decode image")
Stdout.line!("Successfully decoded image, title:\"${record.image.title}\"")?

Ok({})

ImageRequest : {
image : {
Expand Down
21 changes: 6 additions & 15 deletions examples/simple2.roc
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,15 @@ import json.Json
import "data.json" as request_body : List U8

main! = |_args|
decoder = Json.utf8_with({})

decoded : Decode.DecodeResult (List DataRequest)
decoded = Decode.from_bytes_partial(request_body, decoder)
list : List DataRequest
list = Decode.from_bytes(request_body, Json.utf8)?

when decoded.result is
Ok(list) ->
try(Stdout.line!("Successfully decoded list"))
Stdout.line!("Successfully decoded list")?
when List.get(list, 0) is
Ok(rec) -> Stdout.line!("Name of first person is: ${rec.lastname}")
Err(_) -> Stdout.line!("Error occurred in List.get")

Err(TooShort) -> Stdout.line!("A TooShort error occurred")
when List.get(list, 0) is
Ok(rec) -> Stdout.line!("Name of first person is: ${rec.lastname}")
Err(_) -> Stdout.line!("Error occurred in List.get")

DataRequest : {
id : I64,
Expand All @@ -31,7 +26,3 @@ DataRequest : {
gender : Str,
ipaddress : Str,
}
# =>
# Successfully decoded list
# Name of first person is: Penddreth
10 changes: 5 additions & 5 deletions examples/tuple.roc
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import json.Json
main! = |_args|
bytes = Str.to_utf8("[ [ 123,\n\"apples\" ], [ 456, \"oranges\" ]]")

decoded : Decode.DecodeResult (List FruitCount)
decoded = Decode.from_bytes_partial(bytes, Json.utf8)
tuple : List FruitCount
tuple = Decode.from_bytes(bytes, Json.utf8)?

when decoded.result is
Ok(tuple) -> Stdout.line!("Successfully decoded tuple, got ${to_str(tuple)}")
Err(_) -> crash("Error, failed to decode image")
Stdout.line!("Successfully decoded tuple, got ${to_str(tuple)}")?
Ok({})
FruitCount : (U32, Str)
Expand Down

0 comments on commit 7c9b772

Please sign in to comment.