Skip to content

Commit

Permalink
update example simple2
Browse files Browse the repository at this point in the history
  • Loading branch information
lukewilliamboswell committed May 3, 2024
1 parent 78ea1a4 commit 35ffbe0
Showing 1 changed file with 27 additions and 28 deletions.
55 changes: 27 additions & 28 deletions examples/simple2.roc
Original file line number Diff line number Diff line change
@@ -1,39 +1,38 @@
app "simple2"
packages {
cli: "https://github.com/roc-lang/basic-cli/releases/download/0.8.1/x8URkvfyi9I0QhmVG98roKBUs_AZRkLFwFJVJ3942YA.tar.br",
json: "../package/main.roc", # use release URL (ends in tar.br) for local example, see github.com/lukewilliamboswell/roc-json/releases
}
imports [
cli.Stdout,
cli.Task.{ await },
json.Core.{ jsonWithOptions },
"data.json" as requestBody : List U8,
]
provides [main] to cli
app [main] {
cli: platform "https://github.com/roc-lang/basic-cli/releases/download/0.8.1/x8URkvfyi9I0QhmVG98roKBUs_AZRkLFwFJVJ3942YA.tar.br",
json: "../package/main.roc", # use release URL (ends in tar.br) for local example, see github.com/lukewilliamboswell/roc-json/releases
}

import cli.Stdout
import cli.Task
import json.Core
import "data.json" as requestBody : List U8

main =
decoder = jsonWithOptions { }
decoder = Core.jsonWithOptions {}

decoded : Decode.DecodeResult (List DataRequest)
decoded = Decode.fromBytesPartial requestBody decoder

when decoded.result is
Ok list ->
_ <- Stdout.line "Successfully decoded list" |> Task.await
rec1 = List.get list 0
when rec1 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"

Ok 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"

DataRequest : {
id : I64,
firstname : Str,
lastname : Str,
email : Str,
gender : Str,
ipaddress : Str,
id : I64,
firstname : Str,
lastname : Str,
email : Str,
gender : Str,
ipaddress : Str,
}

# =>
# Successfully decoded list
# Name of first person is: Penddreth
# Name of first person is: Penddreth

0 comments on commit 35ffbe0

Please sign in to comment.