Skip to content

Commit

Permalink
Fixed encoding issue caused by misuse of ByteString causing troubles …
Browse files Browse the repository at this point in the history
…for enums with special chars. Fixes #327.
  • Loading branch information
mpscholten committed Aug 24, 2020
1 parent 8d2ee51 commit e2a85c0
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions IHP/SchemaCompiler.hs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ compileTypes options schema@(Schema statements) =
<> "import qualified Data.Proxy\n"
<> "import GHC.Records\n"
<> "import Data.Data\n"
<> "import qualified Data.String.Conversions\n"
<> "import qualified Data.Text.Encoding\n"
<> "import Database.PostgreSQL.Simple.Types (Query (Query), Binary ( .. ))\n"

compileStatementPreview :: [Statement] -> Statement -> Text
Expand Down Expand Up @@ -311,7 +313,7 @@ compileEnumDataDefinitions enum@(CreateEnumType { name, values }) =
"data " <> modelName <> " = " <> (intercalate " | " valueConstructors) <> " deriving (Eq, Show, Read, Enum)\n"
<> "instance FromField " <> modelName <> " where\n"
<> indent (unlines (map compileFromFieldInstanceForValue values))
<> " fromField field (Just value) = returnError ConversionFailed field \"Unexpected value for enum value\"\n"
<> " fromField field (Just value) = returnError ConversionFailed field (\"Unexpected value for enum value. Got: \" <> Data.String.Conversions.cs value)\n"
<> " fromField field Nothing = returnError UnexpectedNull field \"Unexpected null for enum value\"\n"
<> "instance Default " <> modelName <> " where def = " <> tableNameToModelName (unsafeHead values) <> "\n"
<> "instance ToField " <> modelName <> " where\n" <> indent (unlines (map compileToFieldInstanceForValue values))
Expand All @@ -320,7 +322,7 @@ compileEnumDataDefinitions enum@(CreateEnumType { name, values }) =
where
modelName = tableNameToModelName name
valueConstructors = map tableNameToModelName values
compileFromFieldInstanceForValue value = "fromField field (Just " <> tshow value <> ") = pure " <> tableNameToModelName value
compileFromFieldInstanceForValue value = "fromField field (Just value) | value == (Data.Text.Encoding.encodeUtf8 " <> tshow value <> ") = pure " <> tableNameToModelName value
compileToFieldInstanceForValue value = "toField " <> tableNameToModelName value <> " = toField (" <> tshow value <> " :: Text)"
compileInputValue value = "inputValue " <> tableNameToModelName value <> " = " <> tshow value <> " :: Text"

Expand Down

0 comments on commit e2a85c0

Please sign in to comment.