-
Notifications
You must be signed in to change notification settings - Fork 1
/
Scalar.hs
76 lines (67 loc) · 2.52 KB
/
Scalar.hs
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
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
---------------------------------------------------------
--
-- Module : Data.Object.Scalar
-- Copyright : Michael Snoyman
-- License : BSD3
--
-- Maintainer : Michael Snoyman <michael@snoyman.com>
-- Stability : Stable
-- Portability : portable
---------------------------------------------------------
module Data.Object.Scalar
( Scalar (..)
, ScalarObject
, toScalarObject
, fromScalarObject
, module Data.Object.Base
) where
import Data.ByteString.Lazy (ByteString, empty)
import Data.Time.Clock (UTCTime)
import Data.Object.Text
import Data.Object.Base
import System.Locale (defaultTimeLocale)
import Data.Time.Format (formatTime)
import Data.Attempt
import Data.Convertible.Text
data Scalar = Numeric Rational
| Text Text
| Binary ByteString
| Bool Bool
| Timestamp UTCTime
| Null
type ScalarObject = Object String Scalar
instance ConvertAttempt Scalar Text where
convertAttempt = return . convertSuccess
instance ConvertSuccess Scalar Text where
convertSuccess (Numeric n) = convertSuccess $ show n
convertSuccess (Text t) = t
convertSuccess (Binary b) = convertSuccess b
convertSuccess (Bool True) = convertSuccess "true"
convertSuccess (Bool False) = convertSuccess "false"
-- this is W3 format for timestamps.
convertSuccess (Timestamp t) =
convertSuccess $ formatTime defaultTimeLocale "%FT%XZ" t
convertSuccess Null = convertSuccess empty
instance ConvertAttempt Text Scalar where
convertAttempt = return . convertSuccess
instance ConvertSuccess Text Scalar where
convertSuccess = Text -- FIXME this should be more intelligent
instance ToObject (Object String Scalar) Text Text where
toObject = mapKeysValues convertSuccess convertSuccess
instance FromObject (Object String Scalar) Text Text where
fromObject = return . toObject
instance ToObject (Object Text Text) [Char] Scalar where
toObject = mapKeysValues convertSuccess convertSuccess
instance FromObject (Object Text Text) [Char] Scalar where
fromObject = return . toObject
-- | 'toObject' specialized for 'ScalarObject's
toScalarObject :: ToObject a String Scalar => a -> ScalarObject
toScalarObject = toObject
-- | 'fromObject' specialized for 'ScalarObject's
fromScalarObject :: FromObject a String Scalar
=> ScalarObject
-> Attempt a
fromScalarObject = fromObject