File: Common.hs

package info (click to toggle)
haskell-aeson 2.2.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 9,076 kB
  • sloc: haskell: 13,153; makefile: 11
file content (214 lines) | stat: -rw-r--r-- 6,280 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE OverloadedStrings #-}

module UnitTests.OptionalFields.Common
  ( module UnitTests.OptionalFields.Common
  , module Data.Aeson
  , module Data.Aeson.Types
  , module Data.Aeson.TH
  , module Test.Tasty
  , module Test.Tasty.HUnit
  , module Data.Proxy
  ) where

import Data.Aeson
import Data.Aeson.Types
import Data.Aeson.TH
import Data.Maybe (isNothing)
import GHC.Generics (Generic, Generic1)
import Data.Proxy
import Test.Tasty
import Test.Tasty.HUnit
import qualified Data.Text as T

-------------------------------------------------------------------------------
-- Field types
-------------------------------------------------------------------------------

newtype NullableNonEmptyString = NullableNonEmptyString (Maybe String)
  deriving (Eq, Show)

defaultNullableNonEmptyString :: NullableNonEmptyString
defaultNullableNonEmptyString = NullableNonEmptyString Nothing

instance ToJSON NullableNonEmptyString where
  toJSON (NullableNonEmptyString x) = toJSON x
  toEncoding (NullableNonEmptyString x) = toEncoding x
  omitField (NullableNonEmptyString x) = isNothing x

instance FromJSON NullableNonEmptyString where
  parseJSON Null = pure defaultNullableNonEmptyString
  parseJSON (String x) = pure (nne $ T.unpack x)
  parseJSON _ = fail "NullableNonEmptyString.parseJSON: expected String or Null"

  omittedField = Just defaultNullableNonEmptyString



nne :: String -> NullableNonEmptyString
nne str = case filter (/= ' ') str of
  "" -> NullableNonEmptyString Nothing
  _ -> NullableNonEmptyString (Just str)

newtype Default = Default Int
  deriving (Eq, Show)

instance ToJSON Default where
    toJSON (Default i) = toJSON i
    toEncoding (Default i) = toEncoding i
    omitField (Default i) = i == 0

instance FromJSON Default where
    parseJSON = fmap Default . parseJSON
    omittedField = Just (Default 0)

-------------------------------------------------------------------------------
-- Records
-------------------------------------------------------------------------------

-- lax
data RecordA = RecordA
  { required :: String
  , optional :: NullableNonEmptyString
  , default_ :: Default
  }
  deriving (Eq, Show, Generic)

-- strict
data RecordB = RecordB
  { required :: String
  , optional :: NullableNonEmptyString
  , default_ :: Default
  }
  deriving (Eq, Show, Generic)

-- default
data RecordC = RecordC
  { required :: String
  , optional :: NullableNonEmptyString
  , default_ :: Default
  }
  deriving (Eq, Show, Generic)

data HRecordA a = HRecordA
  { required :: String
  , optional :: a
  , default_ :: Default
  }
  deriving (Eq, Show, Generic1)

data HRecordB a = HRecordB
  { required :: String
  , optional :: a
  , default_ :: Default
  }
  deriving (Eq, Show, Generic1)

data HRecordC a = HRecordC
  { required :: String
  , optional :: a
  , default_ :: Default
  }
  deriving (Eq, Show, Generic1)

type HRecordA' = HRecordA NullableNonEmptyString
type HRecordB' = HRecordB NullableNonEmptyString
type HRecordC' = HRecordC NullableNonEmptyString

-------------------------------------------------------------------------------
-- Options
-------------------------------------------------------------------------------

nonOmittingOptions :: Options
nonOmittingOptions = defaultOptions { omitNothingFields = False, allowOmittedFields = False }

omittingOptions :: Options
omittingOptions = defaultOptions { omitNothingFields = True, allowOmittedFields = True }

-------------------------------------------------------------------------------
-- Test utils
-------------------------------------------------------------------------------

encodeCase :: HasCallStack => ToJSON a => a -> Value -> IO ()
encodeCase record obj = do
  decode @Value (encode record)          @?= Just obj
  decode @Value (encode (toJSON record)) @?= Just obj

decodeCase :: forall a. HasCallStack => (FromJSON a, Eq a, Show a) => a -> Value -> IO ()
decodeCase record obj = do
  decode @a (encode obj) @?= Just record

counterCase :: forall a proxy. HasCallStack => (FromJSON a, ToJSON a, Show a) => proxy a -> Value -> IO ()
counterCase _ obj = case decode @a (encode obj) of
  Nothing -> return ()
  Just v  -> assertFailure $ "decode should fail, got: " ++ show v

-------------------------------------------------------------------------------
-- Test inputs
-------------------------------------------------------------------------------

helloWorldRecA :: RecordA
helloWorldRecA = RecordA "hello" (nne "world") (Default 42)

helloWorldRecB :: RecordB
helloWorldRecB = RecordB "hello" (nne "world") (Default 42)

helloWorldRecC :: RecordC
helloWorldRecC = RecordC "hello" (nne "world") (Default 42)

helloWorldHRecA :: HRecordA NullableNonEmptyString
helloWorldHRecA = HRecordA "hello" (nne "world") (Default 42)

helloWorldHRecB :: HRecordB NullableNonEmptyString
helloWorldHRecB = HRecordB "hello" (nne "world") (Default 42)

helloWorldHRecC :: HRecordC NullableNonEmptyString
helloWorldHRecC = HRecordC "hello" (nne "world") (Default 42)

helloWorldObj :: Value
helloWorldObj = object
  [ "required" .= String "hello"
  , "optional" .= String "world"
  , "default_" .= Number 42
  ]

helloRecA :: RecordA
helloRecA = RecordA "hello" defaultNullableNonEmptyString (Default 0)

helloRecB :: RecordB
helloRecB = RecordB "hello" defaultNullableNonEmptyString (Default 0)

helloRecC :: RecordC
helloRecC = RecordC "hello" defaultNullableNonEmptyString (Default 0)

helloHRecA :: HRecordA NullableNonEmptyString
helloHRecA = HRecordA "hello" defaultNullableNonEmptyString (Default 0)

helloHRecB :: HRecordB NullableNonEmptyString
helloHRecB = HRecordB "hello" defaultNullableNonEmptyString (Default 0)

helloHRecC :: HRecordC NullableNonEmptyString
helloHRecC = HRecordC "hello" defaultNullableNonEmptyString (Default 0)

helloObj :: Value
helloObj = object
  [ "required" .= String "hello"
  ]

helloNullObj :: Value
helloNullObj = object
  [ "required" .= String "hello"
  , "optional" .= Null
  , "default_" .= Number 0
  ]

helloNullObj2 :: Value
helloNullObj2 = object
  [ "required" .= String "hello"
  , "optional" .= Null
  , "default_" .= Null
  ]