Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 37 additions & 9 deletions src/SchemaUtils.elm
Original file line number Diff line number Diff line change
Expand Up @@ -2088,14 +2088,19 @@ typeToAnnotationWithMaybe type_ =
CliMonad.succeed Elm.Annotation.unit


basicTypeToAnnotation : Common.BasicType -> { a | format : Maybe String } -> CliMonad Elm.Annotation.Annotation
basicTypeToAnnotation basicType { format } =
basicTypeToAnnotation : Common.BasicType -> { a | format : Maybe String, pattern : Maybe String } -> CliMonad Elm.Annotation.Annotation
basicTypeToAnnotation basicType { format, pattern } =
let
default : Elm.Annotation.Annotation
default =
case basicType of
Common.String ->
Elm.Annotation.string
case pattern of
Just "^\\d+$" ->
Elm.Annotation.int

_ ->
Elm.Annotation.string

Common.Integer ->
Elm.Annotation.int
Expand Down Expand Up @@ -2375,14 +2380,19 @@ typeToEncoder type_ =
CliMonad.succeed (\_ -> Gen.Json.Encode.null)


basicTypeToEncoder : Common.BasicType -> { a | format : Maybe String } -> CliMonad (Elm.Expression -> Elm.Expression)
basicTypeToEncoder basicType { format } =
basicTypeToEncoder : Common.BasicType -> { a | format : Maybe String, pattern : Maybe String } -> CliMonad (Elm.Expression -> Elm.Expression)
basicTypeToEncoder basicType { format, pattern } =
let
default : Elm.Expression -> Elm.Expression
default =
case basicType of
Common.String ->
Gen.Json.Encode.call_.string
case pattern of
Just "^\\d+$" ->
\v -> v |> Gen.String.call_.fromInt |> Gen.Json.Encode.call_.string

_ ->
Gen.Json.Encode.call_.string

Common.Integer ->
Gen.Json.Encode.call_.int
Expand Down Expand Up @@ -2762,7 +2772,7 @@ basicTypeToDecoder basicType { format, const, pattern } =
(Elm.string
("Unexpected value: expected "
++ constToString expected
++ " got "
++ ", got "
)
)
(toString actual)
Expand All @@ -2776,6 +2786,24 @@ basicTypeToDecoder basicType { format, const, pattern } =
case basicType of
Common.String ->
case ( const, pattern ) of
( Nothing, Just "^\\d+$" ) ->
Gen.Json.Decode.string
|> Gen.Json.Decode.andThen
(\actual ->
Elm.Case.maybe (Gen.String.call_.toInt actual)
{ nothing =
Gen.Json.Decode.call_.fail
(Elm.Op.append
(Elm.string
"Unexpected value: expected a string matching \"^\\\\d+$\", got "
)
(Gen.Json.Encode.encode 0 (Gen.Json.Encode.call_.string actual))
)
, just = ( "i", \i -> Gen.Json.Decode.succeed i )
}
)
|> CliMonad.succeed

( Nothing, Just p ) ->
Elm.Let.letIn identity
|> Elm.Let.value "regex"
Expand All @@ -2793,7 +2821,7 @@ basicTypeToDecoder basicType { format, const, pattern } =
(Elm.string
("Unexpected value: expected a string matching "
++ Json.Encode.encode 0 (Json.Encode.string p)
++ " got "
++ ", got "
)
)
(Gen.Json.Encode.encode 0 (Gen.Json.Encode.call_.string actual))
Expand Down Expand Up @@ -2851,7 +2879,7 @@ basicTypeToDecoder basicType { format, const, pattern } =
(Elm.string
("Unexpected value: expected "
++ boolToString expected
++ " got "
++ ", got "
++ boolToString (not expected)
)
)
Expand Down
Loading