mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-27 22:09:09 +00:00
update inspect examples
This commit is contained in:
parent
9b181e1b3f
commit
aeabf96e80
4 changed files with 18 additions and 321 deletions
|
@ -14,12 +14,7 @@ interface Community
|
|||
Community := {
|
||||
people : List Person,
|
||||
friends : List (Set Nat),
|
||||
}
|
||||
implements [
|
||||
Inspect {
|
||||
toInspector: inspectCommunity,
|
||||
},
|
||||
]
|
||||
} implements [Inspect]
|
||||
|
||||
Person := {
|
||||
firstName : Str,
|
||||
|
@ -27,12 +22,7 @@ Person := {
|
|||
age : U8,
|
||||
hasBeard : Bool,
|
||||
favoriteColor : Color,
|
||||
}
|
||||
implements [
|
||||
Inspect {
|
||||
toInspector: inspectPerson,
|
||||
},
|
||||
]
|
||||
} implements [Inspect]
|
||||
|
||||
Color : [
|
||||
Red,
|
||||
|
@ -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
|
||||
|
|
|
@ -25,6 +25,7 @@ GuiFormatter := { nodes : List Elem }
|
|||
record: record,
|
||||
bool: bool,
|
||||
str: str,
|
||||
function: function,
|
||||
opaque: opaque,
|
||||
u8: u8,
|
||||
i8: i8,
|
||||
|
@ -36,6 +37,7 @@ GuiFormatter := { nodes : List Elem }
|
|||
i64: i64,
|
||||
u128: u128,
|
||||
i128: i128,
|
||||
nat: nat,
|
||||
f32: f32,
|
||||
f64: f64,
|
||||
dec: dec,
|
||||
|
@ -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 ->
|
||||
|
@ -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
|
||||
|
|
|
@ -1,246 +0,0 @@
|
|||
interface LogFormatter
|
||||
exposes [
|
||||
LogFormatter,
|
||||
toStr,
|
||||
]
|
||||
imports []
|
||||
|
||||
## Creates String representations of Roc values, for use in inspect-logging.roc
|
||||
|
||||
LogFormatter := { data : Str }
|
||||
implements [
|
||||
InspectFormatter {
|
||||
init: init,
|
||||
list: list,
|
||||
set: set,
|
||||
dict: dict,
|
||||
tag: tag,
|
||||
tuple: tuple,
|
||||
record: record,
|
||||
bool: bool,
|
||||
str: str,
|
||||
opaque: opaque,
|
||||
u8: u8,
|
||||
i8: i8,
|
||||
u16: u16,
|
||||
i16: i16,
|
||||
u32: u32,
|
||||
i32: i32,
|
||||
u64: u64,
|
||||
i64: i64,
|
||||
u128: u128,
|
||||
i128: i128,
|
||||
f32: f32,
|
||||
f64: f64,
|
||||
dec: dec,
|
||||
|
||||
},
|
||||
]
|
||||
|
||||
init : {} -> LogFormatter
|
||||
init = \{} -> @LogFormatter { data: "" }
|
||||
|
||||
list : list, ElemWalker (LogFormatter, Bool) list elem, (elem -> Inspector LogFormatter) -> Inspector LogFormatter
|
||||
list = \content, walkFn, toInspector ->
|
||||
f0 <- Inspect.custom
|
||||
write f0 "["
|
||||
|> \f1 ->
|
||||
(f2, prependSep), elem <- walkFn content (f1, Bool.false)
|
||||
f3 =
|
||||
if prependSep then
|
||||
write f2 ", "
|
||||
else
|
||||
f2
|
||||
|
||||
elem
|
||||
|> toInspector
|
||||
|> Inspect.apply f3
|
||||
|> \f4 -> (f4, Bool.true)
|
||||
|> .0
|
||||
|> write "]"
|
||||
|
||||
set : set, ElemWalker (LogFormatter, Bool) set elem, (elem -> Inspector LogFormatter) -> Inspector LogFormatter
|
||||
set = \content, walkFn, toInspector ->
|
||||
f0 <- Inspect.custom
|
||||
write f0 "{"
|
||||
|> \f1 ->
|
||||
(f2, prependSep), elem <- walkFn content (f1, Bool.false)
|
||||
f3 =
|
||||
if prependSep then
|
||||
write f2 ", "
|
||||
else
|
||||
f2
|
||||
|
||||
elem
|
||||
|> toInspector
|
||||
|> Inspect.apply f3
|
||||
|> \f4 -> (f4, Bool.true)
|
||||
|> .0
|
||||
|> write "}"
|
||||
|
||||
dict : dict, KeyValWalker (LogFormatter, Bool) dict key value, (key -> Inspector LogFormatter), (value -> Inspector LogFormatter) -> Inspector LogFormatter
|
||||
dict = \d, walkFn, keyToInspector, valueToInspector ->
|
||||
f0 <- Inspect.custom
|
||||
write f0 "{"
|
||||
|> \f1 ->
|
||||
(f2, prependSep), key, value <- walkFn d (f1, Bool.false)
|
||||
f3 =
|
||||
if prependSep then
|
||||
write f2 ", "
|
||||
else
|
||||
f2
|
||||
|
||||
Inspect.apply (keyToInspector key) f3
|
||||
|> write ": "
|
||||
|> \x -> Inspect.apply (valueToInspector value) x
|
||||
|> \f4 -> (f4, Bool.true)
|
||||
|> .0
|
||||
|> write "}"
|
||||
|
||||
tag : Str, List (Inspector LogFormatter) -> Inspector LogFormatter
|
||||
tag = \name, fields ->
|
||||
if List.isEmpty fields then
|
||||
f0 <- Inspect.custom
|
||||
write f0 name
|
||||
else
|
||||
f0 <- Inspect.custom
|
||||
write f0 "("
|
||||
|> write name
|
||||
|> \f1 ->
|
||||
f2, inspector <- List.walk fields f1
|
||||
write f2 " "
|
||||
|> \x -> Inspect.apply inspector x
|
||||
|> write ")"
|
||||
|
||||
tuple : List (Inspector LogFormatter) -> Inspector LogFormatter
|
||||
tuple = \fields ->
|
||||
f0 <- Inspect.custom
|
||||
write f0 "("
|
||||
|> \f1 ->
|
||||
(f2, prependSep), inspector <- List.walk fields (f1, Bool.false)
|
||||
f3 =
|
||||
if prependSep then
|
||||
write f2 ", "
|
||||
else
|
||||
f2
|
||||
|
||||
Inspect.apply inspector f3
|
||||
|> \f4 -> (f4, Bool.true)
|
||||
|> .0
|
||||
|> write ")"
|
||||
|
||||
record : List { key : Str, value : Inspector LogFormatter } -> Inspector LogFormatter
|
||||
record = \fields ->
|
||||
f0 <- Inspect.custom
|
||||
write f0 "{"
|
||||
|> \f1 ->
|
||||
(f2, prependSep), { key, value } <- List.walk fields (f1, Bool.false)
|
||||
f3 =
|
||||
if prependSep then
|
||||
write f2 ", "
|
||||
else
|
||||
f2
|
||||
|
||||
write f3 key
|
||||
|> write ": "
|
||||
|> \x -> Inspect.apply value x
|
||||
|> \f4 -> (f4, Bool.true)
|
||||
|> .0
|
||||
|> write "}"
|
||||
|
||||
bool : Bool -> Inspector LogFormatter
|
||||
bool = \b ->
|
||||
if b then
|
||||
f0 <- Inspect.custom
|
||||
write f0 "true"
|
||||
else
|
||||
f0 <- Inspect.custom
|
||||
write f0 "false"
|
||||
|
||||
str : Str -> Inspector LogFormatter
|
||||
str = \s ->
|
||||
f0 <- Inspect.custom
|
||||
f0
|
||||
|> write "\""
|
||||
|> write s
|
||||
|> write "\""
|
||||
|
||||
opaque : Str -> Inspector LogFormatter
|
||||
opaque = \s ->
|
||||
f0 <- Inspect.custom
|
||||
f0
|
||||
|> write "<"
|
||||
|> write s
|
||||
|> write ">"
|
||||
|
||||
u8 : U8 -> Inspector LogFormatter
|
||||
u8 = \num ->
|
||||
f0 <- Inspect.custom
|
||||
write f0 (num |> Num.toStr)
|
||||
|
||||
i8 : I8 -> Inspector LogFormatter
|
||||
i8 = \num ->
|
||||
f0 <- Inspect.custom
|
||||
write f0 (num |> Num.toStr)
|
||||
|
||||
u16 : U16 -> Inspector LogFormatter
|
||||
u16 = \num ->
|
||||
f0 <- Inspect.custom
|
||||
write f0 (num |> Num.toStr)
|
||||
|
||||
i16 : I16 -> Inspector LogFormatter
|
||||
i16 = \num ->
|
||||
f0 <- Inspect.custom
|
||||
write f0 (num |> Num.toStr)
|
||||
|
||||
u32 : U32 -> Inspector LogFormatter
|
||||
u32 = \num ->
|
||||
f0 <- Inspect.custom
|
||||
write f0 (num |> Num.toStr)
|
||||
|
||||
i32 : I32 -> Inspector LogFormatter
|
||||
i32 = \num ->
|
||||
f0 <- Inspect.custom
|
||||
write f0 (num |> Num.toStr)
|
||||
|
||||
u64 : U64 -> Inspector LogFormatter
|
||||
u64 = \num ->
|
||||
f0 <- Inspect.custom
|
||||
write f0 (num |> Num.toStr)
|
||||
|
||||
i64 : I64 -> Inspector LogFormatter
|
||||
i64 = \num ->
|
||||
f0 <- Inspect.custom
|
||||
write f0 (num |> Num.toStr)
|
||||
|
||||
u128 : U128 -> Inspector LogFormatter
|
||||
u128 = \num ->
|
||||
f0 <- Inspect.custom
|
||||
write f0 (num |> Num.toStr)
|
||||
|
||||
i128 : I128 -> Inspector LogFormatter
|
||||
i128 = \num ->
|
||||
f0 <- Inspect.custom
|
||||
write f0 (num |> Num.toStr)
|
||||
|
||||
f32 : F32 -> Inspector LogFormatter
|
||||
f32 = \num ->
|
||||
f0 <- Inspect.custom
|
||||
write f0 (num |> Num.toStr)
|
||||
|
||||
f64 : F64 -> Inspector LogFormatter
|
||||
f64 = \num ->
|
||||
f0 <- Inspect.custom
|
||||
write f0 (num |> Num.toStr)
|
||||
|
||||
dec : Dec -> Inspector LogFormatter
|
||||
dec = \num ->
|
||||
f0 <- Inspect.custom
|
||||
write f0 (num |> Num.toStr)
|
||||
|
||||
write : LogFormatter, Str -> LogFormatter
|
||||
write = \@LogFormatter { data }, added ->
|
||||
@LogFormatter { data: Str.concat data added }
|
||||
|
||||
toStr : LogFormatter -> Str
|
||||
toStr = \@LogFormatter { data } -> data
|
|
@ -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
|
||||
|
@ -36,5 +35,5 @@ main =
|
|||
|> Community.addFriend 0 2
|
||||
|> Community.addFriend 1 2
|
||||
|> Inspect.inspect
|
||||
|> LogFormatter.toStr
|
||||
|> Inspect.toDbgStr
|
||||
|> Stdout.line
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue