From 7c55c8ebb26adad320398167616a5d0848f48512 Mon Sep 17 00:00:00 2001 From: Amitai Burstein Date: Fri, 20 Oct 2023 16:37:34 +0300 Subject: [PATCH] Add `Ord` to Enums (#1848) * Add Ord to Enums * Apply suggestions from code review --- IHP/SchemaCompiler.hs | 2 +- Test/SchemaCompilerSpec.hs | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/IHP/SchemaCompiler.hs b/IHP/SchemaCompiler.hs index 8bcff36fd..dfd521681 100644 --- a/IHP/SchemaCompiler.hs +++ b/IHP/SchemaCompiler.hs @@ -450,7 +450,7 @@ findForeignKeyConstraint CreateTable { name } column = compileEnumDataDefinitions :: (?schema :: Schema) => Statement -> Text compileEnumDataDefinitions CreateEnumType { values = [] } = "" -- Ignore enums without any values compileEnumDataDefinitions enum@(CreateEnumType { name, values }) = - "data " <> modelName <> " = " <> (intercalate " | " valueConstructors) <> " deriving (Eq, Show, Read, Enum, Bounded)\n" + "data " <> modelName <> " = " <> (intercalate " | " valueConstructors) <> " deriving (Eq, Show, Read, Enum, Bounded, Ord)\n" <> "instance FromField " <> modelName <> " where\n" <> indent (unlines (map compileFromFieldInstanceForValue values)) <> " fromField field (Just value) = returnError ConversionFailed field (\"Unexpected value for enum value. Got: \" <> Data.String.Conversions.cs value)\n" diff --git a/Test/SchemaCompilerSpec.hs b/Test/SchemaCompilerSpec.hs index 03d6dbc94..704eff7bd 100644 --- a/Test/SchemaCompilerSpec.hs +++ b/Test/SchemaCompilerSpec.hs @@ -19,7 +19,7 @@ tests = do let output = compileStatementPreview [statement] statement |> Text.strip output `shouldBe` [trimming| - data Mood = Happy | VeryHappy | Sad | VerySad deriving (Eq, Show, Read, Enum, Bounded) + data Mood = Happy | VeryHappy | Sad | VerySad deriving (Eq, Show, Read, Enum, Bounded, Ord) instance FromField Mood where fromField field (Just value) | value == (Data.Text.Encoding.encodeUtf8 "happy") = pure Happy fromField field (Just value) | value == (Data.Text.Encoding.encodeUtf8 "very happy") = pure VeryHappy @@ -56,7 +56,7 @@ tests = do let output = compileStatementPreview [statement] statement |> Text.strip output `shouldBe` [trimming| - data Province = Alberta | Britishcolumbia | Saskatchewan | Manitoba | Ontario | Quebec | Novascotia | Newbrunswick | Princeedwardisland | Newfoundlandandlabrador deriving (Eq, Show, Read, Enum, Bounded) + data Province = Alberta | Britishcolumbia | Saskatchewan | Manitoba | Ontario | Quebec | Novascotia | Newbrunswick | Princeedwardisland | Newfoundlandandlabrador deriving (Eq, Show, Read, Enum, Bounded, Ord) instance FromField Province where fromField field (Just value) | value == (Data.Text.Encoding.encodeUtf8 "Alberta") = pure Alberta fromField field (Just value) | value == (Data.Text.Encoding.encodeUtf8 "BritishColumbia") = pure Britishcolumbia @@ -102,7 +102,7 @@ tests = do let output = compileStatementPreview [enum1, enum2] enum1 |> Text.strip output `shouldBe` [trimming| - data PropertyType = PropertyTypeApartment | House deriving (Eq, Show, Read, Enum, Bounded) + data PropertyType = PropertyTypeApartment | House deriving (Eq, Show, Read, Enum, Bounded, Ord) instance FromField PropertyType where fromField field (Just value) | value == (Data.Text.Encoding.encodeUtf8 "APARTMENT") = pure PropertyTypeApartment fromField field (Just value) | value == (Data.Text.Encoding.encodeUtf8 "HOUSE") = pure House