Skip to content

Commit

Permalink
add rudamentary support for date, datetime and time
Browse files Browse the repository at this point in the history
  • Loading branch information
agrafix committed Oct 30, 2015
1 parent 1f137c0 commit 07c1ba4
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 4 deletions.
16 changes: 14 additions & 2 deletions src/TW/BuiltIn.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
module TW.BuiltIn
( BuiltIn(..)
, allBuiltIns, isBuiltIn
, tyString, tyInt, tyFloat, tyBool, tyMaybe, tyBytes, tyList
, tyString, tyInt, tyFloat, tyBool, tyMaybe, tyBytes, tyList, tyDateTime, tyDate, tyTime
)
where

Expand All @@ -18,7 +18,7 @@ data BuiltIn
} deriving (Show, Eq)

allBuiltIns :: [BuiltIn]
allBuiltIns = [tyString, tyInt, tyFloat, tyBool, tyMaybe, tyBytes, tyList]
allBuiltIns = [tyString, tyInt, tyFloat, tyBool, tyMaybe, tyBytes, tyList, tyDateTime, tyDate, tyTime]

isBuiltIn :: Type -> Maybe (BuiltIn, [Type])
isBuiltIn ty =
Expand Down Expand Up @@ -56,3 +56,15 @@ tyList = builtInVars "List" ["a"]

tyBytes :: BuiltIn
tyBytes = builtIn "Bytes"

-- | DateTime type, format: YYYY-MM-DD HH:MM:SS[Z|+XX|-XX] or YYYY-MM-DDTHH:MM:SS[Z|+XX|-XX]
tyDateTime :: BuiltIn
tyDateTime = builtIn "DateTime"

-- | Date type, format: YYYY-MM-DD
tyDate :: BuiltIn
tyDate = builtIn "Date"

-- | Time type, format: HH:MM:SS
tyTime :: BuiltIn
tyTime = builtIn "Time"
9 changes: 9 additions & 0 deletions src/TW/CodeGen/Elm.hs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,9 @@ jsonEncFor t =
| bi == tyBool -> jsonEnc "bool"
| bi == tyFloat -> jsonEnc "float"
| bi == tyBytes -> "ELib.jencAsBase64"
| bi == tyDateTime -> "ELib.jencDateTime"
| bi == tyTime -> "ELib.jencTime"
| bi == tyDate -> "ELib.jencDate"
| bi == tyList ->
case tvars of
[arg] -> jsonEnc "list" <> " (" <> jsonEncFor arg <> ")"
Expand Down Expand Up @@ -216,6 +219,9 @@ jsonDecFor t =
| bi == tyBool -> jsonDec "bool"
| bi == tyFloat -> jsonDec "float"
| bi == tyBytes -> "ELib.jdecAsBase64"
| bi == tyDateTime -> "ELib.jdecDateTime"
| bi == tyTime -> "ELib.jdecTime"
| bi == tyDate -> "ELib.jdecDate"
| bi == tyList ->
case tvars of
[arg] -> jsonDec "list" <> " (" <> jsonDecFor arg <> ")"
Expand Down Expand Up @@ -249,6 +255,9 @@ makeType t =
| bi == tyInt -> "Int"
| bi == tyBool -> "Bool"
| bi == tyFloat -> "Float"
| bi == tyDateTime -> "ELib.DateTime"
| bi == tyTime -> "ELib.Time"
| bi == tyDate -> "ELib.Date"
| bi == tyMaybe -> "(Maybe " <> T.intercalate " " (map makeType tvars) <> ")"
| bi == tyList -> "(List " <> T.intercalate " " (map makeType tvars) <> ")"
| bi == tyBytes -> "ELib.AsBase64"
Expand Down
6 changes: 5 additions & 1 deletion src/TW/CodeGen/Haskell.hs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ makeModule m =
, "import qualified TW.Support.Lib as HLib"
, "import Control.Applicative"
, "import Control.Monad (join)"
, "import Data.Time"
, "import qualified Data.Aeson as " <> aesonQual
, "import qualified Data.Text as T"
, "import qualified Data.Vector as V"
Expand Down Expand Up @@ -179,8 +180,11 @@ makeType t =
| bi == tyMaybe -> "(Maybe " <> T.intercalate " " (map makeType tvars) <> ")"
| bi == tyBytes -> "HLib.AsBase64"
| bi == tyList -> "(V.Vector " <> T.intercalate " " (map makeType tvars) <> ")"
| bi == tyDateTime -> "UTCTime"
| bi == tyTime -> "TimeOfDay"
| bi == tyDate -> "Day"
| otherwise ->
error $ "Elm: Unimplemented built in type: " ++ show t
error $ "Haskell: Unimplemented built in type: " ++ show t

makeQualTypeName :: QualTypeName -> T.Text
makeQualTypeName qtn =
Expand Down
23 changes: 22 additions & 1 deletion support/elm/Lib.elm
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,34 @@ import Json.Decode as JD
import Json.Encode as JE

type AsBase64 = AsBase64 String

jdecAsBase64 : JD.Decoder AsBase64
jdecAsBase64 = JD.map AsBase64 JD.string

jencAsBase64 : AsBase64 -> JE.Value
jencAsBase64 (AsBase64 str) = JE.string str

type Date = Date String
jdecDate : JD.Decoder Date
jdecDate = JD.map Date JD.string

jencDate : Date -> JE.Value
jencDate (Date str) = JE.string str

type Time = Time String
jdecTime : JD.Decoder Time
jdecTime = JD.map AsBase64 JD.string

jencTime : Time -> JE.Value
jencTime (Time str) = JE.string str

type DateTime = DateTime String
jdecDateTime : JD.Decoder DateTime
jdecDateTime = JD.map DateTime JD.string

jencDateTime : DateTime -> JE.Value
jencDateTime (DateTime str) = JE.string str


encMaybe : (a -> JE.Value) -> Maybe a -> JE.Value
encMaybe f v =
case v of
Expand Down
17 changes: 17 additions & 0 deletions support/haskell/Lib.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{-# OPTIONS_GHC -fno-warn-orphans #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE OverloadedStrings #-}
module TW.Support.Lib
Expand All @@ -8,6 +9,7 @@ where

import Data.Aeson
import Data.Aeson.Types
import data.Time
import qualified Data.ByteString as BS
import qualified Data.ByteString.Base64 as B64
import qualified Data.Text.Encoding as T
Expand All @@ -33,3 +35,18 @@ instance FromJSON AsBase64 where
-- | Enforce bool and ignore it
eatBool :: Bool -> Parser ()
eatBool _ = return ()

-- Extremely hacky until next aeson version includes TimeOfDay
instance ToJSON TimeOfDay where
toJSON tod =
case toJSON (LocalTime (ModifiedJulianDay 0) tod) of
Text x ->
Text (T.drop 11 x)
_ -> error "Library error!"

-- Extremely hacky until next aeson version includes TimeOfDay
instance FromJSON TimeOfDay where
parseJSON =
withText "TimeOfDay" $ \t ->
do localTime <- parseJSON (Text $ "1994-02-04 " <> t)
return $ localTimeOfDay localTime

0 comments on commit 07c1ba4

Please sign in to comment.