This repository was archived by the owner on Jan 9, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 116
Expand file tree
/
Copy pathCodec.hs
More file actions
141 lines (125 loc) · 4.33 KB
/
Codec.hs
File metadata and controls
141 lines (125 loc) · 4.33 KB
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
{-# LANGUAGE GADTs #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}
-- |
-- Module : Pact.Types.Persistence
-- Copyright : (C) 2019 Stuart Popejoy
-- License : BSD-style (see the file LICENSE)
-- Maintainer : Stuart Popejoy <stuart@kadena.io>
--
-- 'Codec' pairs 'ToJSON' and 'FromJSON' marshalling.
--
module Pact.Types.Codec
( jsIntegerBounds
, Codec(..)
, integerCodec
, decimalCodec
, timeCodec
, valueCodec
, pactISO8601Format
, highPrecFormat
, roundtripCodec
, withThisText
) where
import Control.Applicative
import qualified Data.Aeson as A
import Data.Aeson hiding (Object)
import Data.Aeson.Types (Parser,parse)
import Data.Text (Text,unpack)
import Pact.Time
import Data.Decimal (Decimal,DecimalRaw(..))
import Text.Read (readMaybe)
import Data.Ratio ((%), denominator)
-- | Min, max values that Javascript doesn't mess up.
--
-- http://blog.vjeux.com/2010/javascript/javascript-max_int-number-limits.html
-- "The integer part of the Number type in Javascript is safe in [-2^53 .. 2^53] (253 = 9 007 199 254 740 992).
-- Beyond this there will be precision loss on the least significant numbers."
jsIntegerBounds :: (Integer, Integer)
jsIntegerBounds = (-9007199254740991,9007199254740991)
isSafeInteger :: Integer -> Bool
isSafeInteger i = i >= l && i <= h
where (l,h) = jsIntegerBounds
-- | JSON codec pair.
data Codec a = Codec {
encoder :: a -> Value,
decoder :: Value -> Parser a
}
-- | Integers encode to an object that uses Number if in reasonable JS bounds or String otherwise.
integerCodec :: Codec Integer
integerCodec = Codec encodeInteger decodeInteger
where
encodeInteger i
| isSafeInteger i = object [ field .= i ]
| otherwise = object [ field .= show i ]
{-# INLINE encodeInteger #-}
decodeInteger = withObject "Integer" $ \o -> do
s <- o .: field
case s of
Number n -> return (round n)
String n -> case readMaybe (unpack n) of
Just i -> return i
Nothing -> fail $ "Invalid integer value: " ++ show s
_ -> fail $ "Invalid integer value: " ++ show s
{-# INLINE decodeInteger #-}
field = "int"
-- | Decimals encode to a Scientific, which is encoded as an object + String
-- if mantissa precision exceeds JS.
-- TODO fromRational . toRational may not be the speediest.
decimalCodec :: Codec Decimal
decimalCodec = Codec enc dec
where
enc d@(Decimal _places mantissa)
| isSafeInteger mantissa = Number $ fromRational $ toRational d
| otherwise = object [ field .= show d ]
{-# INLINE enc #-}
dec (Number n) = return $ fromRational $ toRational n
dec (A.Object o) = o .: field >>= \s -> case readMaybe (unpack s) of
Just d -> return d
Nothing -> fail $ "Invalid decimal value: " ++ show s
dec v = fail $ "Invalid decimal value: " ++ show v
{-# INLINE dec #-}
field = "decimal"
-- | default Pact ISO8601 format
pactISO8601Format :: String
pactISO8601Format = "%Y-%m-%dT%H:%M:%SZ"
-- | high-precision format
highPrecFormat :: String
highPrecFormat = "%Y-%m-%dT%H:%M:%S.%vZ"
-- | Time uses
timeCodec :: Codec UTCTime
timeCodec = Codec enc dec
where
enc t
| 1 == denom t = object [ field .= formatTime pactISO8601Format t ]
| otherwise = object [ highprec .= formatTime highPrecFormat t ]
where
denom :: UTCTime -> Integer
denom = denominator . (% 1000000) . fromIntegral . toPosixTimestampMicros
{-# INLINE enc #-}
dec = withObject "time" $ \o ->
(o .: field >>= mkTime pactISO8601Format) <|>
(o .: highprec >>= mkTime highPrecFormat)
where
mkTime :: String -> String -> Parser UTCTime
mkTime fmt v = case parseTime fmt v of
Just t -> return t
Nothing -> fail $ "Invalid time value, expected " ++ fmt
{-# INLINE dec #-}
field = "time"
highprec = "timep"
valueCodec :: Codec Value
valueCodec = Codec enc dec
where
enc v = object [field .= v]
{-# INLINE enc #-}
dec = withObject "Value" $ \o -> o .: field
{-# INLINE dec #-}
field = "_P_val"
roundtripCodec :: Codec t -> t -> Result t
roundtripCodec c t = parse (decoder c) $ encoder c t
withThisText :: String -> Text -> Value -> Parser a -> Parser a
withThisText s t v p = withText s go v
where
go tv | tv == t = p
| otherwise = fail $ s ++ ": Expected " ++ show t