elm-export 0.4.0.0 → 0.4.0.1
raw patch · 5 files changed
+172/−69 lines, 5 filesdep ~elm-exportPVP ok
version bump matches the API change (PVP)
Dependency ranges changed: elm-export
API changes (from Hackage documentation)
Files
- elm-export.cabal +2/−2
- src/Elm/Decoder.hs +13/−13
- src/Elm/Encoder.hs +12/−12
- src/Elm/File.hs +1/−1
- test/ExportSpec.hs +144/−41
elm-export.cabal view
@@ -1,5 +1,5 @@ name: elm-export-version: 0.4.0.0+version: 0.4.0.1 cabal-version: >=1.10 build-type: Simple license: OtherLicense@@ -47,7 +47,7 @@ base >=4.8.2.0 && <4.9, bytestring >=0.10.6.0 && <0.11, containers >=0.5.6.2 && <0.6,- elm-export >=0.4.0.0 && <0.5,+ elm-export >=0.4.0.1 && <0.5, hspec >=2.2.2 && <2.3, hspec-core >=2.2.2 && <2.3, quickcheck-instances >=0.3.12 && <0.4,
src/Elm/Decoder.hs view
@@ -14,18 +14,18 @@ render :: ElmTypeExpr -> Reader Options String render (TopLevel (DataType d t)) =- printf "%s : Json.Decode.Decoder %s\n%s =\n%s" fnName d fnName <$> render t+ printf "%s : Decoder %s\n%s =\n%s" fnName d fnName <$> render t where fnName = "decode" ++ d render (DataType d _) = return $ "decode" ++ d -render (Record n t) = printf " Json.Decode.succeed %s\n%s" n <$> render t+render (Record n t) = printf " decode %s\n%s" n <$> render t render (Product (Primitive "List") (Primitive "Char")) = render (Primitive "String") -render (Product (Primitive "List") t) = printf "Json.Decode.list %s" <$> render t+render (Product (Primitive "List") t) = printf "(list %s)" <$> render t -render (Product (Primitive "Maybe") t) = printf "Json.Decode.maybe %s" <$> render t+render (Product (Primitive "Maybe") t) = printf "(maybe %s)" <$> render t render (Product x y) = do bodyX <- render x@@ -34,27 +34,27 @@ render (Selector n t) = do fieldModifier <- asks fieldLabelModifier- printf " |: (\"%s\" := %s)" (fieldModifier n) <$> render t+ printf " |> required \"%s\" %s" (fieldModifier n) <$> render t render (Tuple2 x y) = do bodyX <- render x bodyY <- render y- return $ printf "Json.Decode.tuple2 (,) %s %s" bodyX bodyY+ return $ printf "(tuple2 (,) %s %s)" bodyX bodyY render (Dict x y) =- printf "Json.Decode.map Dict.fromList (Json.Decode.list (%s))" <$> render (Tuple2 x y)+ printf "(map Dict.fromList (list %s))" <$> render (Tuple2 x y) -render (Primitive "String") = return "Json.Decode.string"+render (Primitive "String") = return "string" -render (Primitive "Int") = return "Json.Decode.int"+render (Primitive "Int") = return "int" -render (Primitive "Double") = return "Json.Decode.float"+render (Primitive "Double") = return "float" -render (Primitive "Float") = return "Json.Decode.float"+render (Primitive "Float") = return "float" -render (Primitive "Date") = return "Json.Decode.Extra.date"+render (Primitive "Date") = return "(customDecoder string Date.fromString)" -render (Primitive "Bool") = return "Json.Decode.bool"+render (Primitive "Bool") = return "bool" render (Field t) = render t
src/Elm/Encoder.hs view
@@ -9,32 +9,32 @@ render :: ElmTypeExpr -> Reader Options String render (TopLevel (DataType d t)) =- printf "%s : %s -> Json.Encode.Value\n%s x =%s" fnName d fnName <$> render t+ printf "%s : %s -> Value\n%s x =%s" fnName d fnName <$> render t where fnName = "encode" ++ d render (DataType d _) = return $ "encode" ++ d render (Record _ t) =- printf "\n Json.Encode.object\n [ %s\n ]" <$> render t+ printf "\n object\n [ %s\n ]" <$> render t render (Product (Primitive "List") (Primitive "Char")) = render (Primitive "String") render (Product (Primitive "List") t) =- printf "(Json.Encode.list << List.map %s)" <$> render t+ printf "(list << List.map %s)" <$> render t render (Product (Primitive "Maybe") t) =- printf "(Maybe.withDefault Json.Encode.null << Maybe.map %s)" <$> render t+ printf "(Maybe.withDefault null << Maybe.map %s)" <$> render t render (Tuple2 x y) = do bodyX <- render x bodyY <- render y- return $ printf "Exts.Json.Encode.tuple2 %s %s" bodyX bodyY+ return $ printf "tuple2 %s %s" bodyX bodyY render (Dict x y) = do bodyX <- render x bodyY <- render y- return $ printf "Exts.Json.Encode.dict %s %s" bodyX bodyY+ return $ printf "dict %s %s" bodyX bodyY render (Product x y) = do bodyX <- render x@@ -46,12 +46,12 @@ typeBody <- render t return $ printf "( \"%s\", %s x.%s )" (fieldModifier n) typeBody n -render (Primitive "String") = return "Json.Encode.string"-render (Primitive "Int") = return "Json.Encode.int"-render (Primitive "Double") = return "Json.Encode.float"-render (Primitive "Float") = return "Json.Encode.float"-render (Primitive "Date") = return "(Json.Encode.string << Exts.Date.toISOString)"-render (Primitive "Bool") = return "Json.Encode.bool"+render (Primitive "String") = return "string"+render (Primitive "Int") = return "int"+render (Primitive "Double") = return "float"+render (Primitive "Float") = return "float"+render (Primitive "Date") = return "(string << toISOString)"+render (Primitive "Bool") = return "bool" render (Field t) = render t toElmEncoderSourceWith :: ElmType a => Options -> a -> String
src/Elm/File.hs view
@@ -29,7 +29,7 @@ body = intercalate "\n\n"- (printf "module %s where" namespaceString : declarations spec)+ (printf "module %s exposing (..)" namespaceString : declarations spec) in do printf "Writing: %s\n" file writeFile file body
test/ExportSpec.hs view
@@ -7,7 +7,7 @@ import Data.Char import Data.Map import Data.Proxy-import Data.Text+import Data.Text hiding (unlines) import Data.Time import Elm import GHC.Generics@@ -49,24 +49,54 @@ toElmTypeSpec = describe "Convert to Elm types." $ do it "toElmTypeSource Post" $- shouldMatchTypeSource defaultOptions- (Proxy :: Proxy Post)- "test/PostType.elm"+ shouldMatchTypeSource+ (unlines ["module PostType exposing (..)"+ ,""+ ,"import CommentType exposing (..)"+ ,""+ ,""+ ,"%s"])+ defaultOptions+ (Proxy :: Proxy Post)+ "test/PostType.elm" it "toElmTypeSource Comment" $- shouldMatchTypeSource defaultOptions- (Proxy :: Proxy Comment)- "test/CommentType.elm"+ shouldMatchTypeSource+ (unlines ["module CommentType exposing (..)"+ ,""+ ,"import Date exposing (Date)"+ ,"import Dict exposing (Dict)"+ ,""+ ,""+ ,"%s"])+ defaultOptions+ (Proxy :: Proxy Comment)+ "test/CommentType.elm" it "toElmTypeSource Position" $- shouldMatchTypeSource defaultOptions- (Proxy :: Proxy Position)- "test/PositionType.elm"+ shouldMatchTypeSource+ (unlines ["module PositionType exposing (..)","","","%s"])+ defaultOptions+ (Proxy :: Proxy Position)+ "test/PositionType.elm" it "toElmTypeSourceWithOptions Post" $ shouldMatchTypeSource+ (unlines ["module PostTypeWithOptions exposing (..)"+ ,""+ ,"import CommentType exposing (..)"+ ,""+ ,""+ ,"%s"]) (defaultOptions {fieldLabelModifier = withPrefix "post"}) (Proxy :: Proxy Post) "test/PostTypeWithOptions.elm" it "toElmTypeSourceWithOptions Comment" $ shouldMatchTypeSource+ (unlines ["module CommentTypeWithOptions exposing (..)"+ ,""+ ,"import Date exposing (Date)"+ ,"import Dict exposing (Dict)"+ ,""+ ,""+ ,"%s"]) (defaultOptions {fieldLabelModifier = withPrefix "comment"}) (Proxy :: Proxy Comment) "test/CommentTypeWithOptions.elm"@@ -74,21 +104,61 @@ toElmDecoderSpec :: Hspec.Spec toElmDecoderSpec = describe "Convert to Elm decoders." $- do it "toElmDecoderSource Post" $- shouldMatchDecoderSource defaultOptions- (Proxy :: Proxy Post)- "test/PostDecoder.elm"- it "toElmDecoderSource Comment" $- shouldMatchDecoderSource defaultOptions- (Proxy :: Proxy Comment)- "test/CommentDecoder.elm"+ do it "toElmDecoderSource Comment" $+ shouldMatchDecoderSource+ (unlines ["module CommentDecoder exposing (..)"+ ,""+ ,"import CommentType exposing (..)"+ ,"import Date"+ ,"import Dict"+ ,"import Json.Decode exposing (..)"+ ,"import Json.Decode.Pipeline exposing (..)"+ ,""+ ,""+ ,"%s"])+ defaultOptions+ (Proxy :: Proxy Comment)+ "test/CommentDecoder.elm"+ it "toElmDecoderSource Post" $+ shouldMatchDecoderSource+ (unlines ["module PostDecoder exposing (..)"+ ,""+ ,"import CommentDecoder exposing (..)"+ ,"import Json.Decode exposing (..)"+ ,"import Json.Decode.Pipeline exposing (..)"+ ,"import PostType exposing (..)"+ ,""+ ,""+ ,"%s"])+ defaultOptions+ (Proxy :: Proxy Post)+ "test/PostDecoder.elm" it "toElmDecoderSourceWithOptions Post" $ shouldMatchDecoderSource+ (unlines ["module PostDecoderWithOptions exposing (..)"+ ,""+ ,"import CommentDecoder exposing (..)"+ ,"import Json.Decode exposing (..)"+ ,"import Json.Decode.Pipeline exposing (..)"+ ,"import PostType exposing (..)"+ ,""+ ,""+ ,"%s"]) (defaultOptions {fieldLabelModifier = withPrefix "post"}) (Proxy :: Proxy Post) "test/PostDecoderWithOptions.elm" it "toElmDecoderSourceWithOptions Comment" $ shouldMatchDecoderSource+ (unlines ["module CommentDecoderWithOptions exposing (..)"+ ,""+ ,"import CommentType exposing (..)"+ ,"import Date"+ ,"import Dict"+ ,"import Json.Decode exposing (..)"+ ,"import Json.Decode.Pipeline exposing (..)"+ ,""+ ,""+ ,"%s"]) (defaultOptions {fieldLabelModifier = withPrefix "comment"}) (Proxy :: Proxy Comment) "test/CommentDecoderWithOptions.elm"@@ -96,45 +166,78 @@ toElmEncoderSpec :: Hspec.Spec toElmEncoderSpec = describe "Convert to Elm encoders." $- do it "toElmEncoderSource Post" $- shouldMatchEncoderSource defaultOptions- (Proxy :: Proxy Post)- "test/PostEncoder.elm"- it "toElmEncoderSource Comment" $- shouldMatchEncoderSource defaultOptions- (Proxy :: Proxy Comment)- "test/CommentEncoder.elm"- it "toElmEncoderSourceWithOptions Post" $+ do it "toElmEncoderSource Comment" $ shouldMatchEncoderSource- (defaultOptions {fieldLabelModifier = withPrefix "post"})+ (unlines ["module CommentEncoder exposing (..)"+ ,""+ ,"import CommentType exposing (..)"+ ,"import Exts.Date exposing (..)"+ ,"import Exts.Json.Encode exposing (..)"+ ,"import Json.Encode exposing (..)"+ ,""+ ,""+ ,"%s"])+ defaultOptions+ (Proxy :: Proxy Comment)+ "test/CommentEncoder.elm"+ it "toElmEncoderSource Post" $+ shouldMatchEncoderSource+ (unlines ["module PostEncoder exposing (..)"+ ,""+ ,"import CommentEncoder exposing (..)"+ ,"import Json.Encode exposing (..)"+ ,"import PostType exposing (..)"+ ,""+ ,""+ ,"%s"])+ defaultOptions (Proxy :: Proxy Post)- "test/PostEncoderWithOptions.elm"+ "test/PostEncoder.elm" it "toElmEncoderSourceWithOptions Comment" $ shouldMatchEncoderSource+ (unlines ["module CommentEncoderWithOptions exposing (..)"+ ,""+ ,"import CommentType exposing (..)"+ ,"import Exts.Date exposing (..)"+ ,"import Exts.Json.Encode exposing (..)"+ ,"import Json.Encode exposing (..)"+ ,""+ ,""+ ,"%s"]) (defaultOptions {fieldLabelModifier = withPrefix "comment"}) (Proxy :: Proxy Comment) "test/CommentEncoderWithOptions.elm"+ it "toElmEncoderSourceWithOptions Post" $+ shouldMatchEncoderSource+ (unlines ["module PostEncoderWithOptions exposing (..)"+ ,""+ ,"import CommentEncoder exposing (..)"+ ,"import Json.Encode exposing (..)"+ ,"import PostType exposing (..)"+ ,""+ ,""+ ,"%s"])+ (defaultOptions {fieldLabelModifier = withPrefix "post"})+ (Proxy :: Proxy Post)+ "test/PostEncoderWithOptions.elm" shouldMatchTypeSource :: ElmType a- => Options -> a -> FilePath -> IO ()-shouldMatchTypeSource options x =- shouldMatchFile . printf outputWrapping $ toElmTypeSourceWith options x+ => String -> Options -> a -> FilePath -> IO ()+shouldMatchTypeSource wrapping options x =+ shouldMatchFile . printf wrapping $ toElmTypeSourceWith options x shouldMatchDecoderSource :: ElmType a- => Options -> a -> FilePath -> IO ()-shouldMatchDecoderSource options x =- shouldMatchFile . printf outputWrapping $ toElmDecoderSourceWith options x+ => String -> Options -> a -> FilePath -> IO ()+shouldMatchDecoderSource wrapping options x =+ shouldMatchFile . printf wrapping $ toElmDecoderSourceWith options x shouldMatchEncoderSource :: ElmType a- => Options -> a -> FilePath -> IO ()-shouldMatchEncoderSource options x =- shouldMatchFile . printf outputWrapping $ toElmEncoderSourceWith options x--outputWrapping :: String-outputWrapping = "module Main exposing (..)\n\n\n%s\n"+ => String -> Options -> a -> FilePath -> IO ()+shouldMatchEncoderSource wrapping options x =+ shouldMatchFile . printf wrapping $ toElmEncoderSourceWith options x shouldMatchFile :: String -> FilePath -> IO () shouldMatchFile actual fileExpected =