-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.purs
59 lines (52 loc) · 1.36 KB
/
Main.purs
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
module Main where
import Prelude
import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Console (CONSOLE, log)
import Data.Either (Either(..))
import Data.Foreign (ForeignError)
import Data.List.NonEmpty (NonEmptyList)
import Data.Record (delete, get, insert)
import Simple.JSON (readJSON)
import Type.Prelude (class IsSymbol, class RowLacks, SProxy(..))
rename :: forall prev next ty input inter output
. IsSymbol prev
=> IsSymbol next
=> RowCons prev ty inter input
=> RowLacks prev inter
=> RowCons next ty inter output
=> RowLacks next inter
=> SProxy prev
-> SProxy next
-> Record input
-> Record output
rename prev next record =
insert next value inter
where
value = get prev record
inter :: Record inter
inter = delete prev record
type MyThing =
{ "fieldA" :: String
, "fieldB" :: Int
}
decodeMyThingFromDirtyJSON :: String -> Either (NonEmptyList ForeignError) MyThing
decodeMyThingFromDirtyJSON s = do
parsed <- readJSON s
pure $ rename
(SProxy :: SProxy "MY_FIELD_A")
(SProxy :: SProxy "fieldA")
parsed
testJSON :: String
testJSON = """
{
"MY_FIELD_A": "asdf",
"fieldB": 123
}
"""
main :: forall e. Eff (console :: CONSOLE | e) Unit
main = do
case decodeMyThingFromDirtyJSON testJSON of
Right {fieldA} -> log $ "fieldA: " <> fieldA
Left e -> log $ "error: " <> show e
-- output:
-- fieldA: asdf