-
Notifications
You must be signed in to change notification settings - Fork 201
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
185 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -176,6 +176,7 @@ data PostgresType | |
| PReal | ||
| PDouble | ||
| PPoint | ||
| PPolygon | ||
| PDate | ||
| PBinary | ||
| PTime | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
{-# LANGUAGE TemplateHaskell #-} | ||
{-| | ||
Module: IHP.Postgres.Polygon | ||
Description: Adds support for the Postgres Polygon type | ||
Copyright: (c) digitally induced GmbH, 2020 | ||
-} | ||
module IHP.Postgres.Polygon where | ||
|
||
import GHC.Float | ||
import BasicPrelude | ||
|
||
import Database.PostgreSQL.Simple.ToField | ||
import Database.PostgreSQL.Simple.FromField | ||
import qualified Database.PostgreSQL.Simple.TypeInfo.Static as TI | ||
import Database.PostgreSQL.Simple.TypeInfo.Macro as TI | ||
import Data.ByteString.Builder (byteString, char8) | ||
import Data.Attoparsec.ByteString.Char8 hiding (Result, char8, Parser(..)) | ||
import Data.Attoparsec.Internal.Types (Parser) | ||
import IHP.Postgres.Point | ||
|
||
-- | Represents a Postgres Polygon | ||
-- | ||
-- See https://www.postgresql.org/docs/9.5/datatype-geometric.html | ||
data Polygon = Polygon { points :: [Point] } | ||
deriving (Eq, Show, Ord) | ||
|
||
instance FromField Polygon where | ||
fromField f v = | ||
if typeOid f /= $(inlineTypoid TI.polygon) | ||
then returnError Incompatible f "" | ||
else case v of | ||
Nothing -> returnError UnexpectedNull f "" | ||
Just bs -> | ||
case parseOnly parsePolygon bs of | ||
Left err -> returnError ConversionFailed f err | ||
Right val -> pure val | ||
|
||
parsePolygon :: Parser ByteString Polygon | ||
parsePolygon = do | ||
string "(" | ||
points <- parsePoint `sepBy` (char ',') | ||
string ")" | ||
pure $ Polygon points | ||
|
||
instance ToField Polygon where | ||
toField = serializePolygon | ||
|
||
serializePolygon :: Polygon -> Action | ||
serializePolygon Polygon { points } = Many $ | ||
(Plain (byteString "polygon'")): | ||
( (intersperse (Plain $ char8 ',') $ map serializePoint' points) | ||
++ [ Plain (char8 '\'') ]) | ||
where | ||
serializePoint' :: Point -> Action | ||
serializePoint' Point { x, y } = Many $ | ||
[ Plain (char8 '(') | ||
, toField x | ||
, Plain (char8 ',') | ||
, toField y | ||
, Plain (char8 ')') | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
{-| | ||
Module: Test.Postgres.Polygon | ||
Copyright: (c) digitally induced GmbH, 2021 | ||
-} | ||
module Test.Postgres.Polygon where | ||
|
||
import Test.Hspec | ||
import Test.Postgres.Support | ||
import IHP.Prelude | ||
import IHP.Postgres.Point | ||
import IHP.Postgres.Polygon | ||
import Database.PostgreSQL.Simple.ToField | ||
import qualified Data.Attoparsec.ByteString.Char8 as Attoparsec | ||
|
||
tests = do | ||
let rawPoint1 = "(100,200)" | ||
let parsedPoint1 = Point { x = 100, y = 200 } | ||
let rawPoint2 = "(300,400)" | ||
let parsedPoint2 = Point { x = 300, y = 400 } | ||
let raw = "(" ++ rawPoint1 ++ "," ++ rawPoint2 ++ ")" | ||
let parsed = Polygon { points = [ parsedPoint1, parsedPoint2 ] } | ||
let serialized = Many | ||
[ Plain "polygon'" | ||
, Many | ||
[ Plain "(" | ||
, Plain "100.0" | ||
, Plain "," | ||
, Plain "200.0" | ||
, Plain ")" | ||
] | ||
, Plain "," | ||
, Many | ||
[ Plain "(" | ||
, Plain "300.0" | ||
, Plain "," | ||
, Plain "400.0" | ||
, Plain ")" | ||
] | ||
, Plain "'" | ||
] | ||
|
||
describe "Polygon" do | ||
describe "Parser" do | ||
it "Should Parse" do | ||
Attoparsec.parseOnly parsePolygon raw `shouldBe` Right parsed | ||
|
||
describe "Serializer" do | ||
it "Should Serialize" do | ||
serializePolygon parsed `shouldBe` serialized |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters