git.iko.soy/haskell-servant/servant-elm
on master help sign in
some refactoring
d920d8bGergely Szabo
src/Servant/Elm/Internal/Generate.hs+26−23
@@ -129,13 +129,6 @@ defElmImports =
129 , "import Http"
129 , "import Http"
130 , "import String"
130 , "import String"
131 , "import Url.Builder"
131 , "import Url.Builder"
132− , ""
133− , "maybeBoolToIntStr : Maybe Bool -> String"
134− , "maybeBoolToIntStr mx ="
135− , " case mx of"
136− , " Nothing -> \"\""
137− , " Just True -> \"1\""
138− , " Just False -> \"0\""
139 ]
132 ]
140
133
141{-|
134{-|
@@ -396,7 +389,7 @@ mkLetParams opts request =
396 argType = qarg ^. F.queryArgName . F.argType
389 argType = qarg ^. F.queryArgName . F.argType
397 wrapped = isElmMaybeType argType
390 wrapped = isElmMaybeType argType
398 toStringSrc =
391 toStringSrc =
399− toString opts argType
392+ toString opts (maybeOf argType)
400 in
393 in
401 "[" <+> (if wrapped then elmName else "Just" <+> elmName) <> line <>
394 "[" <+> (if wrapped then elmName else "Just" <+> elmName) <> line <>
402 (indent 4 ("|> Maybe.map" <+> composeRight [toStringSrc, "Url.Builder.string" <+> dquotes name]))
395 (indent 4 ("|> Maybe.map" <+> composeRight [toStringSrc, "Url.Builder.string" <+> dquotes name]))
@@ -416,18 +409,18 @@ mkLetParams opts request =
416 F.List ->
409 F.List ->
417 let
410 let
418 argType = qarg ^. F.queryArgName . F.argType
411 argType = qarg ^. F.queryArgName . F.argType
419− convertedVal =
412+ toStringSrc =
420− if isElmListOfMaybeBoolType argType then
413+ toString opts (listOf (maybeOf argType))
421− parens ("maybeBoolToIntStr" <+> "val")
422− else
423− "val"
424 in
414 in
425 elmName <$>
415 elmName <$>
426 indent 4 ("|> List.map"
416 indent 4 ("|> List.map"
427− <+> parens (backslash <> "val ->" <+> "Just"
417+ <+> composeRight
418+ [ toStringSrc
428− <+> parens ("Url.Builder.string"
419+ , "Url.Builder.string" <+> dquotes (name <> "[]")
429− <+> dquotes (name <> "[]")
420+ , "Just"
430− <+> convertedVal)))
421+ ]
422+ )
423+
431 where
424 where
432 elmName = elmQueryArg qarg
425 elmName = elmQueryArg qarg
433 name = qarg ^. F.queryArgName . F.argName . to (stext . F.unPathSegment)
426 name = qarg ^. F.queryArgName . F.argName . to (stext . F.unPathSegment)
@@ -463,9 +456,9 @@ mkRequest opts request =
463 headerArgName = elmHeaderArg header
456 headerArgName = elmHeaderArg header
464 argType = header ^. F.headerArg . F.argType
457 argType = header ^. F.headerArg . F.argType
465 wrapped = isElmMaybeType argType
458 wrapped = isElmMaybeType argType
466− toStringSrc = toString opts argType
459+ toStringSrc = toString opts (maybeOf argType)
467 in
460 in
468− "Maybe.map" <+> parens (("Http.header" <+> composeLeft [dquotes headerName, toStringSrc]))
461+ "Maybe.map" <+> composeLeft ["Http.header" <+> dquotes headerName, toStringSrc]
469 <+>
462 <+>
470 (if wrapped then headerArgName else parens ("Just" <+> headerArgName))
463 (if wrapped then headerArgName else parens ("Just" <+> headerArgName))
471
464
@@ -557,7 +550,7 @@ mkUrl opts segments =
557 F.Cap arg ->
550 F.Cap arg ->
558 let
551 let
559 toStringSrc =
552 toStringSrc =
560− toString opts (arg ^. F.argType)
553+ toString opts (maybeOf (arg ^. F.argType))
561 in
554 in
562 pipeRight [elmCaptureArg s, toStringSrc]
555 pipeRight [elmCaptureArg s, toStringSrc]
563
556
@@ -628,13 +621,23 @@ defaultElmToString :: EType -> Text
628defaultElmToString argType =
621defaultElmToString argType =
629 case argType of
622 case argType of
630 ETyCon (ETCon "Bool") -> "(\\value -> if value then \"1\" else \"0\")"
623 ETyCon (ETCon "Bool") -> "(\\value -> if value then \"1\" else \"0\")"
631− ETyCon (ETCon "Posix") -> "Time.posixToMillis >> String.fromInt)"
624+ ETyCon (ETCon "Float") -> "String.fromFloat"
625+ ETyCon (ETCon "Char") -> "String.fromChar"
632− ETyApp (ETyCon (ETCon "Maybe")) v -> defaultElmToString v
626+ ETyApp (ETyCon (ETCon "Maybe")) v -> "(Maybe.map " <> defaultElmToString v <> " >> Maybe.withDefault \"\")"
633 _ -> "String.fromInt"
627 _ -> "String.fromInt"
634
628
629+
630+maybeOf :: EType -> EType
631+maybeOf (ETyApp (ETyCon (ETCon "Maybe")) v) = v
632+maybeOf v = v
633+
634+listOf :: EType -> EType
635+listOf (ETyApp (ETyCon (ETCon "List")) v) = v
636+listOf v = v
637+
635toString :: ElmOptions -> EType -> Doc
638toString :: ElmOptions -> EType -> Doc
636toString opts argType =
639toString opts argType =
637− if isElmStringType opts argType || isElmMaybeStringType opts argType then
640+ if isElmStringType opts argType then
638 mempty
641 mempty
639 else
642 else
640 stext $ elmToString opts argType
643 stext $ elmToString opts argType