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

Derive {From,To}JSON for EntityDef #1540

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions persistent/ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog for persistent

## 2.14.7.0

* [#1540](https://github.com/yesodweb/persistent/pull/1540)
* Derive `FromJSON`, `ToJSON`, and `Generic` for `EntityDef` and its dependencies.

## 2.14.6.1

* [#1528](https://github.com/yesodweb/persistent/pull/1528)
Expand Down
87 changes: 81 additions & 6 deletions persistent/Database/Persist/Names.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DeriveLift #-}

-- | This module contains types and functions for working with and
Expand All @@ -6,7 +7,9 @@
-- @since 2.13.0.0
module Database.Persist.Names where

import Data.Aeson (FromJSON, ToJSON)
import Data.Text (Text)
import GHC.Generics (Generic)
import Language.Haskell.TH.Syntax (Lift)
-- Bring `Lift (Map k v)` instance into scope, as well as `Lift Text`
-- instance on pre-1.2.4 versions of `text`
Expand All @@ -23,7 +26,19 @@ class DatabaseName a where
--
-- @since 2.12.0.0
newtype FieldNameDB = FieldNameDB { unFieldNameDB :: Text }
deriving (Show, Eq, Read, Ord, Lift)
deriving (Show, Eq, Read, Ord, Lift, Generic)

-- | Note: JSON representations not considered part of the public API; breaking
-- changes will not be reflected in the package version!
--
-- @since 2.14.7.0
instance FromJSON FieldNameDB

-- | Note: JSON representations not considered part of the public API; breaking
-- changes will not be reflected in the package version!
--
-- @since 2.14.7.0
instance ToJSON FieldNameDB

-- | @since 2.12.0.0
instance DatabaseName FieldNameDB where
Expand All @@ -34,21 +49,57 @@ instance DatabaseName FieldNameDB where
--
-- @since 2.12.0.0
newtype FieldNameHS = FieldNameHS { unFieldNameHS :: Text }
deriving (Show, Eq, Read, Ord, Lift)
deriving (Show, Eq, Read, Ord, Lift, Generic)

-- | Note: JSON representations not considered part of the public API; breaking
-- changes will not be reflected in the package version!
--
-- @since 2.14.7.0
instance FromJSON FieldNameHS

-- | Note: JSON representations not considered part of the public API; breaking
-- changes will not be reflected in the package version!
--
-- @since 2.14.7.0
instance ToJSON FieldNameHS

-- | An 'EntityNameHS' represents the Haskell-side name that @persistent@
-- will use for an entity.
--
-- @since 2.12.0.0
newtype EntityNameHS = EntityNameHS { unEntityNameHS :: Text }
deriving (Show, Eq, Read, Ord, Lift)
deriving (Show, Eq, Read, Ord, Lift, Generic)

-- | Note: JSON representations not considered part of the public API; breaking
-- changes will not be reflected in the package version!
--
-- @since 2.14.7.0
instance FromJSON EntityNameHS

-- | Note: JSON representations not considered part of the public API; breaking
-- changes will not be reflected in the package version!
--
-- @since 2.14.7.0
instance ToJSON EntityNameHS

-- | An 'EntityNameDB' represents the datastore-side name that @persistent@
-- will use for an entity.
--
-- @since 2.12.0.0
newtype EntityNameDB = EntityNameDB { unEntityNameDB :: Text }
deriving (Show, Eq, Read, Ord, Lift)
deriving (Show, Eq, Read, Ord, Lift, Generic)

-- | Note: JSON representations not considered part of the public API; breaking
-- changes will not be reflected in the package version!
--
-- @since 2.14.7.0
instance FromJSON EntityNameDB

-- | Note: JSON representations not considered part of the public API; breaking
-- changes will not be reflected in the package version!
--
-- @since 2.14.7.0
instance ToJSON EntityNameDB

instance DatabaseName EntityNameDB where
escapeWith f (EntityNameDB n) = f n
Expand All @@ -58,7 +109,19 @@ instance DatabaseName EntityNameDB where
--
-- @since 2.12.0.0
newtype ConstraintNameDB = ConstraintNameDB { unConstraintNameDB :: Text }
deriving (Show, Eq, Read, Ord, Lift)
deriving (Show, Eq, Read, Ord, Lift, Generic)

-- | Note: JSON representations not considered part of the public API; breaking
-- changes will not be reflected in the package version!
--
-- @since 2.14.7.0
instance FromJSON ConstraintNameDB

-- | Note: JSON representations not considered part of the public API; breaking
-- changes will not be reflected in the package version!
--
-- @since 2.14.7.0
instance ToJSON ConstraintNameDB

-- | @since 2.12.0.0
instance DatabaseName ConstraintNameDB where
Expand All @@ -69,4 +132,16 @@ instance DatabaseName ConstraintNameDB where
--
-- @since 2.12.0.0
newtype ConstraintNameHS = ConstraintNameHS { unConstraintNameHS :: Text }
deriving (Show, Eq, Read, Ord, Lift)
deriving (Show, Eq, Read, Ord, Lift, Generic)

-- | Note: JSON representations not considered part of the public API; breaking
-- changes will not be reflected in the package version!
--
-- @since 2.14.7.0
instance FromJSON ConstraintNameHS

-- | Note: JSON representations not considered part of the public API; breaking
-- changes will not be reflected in the package version!
--
-- @since 2.14.7.0
instance ToJSON ConstraintNameHS
Loading