Skip to content

Commit 007e161

Browse files
committed
Always include string in enums to match aeson
1 parent e39ae7c commit 007e161

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

src/Data/Aeson/TypeScript/Formatting.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ formatTSDeclaration (FormattingOptions {..}) (TSTypeAlternatives name genericVar
2828
mainDeclaration = case chooseTypeAlternativesFormat typeAlternativesFormat of
2929
Enum -> [i|#{exportPrefix exportMode}enum #{typeNameModifier name} { #{alternativesEnum} }|]
3030
where
31-
alternativesEnum = T.intercalate ", " $ [toEnumName entry | entry <- T.pack <$> names]
31+
alternativesEnum = T.intercalate ", " $ [toEnumName entry <> "=" <> entry | entry <- T.pack <$> names]
3232
EnumWithType -> [i|#{exportPrefix exportMode}enum #{typeNameModifier name}Enum { #{alternativesEnumWithType} }#{enumType}|]
3333
where
3434
alternativesEnumWithType = T.intercalate ", " $ [toEnumName entry <> "=" <> entry | entry <- T.pack <$> names]

test/Formatting.hs

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

66
import Control.Exception
7-
import Data.Aeson (defaultOptions)
7+
import Data.Aeson (SumEncoding(UntaggedValue), defaultOptions, sumEncoding, tagSingleConstructors)
88
import Data.Aeson.TypeScript.TH
99
import Data.Proxy
1010
import Data.String.Interpolate
@@ -17,9 +17,14 @@ $(deriveTypeScript defaultOptions ''D)
1717
data D2 = S2 | F2 deriving (Eq, Show)
1818
$(deriveTypeScript defaultOptions ''D2)
1919

20+
-- A.encode U --> "[]"
2021
data Unit = U deriving (Eq, Show)
2122
$(deriveTypeScript defaultOptions ''Unit)
2223

24+
-- A.encode UTagSingle --> "\"UTagSingle\""
25+
data UnitTagSingle = UTagSingle deriving (Eq, Show)
26+
$(deriveTypeScript (defaultOptions { tagSingleConstructors = True, sumEncoding = UntaggedValue }) ''UnitTagSingle)
27+
2328
data PrimeInType' = PrimeInType
2429
$(deriveTypeScript defaultOptions ''PrimeInType')
2530

@@ -56,25 +61,33 @@ tests = describe "Formatting" $ do
5661
describe "and the Enum format option is set" $ do
5762
it "should generate a TS Enum" $
5863
formatTSDeclarations' (defaultFormattingOptions { typeAlternativesFormat = Enum }) (getTypeScriptDeclarations @D Proxy) `shouldBe`
59-
[i|enum D { S, F }|]
64+
[i|enum D { S="S", F="F" }|]
6065

6166
it "should generate a TS Enum with multiple" $
6267
formatTSDeclarations' (defaultFormattingOptions { typeAlternativesFormat = Enum }) (getTypeScriptDeclarations @D Proxy <> getTypeScriptDeclarations @D2 Proxy) `shouldBe`
63-
[__i|enum D { S, F }
68+
[__i|enum D { S="S", F="F" }
6469

65-
enum D2 { S2, F2 }|]
70+
enum D2 { S2="S2", F2="F2" }|]
6671

67-
it "should generate a TS Enum from unit" $
72+
it "should generate a normal type from Unit, singe tagSingleConstructors=False by default" $
6873
formatTSDeclarations' (defaultFormattingOptions { typeAlternativesFormat = Enum }) (getTypeScriptDeclarations @Unit Proxy) `shouldBe`
6974
[__i|type Unit = IU;
7075

7176
type IU = void[];|]
7277

73-
describe "and the EnumWithType format option is set" $
78+
it "should generate a suitable enum from UnitTagSingle" $
79+
formatTSDeclarations' (defaultFormattingOptions { typeAlternativesFormat = Enum }) (getTypeScriptDeclarations @UnitTagSingle Proxy) `shouldBe`
80+
[__i|enum UnitTagSingle { UTagSingle="UTagSingle" }|]
81+
82+
describe "and the EnumWithType format option is set" $ do
7483
it "should generate a TS Enum with a type declaration" $
7584
formatTSDeclarations' (defaultFormattingOptions { typeAlternativesFormat = EnumWithType }) (getTypeScriptDeclarations @D Proxy) `shouldBe`
7685
[i|enum DEnum { S="S", F="F" }\n\ntype D = keyof typeof DEnum;|]
7786

87+
it "should also work for UnitTagSingle" $
88+
formatTSDeclarations' (defaultFormattingOptions { typeAlternativesFormat = EnumWithType }) (getTypeScriptDeclarations @UnitTagSingle Proxy) `shouldBe`
89+
[i|enum UnitTagSingleEnum { UTagSingle="UTagSingle" }\n\ntype UnitTagSingle = keyof typeof UnitTagSingleEnum;|]
90+
7891
describe "when the name has an apostrophe" $ do
7992
describe "in the type" $ do
8093
it "throws an error" $ do

0 commit comments

Comments
 (0)