Skip to content

Commit d277fbb

Browse files
committed
Handle numbers passed in as strings
1 parent 67dadf2 commit d277fbb

1 file changed

Lines changed: 37 additions & 9 deletions

File tree

src/SchemaUtils.elm

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2122,14 +2122,19 @@ typeToAnnotationWithMaybe type_ =
21222122
CliMonad.succeed Elm.Annotation.unit
21232123

21242124

2125-
basicTypeToAnnotation : Common.BasicType -> { a | format : Maybe String } -> CliMonad Elm.Annotation.Annotation
2126-
basicTypeToAnnotation basicType { format } =
2125+
basicTypeToAnnotation : Common.BasicType -> { a | format : Maybe String, pattern : Maybe String } -> CliMonad Elm.Annotation.Annotation
2126+
basicTypeToAnnotation basicType { format, pattern } =
21272127
let
21282128
default : Elm.Annotation.Annotation
21292129
default =
21302130
case basicType of
21312131
Common.String ->
2132-
Elm.Annotation.string
2132+
case pattern of
2133+
Just "^\\d+$" ->
2134+
Elm.Annotation.int
2135+
2136+
_ ->
2137+
Elm.Annotation.string
21332138

21342139
Common.Integer ->
21352140
Elm.Annotation.int
@@ -2407,14 +2412,19 @@ typeToEncoder type_ =
24072412
CliMonad.succeed (\_ -> Gen.Json.Encode.null)
24082413

24092414

2410-
basicTypeToEncoder : Common.BasicType -> { a | format : Maybe String } -> CliMonad (Elm.Expression -> Elm.Expression)
2411-
basicTypeToEncoder basicType { format } =
2415+
basicTypeToEncoder : Common.BasicType -> { a | format : Maybe String, pattern : Maybe String } -> CliMonad (Elm.Expression -> Elm.Expression)
2416+
basicTypeToEncoder basicType { format, pattern } =
24122417
let
24132418
default : Elm.Expression -> Elm.Expression
24142419
default =
24152420
case basicType of
24162421
Common.String ->
2417-
Gen.Json.Encode.call_.string
2422+
case pattern of
2423+
Just "^\\d+$" ->
2424+
\v -> v |> Gen.String.call_.fromInt |> Gen.Json.Encode.call_.string
2425+
2426+
_ ->
2427+
Gen.Json.Encode.call_.string
24182428

24192429
Common.Integer ->
24202430
Gen.Json.Encode.call_.int
@@ -2787,7 +2797,7 @@ basicTypeToDecoder basicType { format, const, pattern } =
27872797
(Elm.string
27882798
("Unexpected value: expected "
27892799
++ constToString expected
2790-
++ " got "
2800+
++ ", got "
27912801
)
27922802
)
27932803
(toString actual)
@@ -2801,6 +2811,24 @@ basicTypeToDecoder basicType { format, const, pattern } =
28012811
case basicType of
28022812
Common.String ->
28032813
case ( const, pattern ) of
2814+
( Nothing, Just "^\\d+$" ) ->
2815+
Gen.Json.Decode.string
2816+
|> Gen.Json.Decode.andThen
2817+
(\actual ->
2818+
Elm.Case.maybe (Gen.String.call_.toInt actual)
2819+
{ nothing =
2820+
Gen.Json.Decode.call_.fail
2821+
(Elm.Op.append
2822+
(Elm.string
2823+
"Unexpected value: expected a string matching \"^\\\\d+$\", got "
2824+
)
2825+
(Gen.Json.Encode.encode 0 (Gen.Json.Encode.call_.string actual))
2826+
)
2827+
, just = ( "i", \i -> Gen.Json.Decode.succeed i )
2828+
}
2829+
)
2830+
|> CliMonad.succeed
2831+
28042832
( Nothing, Just p ) ->
28052833
Elm.Let.letIn identity
28062834
|> Elm.Let.value "regex"
@@ -2818,7 +2846,7 @@ basicTypeToDecoder basicType { format, const, pattern } =
28182846
(Elm.string
28192847
("Unexpected value: expected a string matching "
28202848
++ Json.Encode.encode 0 (Json.Encode.string p)
2821-
++ " got "
2849+
++ ", got "
28222850
)
28232851
)
28242852
(Gen.Json.Encode.encode 0 (Gen.Json.Encode.call_.string actual))
@@ -2876,7 +2904,7 @@ basicTypeToDecoder basicType { format, const, pattern } =
28762904
(Elm.string
28772905
("Unexpected value: expected "
28782906
++ boolToString expected
2879-
++ " got "
2907+
++ ", got "
28802908
++ boolToString (not expected)
28812909
)
28822910
)

0 commit comments

Comments
 (0)