Skip to content

Commit

Permalink
Merge pull request #144 from wolfadex/time-what-is-time
Browse files Browse the repository at this point in the history
Add support for datetime and date
  • Loading branch information
wolfadex authored Sep 23, 2024
2 parents 0ebe901 + 9e0fb82 commit 748262a
Show file tree
Hide file tree
Showing 11 changed files with 11,648 additions and 7 deletions.
2,424 changes: 2,424 additions & 0 deletions codegen/Gen/Date.elm

Large diffs are not rendered by default.

3,114 changes: 3,114 additions & 0 deletions codegen/Gen/Parser.elm

Large diffs are not rendered by default.

3,220 changes: 3,220 additions & 0 deletions codegen/Gen/Parser/Advanced.elm

Large diffs are not rendered by default.

631 changes: 631 additions & 0 deletions codegen/Gen/Rfc3339.elm

Large diffs are not rendered by default.

1,211 changes: 1,211 additions & 0 deletions codegen/Gen/Time.elm

Large diffs are not rendered by default.

936 changes: 936 additions & 0 deletions codegen/Gen/Time/Extra.elm

Large diffs are not rendered by default.

11 changes: 9 additions & 2 deletions codegen/elm.codegen.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,15 @@
"elm/json": "1.1.3",
"elm-community/json-extra": "4.3.0",
"elm/url": "1.0.0",
"dillonkearns/elm-pages": "10.1.0"
"dillonkearns/elm-pages": "10.1.0",
"justinmimbs/time-extra": "1.2.0",
"justinmimbs/date": "4.1.0",
"elm/time": "1.0.0",
"elm/parser": "1.1.0",
"wolfadex/elm-rfc3339": "2.0.0"
},
"local": ["helpers"]
"local": [
"helpers"
]
}
}
9 changes: 6 additions & 3 deletions example/elm.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,17 @@
"elm/core": "1.0.5",
"elm/http": "2.0.0",
"elm/json": "1.1.3",
"elm/parser": "1.1.0",
"elm/time": "1.0.0",
"elm/url": "1.0.0",
"elm-community/json-extra": "4.3.0"
"elm-community/json-extra": "4.3.0",
"justinmimbs/date": "4.1.0",
"justinmimbs/time-extra": "1.2.0",
"wolfadex/elm-rfc3339": "2.0.0"
},
"indirect": {
"elm/file": "1.0.5",
"elm/html": "1.0.0",
"elm/parser": "1.1.0",
"elm/time": "1.0.0",
"elm/virtual-dom": "1.0.3",
"rtfeldman/elm-iso8601-date-strings": "1.1.4"
}
Expand Down
9 changes: 8 additions & 1 deletion src/Cli.elm
Original file line number Diff line number Diff line change
Expand Up @@ -746,7 +746,14 @@ printSuccessMessageAndWarnings ( outputPaths, warnings ) =

optionalPackages : List String
optionalPackages =
[ "elm/bytes", "elm/url" ]
[ "elm/bytes"
, "elm/parser"
, "elm/time"
, "elm/url"
, "justinmimbs/date"
, "justinmimbs/time-extra"
, "wolfadex/elm-rfc3339"
]

toInstall : String -> String
toInstall dependency =
Expand Down
2 changes: 2 additions & 0 deletions src/Common.elm
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,8 @@ type Type
| Ref (List String)
| Bytes
| Unit
| Date
| DateTime


type alias Object =
Expand Down
88 changes: 87 additions & 1 deletion src/SchemaUtils.elm
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,16 @@ import Elm.ToString
import FastDict
import Gen.Basics
import Gen.Bytes
import Gen.Date
import Gen.Json.Decode
import Gen.Json.Encode
import Gen.List
import Gen.Maybe
import Gen.OpenApi.Common
import Gen.Parser.Advanced
import Gen.Result
import Gen.Rfc3339
import Gen.Time
import Json.Decode
import Json.Schema.Definitions
import Maybe.Extra
Expand Down Expand Up @@ -150,7 +155,22 @@ schemaToType qualify schema =
objectSchemaToType qualify subSchema

