servant-elm 0.6.1 → 0.7.0
raw patch · 10 files changed
+79/−62 lines, 10 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
+ Servant.Elm: [elmToString] :: ElmOptions -> EType -> Text
+ Servant.Elm: defaultElmToString :: EType -> Text
+ Servant.Elm.Internal.Generate: [elmToString] :: ElmOptions -> EType -> Text
+ Servant.Elm.Internal.Generate: composeLeft :: [Doc] -> Doc
+ Servant.Elm.Internal.Generate: composeRight :: [Doc] -> Doc
+ Servant.Elm.Internal.Generate: defaultElmToString :: EType -> Text
+ Servant.Elm.Internal.Generate: listOf :: EType -> EType
+ Servant.Elm.Internal.Generate: maybeOf :: EType -> EType
+ Servant.Elm.Internal.Generate: pipeLeft :: [Doc] -> Doc
+ Servant.Elm.Internal.Generate: pipeRight :: [Doc] -> Doc
+ Servant.Elm.Internal.Generate: toString :: ElmOptions -> EType -> Doc
- Servant.Elm: ElmOptions :: UrlPrefix -> (EType -> EType) -> (ETypeDef -> ETypeDef) -> [EType] -> [EType] -> ElmOptions
+ Servant.Elm: ElmOptions :: UrlPrefix -> (EType -> EType) -> (ETypeDef -> ETypeDef) -> (EType -> Text) -> [EType] -> [EType] -> ElmOptions
- Servant.Elm.Internal.Generate: ElmOptions :: UrlPrefix -> (EType -> EType) -> (ETypeDef -> ETypeDef) -> [EType] -> [EType] -> ElmOptions
+ Servant.Elm.Internal.Generate: ElmOptions :: UrlPrefix -> (EType -> EType) -> (ETypeDef -> ETypeDef) -> (EType -> Text) -> [EType] -> [EType] -> ElmOptions
Files
- CHANGELOG.md +5/−0
- README.md +0/−7
- servant-elm.cabal +1/−1
- src/Servant/Elm.hs +2/−0
- src/Servant/Elm/Internal/Generate.hs +60/−35
- test/GenerateSpec.hs +0/−6
- test/elm-sources/getBooksByIdSource.elm +1/−1
- test/elm-sources/getBooksByTitleSource.elm +1/−1
- test/elm-sources/getBooksSource.elm +5/−9
- test/elm-sources/getWithaheaderSource.elm +4/−2
CHANGELOG.md view
@@ -1,3 +1,8 @@+0.7.0+-----++* PR #59. Using the appropriate toString functions for different types+ 0.6.1 -----
README.md view
@@ -79,13 +79,6 @@ import String import Url.Builder -maybeBoolToIntStr : Maybe Bool -> String-maybeBoolToIntStr mx =- case mx of- Nothing -> ""- Just True -> "1"- Just False -> "0"- type alias Book = { name: String }
servant-elm.cabal view
@@ -1,5 +1,5 @@ name: servant-elm-version: 0.6.1+version: 0.7.0 synopsis: Automatically derive Elm functions to query servant webservices. description: Please see README.md homepage: http://github.com/mattjbray/servant-elm#readme
src/Servant/Elm.hs view
@@ -21,6 +21,7 @@ , defElmOptions , defElmImports , defaultOptions+ , defaultElmToString -- * Convenience re-exports from the "Elm" module , DefineElm (..) , EType (..)@@ -34,6 +35,7 @@ import Servant.Elm.Internal.Generate (ElmOptions (..), UrlPrefix (..), defElmImports, defElmOptions,+ defaultElmToString, generateElmForAPI, generateElmForAPIWith, generateElmModule,
src/Servant/Elm/Internal/Generate.hs view
@@ -53,6 +53,8 @@ -- ^ Alterations to perform on ETypes before code generation. , elmAlterations :: (ETypeDef -> ETypeDef) -- ^ Alterations to perform on ETypeDefs before code generation.+ , elmToString :: (EType -> Text)+ -- ^ Elm functions creating a string from a given type. , emptyResponseElmTypes :: [EType] -- ^ Types that represent an empty Http response. , stringElmTypes :: [EType]@@ -87,6 +89,7 @@ { urlPrefix = Static "" , elmTypeAlterations = Elm.defaultTypeAlterations , elmAlterations = Elm.defaultAlterations+ , elmToString = defaultElmToString , emptyResponseElmTypes = [ toElmType (Proxy :: Proxy ()) ]@@ -126,13 +129,6 @@ , "import Http" , "import String" , "import Url.Builder"- , ""- , "maybeBoolToIntStr : Maybe Bool -> String"- , "maybeBoolToIntStr mx ="- , " case mx of"- , " Nothing -> \"\""- , " Just True -> \"1\""- , " Just False -> \"0\"" ] {-|@@ -392,15 +388,11 @@ let argType = qarg ^. F.queryArgName . F.argType wrapped = isElmMaybeType argType- -- Don't use "toString" on Elm Strings, otherwise we get extraneous quotes. toStringSrc =- if isElmStringType opts argType || isElmMaybeStringType opts argType then- ""- else- "String.fromInt >> "+ toString opts (maybeOf argType) in "[" <+> (if wrapped then elmName else "Just" <+> elmName) <> line <>- (indent 4 ("|> Maybe.map" <+> parens (toStringSrc <> "Url.Builder.string" <+> dquotes name)))+ (indent 4 ("|> Maybe.map" <+> composeRight [toStringSrc, "Url.Builder.string" <+> dquotes name])) <+> "]" -- (if wrapped then name else "Just" <+> name) <$> -- indent 4 ("|> Maybe.map" <+> parens (toStringSrc <> "Http.encodeUri >> (++)" <+> dquotes (elmName <> equals)) <$>@@ -417,18 +409,18 @@ F.List -> let argType = qarg ^. F.queryArgName . F.argType- convertedVal =- if isElmListOfMaybeBoolType argType then- parens ("maybeBoolToIntStr" <+> "val")- else- "val"+ toStringSrc =+ toString opts (listOf (maybeOf argType)) in elmName <$> indent 4 ("|> List.map"- <+> parens (backslash <> "val ->" <+> "Just"- <+> parens ("Url.Builder.string"- <+> dquotes (name <> "[]")- <+> convertedVal)))+ <+> composeRight+ [ toStringSrc+ , "Url.Builder.string" <+> dquotes (name <> "[]")+ , "Just"+ ]+ )+ where elmName = elmQueryArg qarg name = qarg ^. F.queryArgName . F.argName . to (stext . F.unPathSegment)@@ -464,13 +456,9 @@ headerArgName = elmHeaderArg header argType = header ^. F.headerArg . F.argType wrapped = isElmMaybeType argType- toStringSrc =- if isElmMaybeStringType opts argType || isElmStringType opts argType then- mempty- else- " << String.fromInt"+ toStringSrc = toString opts (maybeOf argType) in- "Maybe.map" <+> parens (("Http.header" <+> dquotes headerName <> toStringSrc))+ "Maybe.map" <+> composeLeft ["Http.header" <+> dquotes headerName, toStringSrc] <+> (if wrapped then headerArgName else parens ("Just" <+> headerArgName)) @@ -561,14 +549,10 @@ dquotes (stext (F.unPathSegment path)) F.Cap arg -> let- -- Don't use "toString" on Elm Strings, otherwise we get extraneous quotes.- toStringSrc =- if isElmStringType opts (arg ^. F.argType) then- empty- else- " |> String.fromInt"+ toStringSrc = + toString opts (maybeOf (arg ^. F.argType)) in- (elmCaptureArg s) <> toStringSrc+ pipeRight [elmCaptureArg s, toStringSrc] mkQueryParams@@ -632,3 +616,44 @@ elmListOfMaybes :: [Doc] -> Doc elmListOfMaybes [] = lbracket <> rbracket elmListOfMaybes ds = "List.filterMap identity" <$> indent 4 (elmList ds)++defaultElmToString :: EType -> Text+defaultElmToString argType =+ case argType of+ ETyCon (ETCon "Bool") -> "(\\value -> if value then \"true\" else \"false\")"+ ETyCon (ETCon "Float") -> "String.fromFloat"+ ETyCon (ETCon "Char") -> "String.fromChar"+ ETyApp (ETyCon (ETCon "Maybe")) v -> "(Maybe.map " <> defaultElmToString v <> " >> Maybe.withDefault \"\")"+ _ -> "String.fromInt"+++maybeOf :: EType -> EType+maybeOf (ETyApp (ETyCon (ETCon "Maybe")) v) = v+maybeOf v = v++listOf :: EType -> EType+listOf (ETyApp (ETyCon (ETCon "List")) v) = v+listOf v = v++toString :: ElmOptions -> EType -> Doc+toString opts argType =+ if isElmStringType opts argType then+ mempty+ else+ stext $ elmToString opts argType++pipeLeft :: [Doc] -> Doc+pipeLeft =+ encloseSep lparen rparen " <| " . filter (not . isEmpty)++pipeRight :: [Doc] -> Doc+pipeRight =+ encloseSep lparen rparen " |> " . filter (not . isEmpty)++composeLeft :: [Doc] -> Doc+composeLeft =+ encloseSep lparen rparen " << " . filter (not . isEmpty)++composeRight :: [Doc] -> Doc+composeRight =+ encloseSep lparen rparen " >> " . filter (not . isEmpty)
test/GenerateSpec.hs view
@@ -70,12 +70,6 @@ "import Url.Builder\n" <> "import Json.Decode as J\n\n" <> "type alias Book = {}\n\n" <>- "maybeBoolToIntStr : Maybe Bool -> String\n" <>- "maybeBoolToIntStr mx =\n" <>- " case mx of\n" <>- " Nothing -> \"\"\n" <>- " Just True -> \"1\"\n" <>- " Just False -> \"0\"\n\n" <> "jsonDecBook = J.succeed {}\n\n" ) , ( "test/elm-sources/postBooksSource.elm"
test/elm-sources/getBooksByIdSource.elm view
@@ -24,7 +24,7 @@ , url = Url.Builder.crossOrigin "" [ "books"- , capture_id |> String.fromInt+ , (capture_id |> String.fromInt) ] params , body =
test/elm-sources/getBooksByTitleSource.elm view
@@ -23,7 +23,7 @@ , url = Url.Builder.crossOrigin "" [ "books"- , capture_title+ , (capture_title) ] params , body =
test/elm-sources/getBooksSource.elm view
@@ -7,13 +7,6 @@ type alias Book = {} -maybeBoolToIntStr : Maybe Bool -> String-maybeBoolToIntStr mx =- case mx of- Nothing -> ""- Just True -> "1"- Just False -> "0"- jsonDecBook = J.succeed {} getBooks : Bool -> (Maybe String) -> (Maybe Int) -> String -> (List (Maybe Bool)) -> (Result Http.Error ((List Book)) -> msg) -> Cmd msg@@ -29,11 +22,14 @@ , [ query_sort |> Maybe.map (Url.Builder.string "sort") ] , [ query_year- |> Maybe.map (String.fromInt >> Url.Builder.string "year") ]+ |> Maybe.map (String.fromInt+ >> Url.Builder.string "year") ] , [ Just query_category |> Maybe.map (Url.Builder.string "category") ] , query_filters- |> List.map (\val -> Just (Url.Builder.string "filters[]" (maybeBoolToIntStr val)))+ |> List.map ((Maybe.map (\value -> if value then "true" else "false") >> Maybe.withDefault "")+ >> Url.Builder.string "filters[]"+ >> Just) ]) in Http.request
test/elm-sources/getWithaheaderSource.elm view
@@ -19,9 +19,11 @@ , headers = List.filterMap identity [ Maybe.map (Http.header "myStringHeader") header_myStringHeader- , Maybe.map (Http.header "MyIntHeader" << String.fromInt) header_MyIntHeader+ , Maybe.map (Http.header "MyIntHeader"+ << String.fromInt) header_MyIntHeader , Maybe.map (Http.header "MyRequiredStringHeader") (Just header_MyRequiredStringHeader)- , Maybe.map (Http.header "MyRequiredIntHeader" << String.fromInt) (Just header_MyRequiredIntHeader)+ , Maybe.map (Http.header "MyRequiredIntHeader"+ << String.fromInt) (Just header_MyRequiredIntHeader) ] , url = Url.Builder.crossOrigin ""