Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deterministic code gen. #5

Merged
merged 1 commit into from
Jan 11, 2022
Merged
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
6 changes: 3 additions & 3 deletions src/Servant/TS/Gen.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module Servant.TS.Gen (

import Data.HashMap.Strict (HashMap)
import qualified Data.HashMap.Strict as HashMap
import Data.List (groupBy, sortBy)
import Data.List (groupBy, sort, sortBy, sortOn)
import Data.Map.Lazy (Map)
import qualified Data.Map.Lazy as Map
import Data.Proxy
Expand Down Expand Up @@ -175,7 +175,7 @@ writeCustomType opts (tr, t) = let prefix = "export type " <> typeName
writeCustomTypeDef :: Int -> TsRefType -> Text
writeCustomTypeDef i (TsUnion ts) = Text.intercalate ("\n" <> i' <> Text.replicate i " " <> " | ") (writeCustomTypeDef i <$> ts)

writeCustomTypeDef i (TsObject ts) = "{ " <> Text.intercalate ", " ((\(n, t) -> n <> ": " <> writeCustomTypeDef i t) <$> HashMap.toList ts) <> " }"
writeCustomTypeDef i (TsObject ts) = "{ " <> Text.intercalate ", " ((\(n, t) -> n <> ": " <> writeCustomTypeDef i t) <$> sortOn fst (HashMap.toList ts)) <> " }"

writeCustomTypeDef i (TsTuple ts) = let tuple = Text.intercalate ", " $ writeCustomTypeDef i <$> ts
in if length ts == 1 then tuple else "[" <> tuple <> "]"
Expand All @@ -187,7 +187,7 @@ writeCustomType opts (tr, t) = let prefix = "export type " <> typeName
in i' <> "export function is" <> n <> "($u: " <> typeName <> "): $u is " <> tr <> "\n" <>
i' <> "{\n" <>
i' <> i' <> "let $t = <" <> tr <> ">$u;\n" <>
i' <> i' <> "return " <> Text.intercalate " && " (("$t." <> ) . (<> " !== undefined") <$> HashMap.keys ts) <> ";\n" <>
i' <> i' <> "return " <> Text.intercalate " && " (("$t." <> ) . (<> " !== undefined") <$> sort (HashMap.keys ts)) <> ";\n" <>
i' <> "}"

writeCustomTypes :: TsGenOptions -> Map TsTypeName TsRefType -> Text
Expand Down