Skip to content

Commit

Permalink
Extracted IHP.Postgres.* into it's own ihp-postgresql-simple-extra mo…
Browse files Browse the repository at this point in the history
…dule

This helps speed up compile time for IHP builds
  • Loading branch information
mpscholten committed May 1, 2023
1 parent 0ee0812 commit 6e0c329
Show file tree
Hide file tree
Showing 21 changed files with 180 additions and 37 deletions.
20 changes: 20 additions & 0 deletions NixSupport/haskell-packages/ihp-postgresql-simple-extra.nix
@@ -0,0 +1,20 @@
{ mkDerivation, aeson, attoparsec, base, basic-prelude, bytestring
, hspec, hspec-discover, ip, lib, postgresql-simple, text, time
, time-compat
}:
mkDerivation {
pname = "ihp-postgresql-simple-extra";
version = "1.0.1";
src = ./../../ihp-postgresql-simple-extra;
libraryHaskellDepends = [
aeson attoparsec base basic-prelude bytestring ip postgresql-simple
text time time-compat
];
testHaskellDepends = [
aeson attoparsec base basic-prelude bytestring hspec hspec-discover
ip postgresql-simple text time time-compat
];
testToolDepends = [ hspec-discover ];
description = "Extra data types for postgresql-simple";
license = lib.licenses.mit;
}
1 change: 1 addition & 0 deletions devenv.nix
Expand Up @@ -77,6 +77,7 @@
mmark-cli
hspec
ihp-hsx
ihp-postgresql-simple-extra
]);

scripts.tests.exec = ''
Expand Down
Expand Up @@ -13,11 +13,11 @@ import Database.PostgreSQL.Simple.FromField
import qualified Database.PostgreSQL.Simple.TypeInfo.Static as TI
import Database.PostgreSQL.Simple.TypeInfo.Macro as TI
import Data.Attoparsec.ByteString.Char8 as Attoparsec
import Data.String.Conversions (cs)

-- We use the @ip@ package for representing IP addresses
import qualified Net.IP as IP
import Net.IP (IP)
import qualified Data.Text.Encoding as Text

instance FromField IP where
fromField f v =
Expand All @@ -32,7 +32,7 @@ instance FromField IP where
where
parser = do
ip <- Attoparsec.takeWhile (\char -> char /= ' ')
case IP.decode (cs ip) of
case IP.decode (Text.decodeUtf8 ip) of
Just ip -> pure ip
Nothing -> fail "Invalid IP"

Expand Down
Expand Up @@ -13,10 +13,10 @@ import Database.PostgreSQL.Simple.FromField
import qualified Database.PostgreSQL.Simple.TypeInfo.Static as TI
import Database.PostgreSQL.Simple.TypeInfo.Macro as TI
import Data.Attoparsec.ByteString.Char8 as Attoparsec
import Data.String.Conversions (cs)
import Data.Aeson

import IHP.Postgres.TimeParser (PGInterval(..))
import qualified Data.Text.Encoding as Text

instance FromField PGInterval where
fromField f v =
Expand All @@ -39,4 +39,4 @@ instance FromJSON PGInterval where
parseJSON = withText "PGInterval" $ \text -> pure (PGInterval (encodeUtf8 text))

instance ToJSON PGInterval where
toJSON (PGInterval pgInterval) = String (cs pgInterval)
toJSON (PGInterval pgInterval) = String (Text.decodeUtf8 pgInterval)
File renamed without changes.
Expand Up @@ -6,7 +6,6 @@ Copyright: (c) digitally induced GmbH, 2022
-}
module IHP.Postgres.Polygon where

import GHC.Float
import BasicPrelude

import Database.PostgreSQL.Simple.ToField
Expand Down
Expand Up @@ -7,14 +7,14 @@ Copyright: (c) digitally induced GmbH, 2021
module IHP.Postgres.TSVector where

import BasicPrelude
import Data.String.Conversions (cs)
import IHP.Postgres.TypeInfo
import Database.PostgreSQL.Simple.ToField
import Database.PostgreSQL.Simple.FromField
import Database.PostgreSQL.Simple.TypeInfo.Macro
import Data.Attoparsec.ByteString.Char8 as Attoparsec hiding (Parser(..))
import Data.Attoparsec.Internal.Types (Parser)
import Data.ByteString.Builder (byteString, charUtf8)
import qualified Data.Text.Encoding as Text

-- | Represents a Postgres tsvector
--
Expand Down Expand Up @@ -63,7 +63,7 @@ parseTSVector = TSVector <$> many' parseLexeme
weight <- option 'D' $ choice [char 'A', char 'B', char 'C', char 'D']
pure $ LexemeRanking { position = truncate position, weight }

