Skip to content

Commit

Permalink
Add Ord to Enums (#1848)
Browse files Browse the repository at this point in the history
* Add Ord to Enums

* Apply suggestions from code review
  • Loading branch information
amitaibu authored Oct 20, 2023
1 parent e66f966 commit 7c55c8e
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
2 changes: 1 addition & 1 deletion IHP/SchemaCompiler.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 3 additions & 3 deletions Test/SchemaCompilerSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 7c55c8e

Please sign in to comment.