Skip to content

Commit 2e5c123

Browse files
committed
Add a better check for enum mode
1 parent 7933c64 commit 2e5c123

File tree

4 files changed

+31
-11
lines changed

4 files changed

+31
-11
lines changed

aeson-typescript.cabal

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
cabal-version: 1.12
22

3-
-- This file has been generated from package.yaml by hpack version 0.35.1.
3+
-- This file has been generated from package.yaml by hpack version 0.36.0.
44
--
55
-- see: https://github.com/sol/hpack
66

@@ -67,6 +67,7 @@ library
6767
build-depends:
6868
aeson
6969
, base >=4.7 && <5
70+
, bytestring
7071
, containers
7172
, mtl
7273
, string-interpolate

package.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ tested-with:
3535
dependencies:
3636
- aeson
3737
- base >= 4.7 && < 5
38+
- bytestring
3839
- containers
3940
- mtl
4041
- string-interpolate

src/Data/Aeson/TypeScript/Formatting.hs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
module Data.Aeson.TypeScript.Formatting where
44

5+
import Data.Aeson as A
56
import Data.Aeson.TypeScript.Types
7+
import qualified Data.ByteString.Lazy.Char8 as BL8
68
import Data.Function ((&))
79
import qualified Data.List as L
810
import Data.Maybe
@@ -23,15 +25,31 @@ formatTSDeclaration :: FormattingOptions -> TSDeclaration -> String
2325
formatTSDeclaration (FormattingOptions {..}) (TSTypeAlternatives name genericVariables names maybeDoc) =
2426
makeDocPrefix maybeDoc <> mainDeclaration
2527
where
26-
mainDeclaration = case typeAlternativesFormat of
28+
mainDeclaration = case chooseTypeAlternativesFormat typeAlternativesFormat of
2729
Enum -> [i|#{exportPrefix exportMode}enum #{typeNameModifier name} { #{alternativesEnum} }|]
30+
where
31+
alternativesEnum = T.intercalate ", " $ [toEnumName entry | entry <- T.pack <$> names]
2832
EnumWithType -> [i|#{exportPrefix exportMode}enum #{typeNameModifier name}Enum { #{alternativesEnumWithType} }#{enumType}|]
33+
where
34+
alternativesEnumWithType = T.intercalate ", " $ [toEnumName entry <> "=" <> entry | entry <- T.pack <$> names]
35+
enumType = [i|\n\ntype #{name} = keyof typeof #{typeNameModifier name}Enum;|] :: T.Text
2936
TypeAlias -> [i|#{exportPrefix exportMode}type #{typeNameModifier name}#{getGenericBrackets genericVariables} = #{alternatives};|]
37+
where
38+
alternatives = T.intercalate " | " (fmap T.pack names)
39+
40+
-- Only allow certain formats if some checks pass
41+
chooseTypeAlternativesFormat Enum
42+
| all isDoubleQuotedString names = Enum
43+
| otherwise = TypeAlias
44+
chooseTypeAlternativesFormat EnumWithType
45+
| all isDoubleQuotedString names = EnumWithType
46+
| otherwise = TypeAlias
47+
chooseTypeAlternativesFormat x = x
48+
49+
isDoubleQuotedString s = case A.eitherDecode (BL8.pack s) of
50+
Right (A.String _) -> True
51+
_ -> False
3052

31-
alternatives = T.intercalate " | " (fmap T.pack names)
32-
alternativesEnum = T.intercalate ", " $ [toEnumName entry | entry <- T.pack <$> names]
33-
alternativesEnumWithType = T.intercalate ", " $ [toEnumName entry <> "=" <> entry | entry <- T.pack <$> names]
34-
enumType = [i|\n\ntype #{name} = keyof typeof #{typeNameModifier name}Enum;|] :: T.Text
3553
toEnumName = T.replace "\"" ""
3654

3755
formatTSDeclaration (FormattingOptions {..}) (TSInterfaceDeclaration interfaceName genericVariables (filter (not . isNoEmitTypeScriptField) -> members) maybeDoc) =

test/Formatting.hs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ module Formatting (tests) where
55

66
import Control.Exception
77
import Data.Aeson (defaultOptions)
8-
import Data.Aeson.TH
98
import Data.Aeson.TypeScript.TH
109
import Data.Proxy
1110
import Data.String.Interpolate
@@ -20,7 +19,6 @@ $(deriveTypeScript defaultOptions ''D2)
2019

2120
data Unit = U deriving (Eq, Show)
2221
$(deriveTypeScript defaultOptions ''Unit)
23-
$(deriveJSON defaultOptions ''Unit)
2422

2523
data PrimeInType' = PrimeInType
2624
$(deriveTypeScript defaultOptions ''PrimeInType')
@@ -66,9 +64,11 @@ tests = describe "Formatting" $ do
6664

6765
enum D2 { S2, F2 }|]
6866

69-
-- it "should generate a TS Enum from unit" $
70-
-- formatTSDeclarations' (defaultFormattingOptions { typeAlternativesFormat = Enum }) (getTypeScriptDeclarations @Unit Proxy) `shouldBe`
71-
-- [__i|enum Unit { U }|]
67+
it "should generate a TS Enum from unit" $
68+
formatTSDeclarations' (defaultFormattingOptions { typeAlternativesFormat = Enum }) (getTypeScriptDeclarations @Unit Proxy) `shouldBe`
69+
[__i|type Unit = IU;
70+
71+
type IU = void[];|]
7272

7373
describe "and the EnumWithType format option is set" $
7474
it "should generate a TS Enum with a type declaration" $

0 commit comments

Comments
 (0)