-
Notifications
You must be signed in to change notification settings - Fork 0
/
Functions.hs
123 lines (105 loc) · 3.35 KB
/
Functions.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
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
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TypeApplications #-}
module Functions (test) where
import Control.Monad (guard)
import Data.Aeson.Types (Parser, Value, object, withObject, (.:), (.=))
import System.OsString
import System.OsString.Aeson
import Utils
data Mapping = Mapping
{ source :: OsString
, destination :: OsString
}
deriving (Eq)
example :: Mapping
example =
Mapping
{ source = exampleSource
, destination = exampleDestination
}
binaryFromJSON :: Value -> Parser Mapping
binaryFromJSON = withObject "Mapping" $ \obj -> do
source <- fromBinary =<< obj .: "source"
destination <- fromBinary =<< obj .: "destination"
pure Mapping{..}
binaryToJSON :: Mapping -> Value
binaryToJSON Mapping{..} =
object
[ "source" .= toBinary source
, "destination" .= toBinary destination
]
testBinary :: IO ()
testBinary = do
putStrLn "## Binary"
let
json = binaryToJSON example
printJSON json
mapping <- parseThrow binaryFromJSON json
guard $ mapping == example
textualFromJSON :: Value -> Parser Mapping
textualFromJSON = withObject "Mapping" $ \obj -> do
source <- fromTextual @Unicode =<< obj .: "source"
destination <- fromTextual @Unicode =<< obj .: "destination"
pure Mapping{..}
textualToJSON :: Mapping -> IO Value
textualToJSON Mapping{..} = do
source' <- toTextual @Unicode source
destination' <- toTextual @Unicode destination
pure . object $
[ "source" .= source'
, "destination" .= destination'
]
testTextual :: IO ()
testTextual = do
putStrLn "## Textual"
json <- textualToJSON example
printJSON json
mapping <- parseThrow textualFromJSON json
guard $ mapping == example
taggedBinaryFromJSON :: Value -> Parser Mapping
taggedBinaryFromJSON = withObject "Mapping" $ \obj -> do
source <- fromTagged fromBinaryAs =<< obj .: "source"
destination <- fromTagged fromBinaryAs =<< obj .: "destination"
pure Mapping{..}
taggedBinaryToJSON :: Mapping -> Value
taggedBinaryToJSON Mapping{..} =
object
[ "source" .= toTagged toBinaryAs source
, "destination" .= toTagged toBinaryAs destination
]
testTaggedBinary :: IO ()
testTaggedBinary = do
putStrLn "## Tagged Binary"
let
json = taggedBinaryToJSON example
printJSON json
mapping <- parseThrow taggedBinaryFromJSON json
guard $ mapping == example
taggedTextualFromJSON :: Value -> Parser Mapping
taggedTextualFromJSON = withObject "Mapping" $ \obj -> do
source <- fromTagged (fromTextualAs @Unicode) =<< obj .: "source"
destination <- fromTagged (fromTextualAs @Unicode) =<< obj .: "destination"
pure Mapping{..}
taggedTextualToJSON :: Mapping -> IO Value
taggedTextualToJSON Mapping{..} = do
source' <- toTaggedM (toTextualAs @Unicode) source
destination' <- toTaggedM (toTextualAs @Unicode) destination
pure . object $
[ "source" .= source'
, "destination" .= destination'
]
testTaggedTextual :: IO ()
testTaggedTextual = do
putStrLn "## Tagged Textual"
json <- taggedTextualToJSON example
printJSON json
mapping <- parseThrow taggedTextualFromJSON json
guard $ mapping == example
test :: IO ()
test = do
putStrLn "# Functions"
testBinary
testTextual
testTaggedBinary
testTaggedTextual