File: TypeLitFieldDefsSpec.hs

package info (click to toggle)
haskell-persistent 2.14.6.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,120 kB
  • sloc: haskell: 12,767; makefile: 3
file content (59 lines) | stat: -rw-r--r-- 1,730 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE OverloadedLabels #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE UndecidableInstances #-}
{-# OPTIONS_GHC -Wno-unused-local-binds #-}

module Database.Persist.TH.TypeLitFieldDefsSpec where

import GHC.TypeLits
import TemplateTestImports

newtype Finite (n :: Nat) = Finite Int

instance PersistField (Finite n) where
    toPersistValue (Finite n) = toPersistValue n
    fromPersistValue = fmap Finite . fromPersistValue

instance PersistFieldSql (Finite n) where
    sqlType _ = sqlType (Proxy :: Proxy Int)

newtype Labelled (t :: Symbol) = Labelled Int

instance PersistField (Labelled n) where
    toPersistValue (Labelled n) = toPersistValue n
    fromPersistValue = fmap Labelled . fromPersistValue

instance PersistFieldSql (Labelled n) where
    sqlType _ = sqlType (Proxy :: Proxy Int)

mkPersist sqlSettings [persistLowerCase|
WithFinite
    one    (Finite 1)
    twenty (Finite 20)

WithLabelled
    one    (Labelled "one")
    twenty (Labelled "twenty")
|]

spec :: Spec
spec = describe "TypeLitFieldDefs" $ do
    it "should support numeric type literal fields in entity definition" $ do
        let mkFinite :: Finite 1 -> Finite 20 -> WithFinite
            mkFinite = WithFinite
        compiles

    it "should support string based type literal fields in entity definition" $ do
        let mkLabelled :: Labelled "one" -> Labelled "twenty" -> WithLabelled
            mkLabelled = WithLabelled
        compiles

compiles :: Expectation
compiles = True `shouldBe` True