pure $ Lexeme { token = cs token, ranking }
pure $ Lexeme { token = Text.decodeUtf8 token, ranking }


instance ToField TSVector where
Expand All @@ -73,7 +73,7 @@ serializeTSVector :: TSVector -> Action
serializeTSVector (TSVector lexemes) = Many $ map serializeLexeme lexemes
where
serializeLexeme Lexeme { token, ranking } = Many
[ Plain $ byteString $ cs token
[ Plain $ byteString $ Text.encodeUtf8 token
, toField ':'
, Many $ intersperse (toField ',') (map serializeLexemeRanking ranking)
]
Expand Down
Expand Up @@ -3,20 +3,11 @@ module IHP.Postgres.TimeParser where

import BasicPrelude hiding (takeWhile)
import Data.Attoparsec.ByteString.Char8
import Data.Attoparsec.Combinator
import Data.Bits ((.&.))
import Data.ByteString (ByteString)
import Data.Char (ord)
import Control.Applicative ((<|>))

import Data.Fixed (Pico, Fixed(MkFixed))
import Data.Int (Int64)
import Data.Maybe (fromMaybe)
import Data.Time.Calendar.Compat (Day, fromGregorianValid, addDays)
import Data.Time.Clock.Compat (UTCTime(..), NominalDiffTime)
import Data.Time.Format.ISO8601.Compat (iso8601ParseM)
import Data.Time.LocalTime.Compat (CalendarDiffTime)
import Data.String.Conversions (cs)
import Data.Time.Clock.Compat (NominalDiffTime)
import qualified Data.ByteString.Char8 as B8
import qualified Data.Time.LocalTime.Compat as Local

Expand Down Expand Up @@ -49,7 +40,7 @@ data PGTimeInterval = PGTimeInterval { pgYears :: !Integer

unpackInterval :: PGInterval -> PGTimeInterval
unpackInterval (PGInterval bs) = case parseOnly pPGInterval bs of
Left err -> error ("Couldn't parse PGInterval. " <> cs err)
Left err -> error ("Couldn't parse PGInterval. " <> err)
Right val -> val


Expand Down
File renamed without changes.
21 changes: 21 additions & 0 deletions ihp-postgresql-simple-extra/LICENSE
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2020 digitally induced GmbH

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
9 changes: 9 additions & 0 deletions ihp-postgresql-simple-extra/README.md
@@ -0,0 +1,9 @@
# ihp-postgres-simple-extra

This package is included by default in IHP apps and implements support for postgres data types that are not supported by the `postgresql-simple` package by default:

- `INET`
- `INTERVAL`
- `POINT`
- `POLYGON`
- `TSVECTOR`
Expand Up @@ -5,8 +5,6 @@ Copyright: (c) digitally induced GmbH, 2023
module Test.Postgres.Interval where

import Test.Hspec
import Test.Postgres.Support
import IHP.Prelude
import IHP.Postgres.Interval
import IHP.Postgres.TimeParser
import Database.PostgreSQL.Simple.ToField
Expand Down
Expand Up @@ -4,9 +4,9 @@ Copyright: (c) digitally induced GmbH, 2021
-}
module Test.Postgres.Point where

import Data.Either
import Test.Hspec
import Test.Postgres.Support
import IHP.Prelude
import Test.Postgres.Support ()
import IHP.Postgres.Point
import Database.PostgreSQL.Simple.ToField
import qualified Data.Attoparsec.ByteString.Char8 as Attoparsec
Expand Down
Expand Up @@ -4,9 +4,10 @@ Copyright: (c) digitally induced GmbH, 2022
-}
module Test.Postgres.Polygon where

import CorePrelude
import Data.Either
import Test.Hspec
import Test.Postgres.Support
import IHP.Prelude
import IHP.Postgres.Point
import IHP.Postgres.Polygon
import Database.PostgreSQL.Simple.ToField
Expand All @@ -17,7 +18,7 @@ tests = do
let parsedPoint1 = Point { x = 100, y = 200 }
let rawPoint2 = "(300,400)"
let parsedPoint2 = Point { x = 300, y = 400 }
let raw = "(" ++ rawPoint1 ++ "," ++ rawPoint2 ++ ")"
let raw = "(" <> rawPoint1 <> "," <> rawPoint2 <> ")"
let parsed = Polygon { points = [ parsedPoint1, parsedPoint2 ] }
let serialized = Many
[ Plain "polygon'"
Expand Down
Expand Up @@ -4,7 +4,7 @@ Copyright: (c) digitally induced GmbH, 2021
-}
module Test.Postgres.Support where