Json.Schema.Definitions.StringType ->
CliMonad.succeed { type_ = Common.String, documentation = subSchema.description }
case subSchema.format of
Just "date-time" ->
CliMonad.succeed { type_ = Common.DateTime, documentation = subSchema.description }

Just "date" ->
CliMonad.succeed { type_ = Common.Date, documentation = subSchema.description }

Just "password" ->
CliMonad.succeed { type_ = Common.String, documentation = subSchema.description }

Just format ->
CliMonad.succeed { type_ = Common.String, documentation = subSchema.description }
|> CliMonad.withWarning ("Format \"" ++ format ++ "\" not supported - using string instead")

Nothing ->
CliMonad.succeed { type_ = Common.String, documentation = subSchema.description }

Json.Schema.Definitions.IntegerType ->
CliMonad.succeed { type_ = Common.Int, documentation = subSchema.description }
Expand Down Expand Up @@ -542,6 +562,12 @@ typeToAnnotation qualify type_ =
Common.String ->
CliMonad.succeed Elm.Annotation.string

Common.DateTime ->
CliMonad.succeed Gen.Time.annotation_.posix

Common.Date ->
CliMonad.succeed Gen.Date.annotation_.date

Common.Int ->
CliMonad.succeed Elm.Annotation.int

Expand Down Expand Up @@ -611,6 +637,12 @@ typeToAnnotationMaybe qualify type_ =
Common.String ->
CliMonad.succeed Elm.Annotation.string

Common.DateTime ->
CliMonad.succeed Gen.Time.annotation_.posix

Common.Date ->
CliMonad.succeed Gen.Date.annotation_.date

Common.Int ->
CliMonad.succeed Elm.Annotation.int

Expand Down Expand Up @@ -706,6 +738,32 @@ typeToEncoder qualify type_ =
Common.String ->
CliMonad.succeed Gen.Json.Encode.call_.string

Common.DateTime ->
CliMonad.succeed
(\instant ->
Gen.Rfc3339.make_.dateTimeOffset
(Elm.record
[ ( "instant", instant )
, ( "offset"
, Elm.record
[ ( "hour", Elm.int 0 )
, ( "minute", Elm.int 0 )
]
)
]
)
|> Gen.Rfc3339.toString
|> Gen.Json.Encode.call_.string
)

Common.Date ->
CliMonad.succeed
(\date ->
date
|> Gen.Date.toIsoString
|> Gen.Json.Encode.call_.string
)

Common.Int ->
CliMonad.succeed Gen.Json.Encode.call_.int

Expand Down Expand Up @@ -934,6 +992,34 @@ typeToDecoder qualify type_ =
Common.String ->
CliMonad.succeed Gen.Json.Decode.string

Common.DateTime ->
CliMonad.succeed
(Gen.Json.Decode.string
|> Gen.Json.Decode.andThen
(\raw ->
Gen.Result.caseOf_.result
(Gen.Parser.Advanced.call_.run Gen.Rfc3339.dateTimeOffsetParser raw)
{ ok = \record -> Gen.Json.Decode.succeed (Elm.get "instant" record)

-- TODO: improve error message
, err = \_ -> Gen.Json.Decode.fail "Invalid RFC-3339 date-time"
}
)
)

Common.Date ->
CliMonad.succeed
(Gen.Json.Decode.string
|> Gen.Json.Decode.andThen
(\raw ->
Gen.Result.caseOf_.result
(Gen.Date.call_.fromIsoString raw)
{ ok = Gen.Json.Decode.succeed
, err = Gen.Json.Decode.call_.fail
}
)
)

Common.Int ->
CliMonad.succeed Gen.Json.Decode.int

Expand Down

0 comments on commit 748262a

Please sign in to comment.