Skip to content

Commit

Permalink
Merge pull request #159 from wolfadex/const-bool-tweak
Browse files Browse the repository at this point in the history
In the bool case we can just put the expected and actual values inside the string
  • Loading branch information
wolfadex authored Nov 5, 2024
2 parents d67b744 + df9cd54 commit a04b974
Showing 1 changed file with 27 additions and 10 deletions.
37 changes: 27 additions & 10 deletions src/SchemaUtils.elm
Original file line number Diff line number Diff line change
Expand Up @@ -1112,21 +1112,38 @@ typeToDecoder qualify type_ =
base Elm.float String.fromFloat Gen.String.call_.fromFloat const Gen.Json.Decode.float

Common.Bool { const } ->
base Elm.bool
(\b ->
let
boolToString : Bool -> String
boolToString b =
if b then
"true"

else
"false"
)
(\s ->
Elm.ifThen s
(Elm.string "true")
(Elm.string "false")
)
const
Gen.Json.Decode.bool
in
(case const of
Nothing ->
Gen.Json.Decode.bool

Just expected ->
Gen.Json.Decode.bool
|> Gen.Json.Decode.andThen
(\actual ->
Elm.ifThen
(Elm.Op.equal actual (Elm.bool expected))
(Gen.Json.Decode.succeed actual)
(Gen.Json.Decode.call_.fail
(Elm.string
("Unexpected value: expected "
++ boolToString expected
++ " got "
++ boolToString (not expected)
)
)
)
)
)
|> CliMonad.succeed

Common.Null ->
CliMonad.succeed (Gen.Json.Decode.null Elm.unit)
Expand Down

0 comments on commit a04b974

Please sign in to comment.