import IHP.Prelude
import Prelude
import Data.ByteString.Builder (toLazyByteString)
import Database.PostgreSQL.Simple.ToField
import qualified Data.ByteString.Builder as Builder
Expand Down
Expand Up @@ -4,9 +4,9 @@ Copyright: (c) digitally induced GmbH, 2021
-}
module Test.Postgres.TSVector where

import Prelude
import Test.Hspec
import Test.Postgres.Support
import IHP.Prelude
import IHP.Postgres.TSVector
import Database.PostgreSQL.Simple.ToField
import qualified Data.Attoparsec.ByteString.Char8 as Attoparsec
Expand Down
16 changes: 16 additions & 0 deletions ihp-postgresql-simple-extra/Test/Spec.hs
@@ -0,0 +1,16 @@
module Main where

import CorePrelude
import Test.Hspec

import qualified Test.Postgres.Point
import qualified Test.Postgres.Polygon
import qualified Test.Postgres.Interval
import qualified Test.Postgres.TSVector

main :: IO ()
main = hspec do
Test.Postgres.Point.tests
Test.Postgres.Polygon.tests
Test.Postgres.Interval.tests
Test.Postgres.TSVector.tests
93 changes: 93 additions & 0 deletions ihp-postgresql-simple-extra/ihp-postgresql-simple-extra.cabal
@@ -0,0 +1,93 @@
cabal-version: 2.2
name: ihp-postgresql-simple-extra
version: 1.0.1
synopsis: Extra data types for postgresql-simple
description: This package is included by default in IHP apps and implements support for postgres data types that are not supported by the postgresql-simple package by default
license: MIT
license-file: LICENSE
author: digitally induced GmbH
maintainer: support@digitallyinduced.com
bug-reports: https://github.com/digitallyinduced/ihp/issues
category: Database
build-type: Simple
extra-source-files: README.md

source-repository head
type: git
location: https://github.com/digitallyinduced/ihp.git

common shared-properties
default-language: Haskell2010
build-depends:
base
, bytestring
, attoparsec
, basic-prelude
, text
, postgresql-simple
, ip
, time
, time-compat
, aeson
default-extensions:
OverloadedStrings
, NoImplicitPrelude
, ImplicitParams
, Rank2Types
, NamedFieldPuns
, TypeSynonymInstances
, FlexibleInstances
, DisambiguateRecordFields
, DuplicateRecordFields
, OverloadedLabels
, FlexibleContexts
, DataKinds
, QuasiQuotes
, TypeFamilies
, PackageImports
, ScopedTypeVariables
, RecordWildCards
, TypeApplications
, DataKinds
, InstanceSigs
, DeriveGeneric
, MultiParamTypeClasses
, TypeOperators
, DeriveDataTypeable
, DefaultSignatures
, BangPatterns
, FunctionalDependencies
, PartialTypeSignatures
, BlockArguments
, LambdaCase
, StandaloneDeriving
, TemplateHaskell
, OverloadedRecordDot

library
import: shared-properties
hs-source-dirs: .
exposed-modules:
IHP.Postgres.TypeInfo
, IHP.Postgres.Point
, IHP.Postgres.Interval
, IHP.Postgres.TimeParser
, IHP.Postgres.Polygon
, IHP.Postgres.Inet
, IHP.Postgres.TSVector

test-suite spec
import: shared-properties
type: exitcode-stdio-1.0
other-modules:
Test.Postgres.Interval
, Test.Postgres.Point
, Test.Postgres.Polygon
, Test.Postgres.Support
, Test.Postgres.TSVector
hs-source-dirs: .
main-is: Test/Spec.hs
build-depends:
hspec >= 2.7
, hspec-discover >= 2.7
, ihp-postgresql-simple-extra
9 changes: 1 addition & 8 deletions ihp.cabal
Expand Up @@ -49,7 +49,6 @@ common shared-properties
, random-strings
, uuid
, time
, time-compat
, attoparsec
, ghc-prim
, case-insensitive
Expand Down Expand Up @@ -105,6 +104,7 @@ common shared-properties
, unagi-chan
, with-utf8
, ihp-hsx
, ihp-postgresql-simple-extra
default-extensions:
OverloadedStrings
, NoImplicitPrelude
Expand Down Expand Up @@ -248,13 +248,6 @@ library
, IHP.Test.Mocking
, IHP.Test.Database
, IHP.Version
, IHP.Postgres.TypeInfo
, IHP.Postgres.Point
, IHP.Postgres.Interval
, IHP.Postgres.TimeParser
, IHP.Postgres.Polygon
, IHP.Postgres.Inet
, IHP.Postgres.TSVector
, Paths_ihp
, IHP.Job.Queue
, IHP.Job.Runner
Expand Down

0 comments on commit 6e0c329

Please sign in to comment.