Skip to content
Open
Show file tree
Hide file tree
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
8 changes: 8 additions & 0 deletions src/Elm/Decoder.hs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ instance HasDecoderRef ElmPrimitive where
renderRef (EList datatype) = do
dt <- renderRef datatype
return . parens $ "list" <+> dt
renderRef (EMap EString value) = do
require "Dict"
d <- renderRef value
return . parens $ "dict" <+> d
renderRef (EMap EInt value) = do
require "Dict"
d <- renderRef value
return . parens $ "dict" <+> d <+> " |> map (Dict.toList >> List.filterMap (\\( k, v ) -> String.toInt k |> Result.toMaybe |> Maybe.map (\\i -> ( i, v ))) >> Dict.fromList)"
renderRef (EDict EString value) = do
require "Dict"
d <- renderRef value
Expand Down
4 changes: 4 additions & 0 deletions src/Elm/Encoder.hs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ instance HasEncoderRef ElmPrimitive where
dy <- renderRef y
require "Exts.Json.Encode"
return . parens $ "Exts.Json.Encode.tuple2" <+> dx <+> dy
renderRef (EMap _ v) = do
dv <- renderRef v
require "Exts.Json.Encode"
return . parens $ "Dict.toList >> (\\ (k,v) -> (toString k, (" <+> dv <+> ") v)) >> Json.encode.object"
renderRef (EDict k v) = do
dk <- renderRef k
dv <- renderRef v
Expand Down
5 changes: 5 additions & 0 deletions src/Elm/Record.hs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ instance HasTypeRef ElmPrimitive where
renderRef (EMaybe datatype) = do
dt <- renderRef datatype
return $ "Maybe" <+> parens dt
renderRef (EMap k v) = do
require "Dict"
dk <- renderRef k
dv <- renderRef v
return $ "Dict" <+> parens dk <+> parens dv
renderRef (EDict k v) = do
require "Dict"
dk <- renderRef k
Expand Down
13 changes: 11 additions & 2 deletions src/Elm/Type.hs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ data ElmPrimitive
ElmDatatype
| EDict ElmPrimitive
ElmDatatype
| EMap ElmPrimitive
ElmDatatype
deriving (Show, Eq)

data ElmConstructor
Expand Down Expand Up @@ -172,11 +174,15 @@ instance (ElmType a) =>
ElmType (Proxy a) where
toElmType _ = toElmType (undefined :: a)

instance (HasElmComparable k, ElmType v) =>
instance {-# OVERLAPS #-} (ElmType v) =>
ElmType (Map String v) where
toElmType _ = ElmPrimitive $ EDict EString (toElmType (Proxy :: Proxy v))

instance {-# OVERLAPS #-} (HasElmComparable k, ElmType v) =>
ElmType (Map k v) where
toElmType _ =
ElmPrimitive $
EDict (toElmComparable (undefined :: k)) (toElmType (Proxy :: Proxy v))
EMap (toElmComparable (undefined :: k)) (toElmType (Proxy :: Proxy v))

instance (ElmType v) =>
ElmType (IntMap v) where
Expand All @@ -188,6 +194,9 @@ class HasElmComparable a where
instance HasElmComparable String where
toElmComparable _ = EString

instance HasElmComparable Int where
toElmComparable _ = EInt

instance HasElmComparable Text where
toElmComparable _ = EString

Expand Down
3 changes: 3 additions & 0 deletions test/ExportSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,9 @@ toElmDecoderSpec =
it "toElmDecoderRef (IntMap (Maybe String))" $
toElmDecoderRef (Proxy :: Proxy (IntMap (Maybe String))) `shouldBe`
"(map Dict.fromList (list (map2 (,) (index 0 int) (index 1 (nullable string)))))"
it "toElmDecoderRef (Map Int (Maybe String))" $
toElmDecoderRef (Proxy :: Proxy (Map Int (Maybe String))) `shouldBe`
"(dict (nullable string) |> map (Dict.toList >> List.filterMap (\\( k, v ) -> String.toInt k |> Result.toMaybe |> Maybe.map (\\i -> ( i, v ))) >> Dict.fromList))"

toElmEncoderSpec :: Hspec.Spec
toElmEncoderSpec =
Expand Down