Skip to content

Commit

Permalink
update inspect examples
Browse files Browse the repository at this point in the history
  • Loading branch information
bhansconnect committed Nov 28, 2023
1 parent 7402140 commit c1dd9f9
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 321 deletions.
72 changes: 2 additions & 70 deletions examples/Community.roc
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,15 @@ interface Community
Community := {
people : List Person,
friends : List (Set Nat),
}
implements [
Inspect {
toInspector: inspectCommunity,
},
]
} implements [Inspect]

Person := {
firstName : Str,
lastName : Str,
age : U8,
hasBeard : Bool,
favoriteColor : Color,
}
implements [
Inspect {
toInspector: inspectPerson,
},
]
} implements [Inspect]

Color : [
Red,
Expand Down Expand Up @@ -90,61 +80,3 @@ walkFriendNames = \@Community { people, friends }, s0, nextFn ->
(nextFn s1 personName friendNames, id + 1)
out

# The functions below will be auto-generated in the future
inspectCommunity : Community -> Inspector f where f implements InspectFormatter
inspectCommunity = \@Community { people, friends } ->
f0 <- Inspect.custom
[
{ key: "people", value: Inspect.list people List.walk Inspect.toInspector },
{
key: "friends",
value: Inspect.list
friends
List.walk
(\s -> Inspect.set
s
Set.walk
(\num -> num |> Num.toU64 |> Inspect.u64)
),
# value: Inspect.dict
# (@Community { people, friends })
# walkFriendNames
# Inspect.str
# (\s -> Inspect.set s Set.walk Inspect.str),
},
]
|> Inspect.record
|> Inspect.apply f0

inspectPerson : Person -> Inspector f where f implements InspectFormatter
inspectPerson = \@Person { firstName, lastName, age, hasBeard, favoriteColor } ->
# In practice, this would never be done manually due to autoderive.
# Instead you would just write:
# Inspect.inspect innerRecord
# This is what the auto-derive would generate.

f0 <- Inspect.custom

favoriteColorTag =
when favoriteColor is
Red ->
Inspect.tag "Red" []

Green ->
Inspect.tag "Green" []

Blue ->
Inspect.tag "Blue" []

RGB (r, g, b) ->
Inspect.tag "RGB" [Inspect.tuple [Inspect.u8 r, Inspect.u8 g, Inspect.u8 b]]

[
{ key: "firstName", value: Inspect.str firstName },
{ key: "lastName", value: Inspect.str lastName },
{ key: "age", value: Inspect.u8 age },
{ key: "hasBeard", value: Inspect.bool hasBeard },
{ key: "favoriteColor", value: favoriteColorTag },
]
|> Inspect.record
|> Inspect.apply f0
18 changes: 15 additions & 3 deletions examples/GuiFormatter.roc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ GuiFormatter := { nodes : List Elem }
record: record,
bool: bool,
str: str,
function: function,
opaque: opaque,
u8: u8,
i8: i8,
Expand All @@ -36,6 +37,7 @@ GuiFormatter := { nodes : List Elem }
i64: i64,
u128: u128,
i128: i128,
nat: nat,
f32: f32,
f64: f64,
dec: dec,
Expand Down Expand Up @@ -149,10 +151,15 @@ str = \s ->
f0 <- Inspect.custom
addNode f0 (Text "\"\(s)\"")

opaque : Str -> Inspector GuiFormatter
opaque = \s ->
opaque : * -> Inspector GuiFormatter
opaque = \_ ->
f0 <- Inspect.custom
addNode f0 (Text "<\(s)>")
addNode f0 (Text "<opaque>")

function : * -> Inspector GuiFormatter
function = \_ ->
f0 <- Inspect.custom
addNode f0 (Text "<function>")

u8 : U8 -> Inspector GuiFormatter
u8 = \num ->
Expand Down Expand Up @@ -204,6 +211,11 @@ i128 = \num ->
f0 <- Inspect.custom
addNode f0 (num |> Num.toStr |> Text)

nat : Nat -> Inspector GuiFormatter
nat = \num ->
f0 <- Inspect.custom
addNode f0 (num |> Num.toStr |> Text)

f32 : F32 -> Inspector GuiFormatter
f32 = \num ->
f0 <- Inspect.custom
Expand Down
246 changes: 0 additions & 246 deletions examples/LogFormatter.roc

This file was deleted.

3 changes: 1 addition & 2 deletions examples/inspect-logging.roc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ app "inspect-logging"
packages { pf: "https://github.com/roc-lang/basic-cli/releases/download/0.6.0/QOQW08n38nHHrVVkJNiPIjzjvbR3iMjXeFY5w1aT46w.tar.br" }
imports [
pf.Stdout,
LogFormatter,
Community,
]
provides [main] to pf
Expand Down Expand Up @@ -36,5 +35,5 @@ main =
|> Community.addFriend 0 2
|> Community.addFriend 1 2
|> Inspect.inspect
|> LogFormatter.toStr
|> Inspect.toDbgStr
|> Stdout.line

0 comments on commit c1dd9f9

Please sign in to comment.