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

Add Ord to Enums #1848

Merged
merged 2 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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
12 changes: 6 additions & 6 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 Expand Up @@ -178,7 +178,7 @@ tests = do

type instance PrimaryKey "users" = UUID

type User = User'
type User = User'
amitaibu marked this conversation as resolved.
Show resolved Hide resolved

type instance GetTableName (User' ) = "users"
type instance GetModelByTableName "users" = User
Expand Down Expand Up @@ -247,7 +247,7 @@ tests = do

type instance PrimaryKey "users" = UUID

type User = User'
type User = User'
amitaibu marked this conversation as resolved.
Show resolved Hide resolved

type instance GetTableName (User' ) = "users"
type instance GetModelByTableName "users" = User
Expand Down Expand Up @@ -316,7 +316,7 @@ tests = do

type instance PrimaryKey "users" = UUID

type User = User'
type User = User'
amitaibu marked this conversation as resolved.
Show resolved Hide resolved

type instance GetTableName (User' ) = "users"
type instance GetModelByTableName "users" = User
Expand Down
Loading