/
ReadWrite.purs
304 lines (250 loc) · 9.36 KB
/
ReadWrite.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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
module Foreign.ReadWrite
( class ReadForeign
, readForeign
, class WriteForeign
, writeForeign
, IncompleteRecord(..)
, class ReadForeignRecord
, readForeignRecordImpl
, readForeignRecord
, class WriteForeignRecord
, writeForeignRecordImpl
, writeForeignRecord
, undefined
) where
import Prelude
import Control.Monad.Except (catchError)
import Data.FoldableWithIndex (forWithIndex_)
import Data.Generic.Rep (class Generic)
import Data.Identity (Identity(..))
import Data.List.NonEmpty (head)
import Data.Maybe (Maybe(..), maybe)
import Data.Newtype (class Newtype)
import Data.Show.Generic (genericShow)
import Data.Symbol (class IsSymbol, reflectSymbol)
import Data.TraversableWithIndex (traverseWithIndex)
import Foreign (FT, Foreign, ForeignError(..), fail, isUndefined, readArray, readBoolean, readChar, readInt, readNumber, readString, tagOf, unsafeFromForeign, unsafeToForeign)
import Foreign.Object (Object, insert, lookup, runST, thawST)
import Foreign.Object as Object
import Foreign.Object.ST (new, peek, poke)
import Prim.Row as R
import Prim.RowList (RowList)
import Prim.RowList as RL
import Record as Record
import Record.Builder (Builder)
import Record.Builder as Builder
import Record.Unsafe (unsafeGet)
import Safe.Coerce (coerce)
import Type.Proxy (Proxy(..))
import Unsafe.Coerce (unsafeCoerce)
-- | Test whether a foreign value is an object
isObject ∷ Foreign → Boolean
isObject v = tagOf v == "Object"
-- | Attempt to coerce a foreign value to an `Object`.
readObject ∷ ∀ m. Monad m ⇒ Foreign → FT m (Object Foreign)
readObject value
| isObject value = pure $ unsafeFromForeign value
| otherwise = fail $ TypeMismatch "Object" (tagOf value)
foreign import undefined_ ∷ Foreign
undefined ∷ Foreign
undefined = undefined_
class ReadForeign a where
readForeign ∷ ∀ m. Monad m ⇒ Foreign → FT m a
class WriteForeign a where
writeForeign ∷ a → Foreign
instance ReadForeign Foreign where
readForeign = pure
instance WriteForeign Foreign where
writeForeign = identity
instance ReadForeign Void where
readForeign _ = fail (ForeignError "Attempted to read Void from Foreign")
instance WriteForeign Void where
writeForeign = absurd
-- | Matches `undefined`
instance ReadForeign Unit where
readForeign a =
if isUndefined a then pure unit
else fail (TypeMismatch "undefined" (tagOf a))
-- | Writes `undefined`
instance WriteForeign Unit where
writeForeign _ = undefined
instance ReadForeign String where
readForeign = readString
instance WriteForeign String where
writeForeign = unsafeToForeign ∷ String → Foreign
instance ReadForeign Char where
readForeign = readChar
instance WriteForeign Char where
writeForeign = unsafeToForeign ∷ Char → Foreign
instance ReadForeign Boolean where
readForeign = readBoolean
instance WriteForeign Boolean where
writeForeign = unsafeToForeign ∷ Boolean → Foreign
instance ReadForeign Number where
readForeign = readNumber
instance WriteForeign Number where
writeForeign = unsafeToForeign ∷ Number → Foreign
instance ReadForeign Int where
readForeign = readInt
instance WriteForeign Int where
writeForeign = unsafeToForeign ∷ Int → Foreign
instance ReadForeign a ⇒ ReadForeign (Identity a) where
readForeign = readForeign >>> map (coerce ∷ a → Identity a)
instance WriteForeign a ⇒ WriteForeign (Identity a) where
writeForeign = (coerce ∷ Identity a → a) >>> writeForeign
instance ReadForeign a ⇒ ReadForeign (Array a) where
readForeign = readArray >=> traverseWithIndex \i value → readForeign value
`catchError` (head >>> ErrorAtIndex i >>> fail)
instance WriteForeign a ⇒ WriteForeign (Array a) where
writeForeign = map writeForeign >>>
(unsafeToForeign ∷ Array Foreign → Foreign)
instance ReadForeign a ⇒ ReadForeign (Object a) where
readForeign = readObject >=> traverseWithIndex \k value → readForeign value
`catchError` (head >>> ErrorAtProperty k >>> fail)
instance WriteForeign a ⇒ WriteForeign (Object a) where
writeForeign = map writeForeign >>>
(unsafeToForeign ∷ Object Foreign → Foreign)
instance ReadForeign a ⇒ ReadForeign (Maybe a) where
readForeign value =
if isUndefined value then pure Nothing else Just <$> readForeign value
instance WriteForeign a ⇒ WriteForeign (Maybe a) where
writeForeign = maybe undefined writeForeign
-- | If any extra keys exist in the foreign object being read, it will result in
-- | an error. Preserves key order.
instance
( RL.RowToList row rl
, ReadForeignRecord rl row
) ⇒
ReadForeign (Record row) where
readForeign = readForeignRecord (Proxy ∷ Proxy rl) true
-- | Preserves key order.
instance
( RL.RowToList row rl
, WriteForeignRecord rl row
) ⇒
WriteForeign (Record row) where
writeForeign = writeForeignRecord (Proxy ∷ Proxy rl)
newtype IncompleteRecord r = IncompleteRecord r
derive instance Newtype (IncompleteRecord r) _
derive instance Generic (IncompleteRecord r) _
instance Show r ⇒ Show (IncompleteRecord r) where
show = genericShow
-- | Does not error if extra keys exist in the foreign object being read.
-- | Preserves key order.
instance
( RL.RowToList row rl
, ReadForeignRecord rl row
) ⇒
ReadForeign (IncompleteRecord (Record row)) where
readForeign = map IncompleteRecord <<<
readForeignRecord (Proxy ∷ Proxy rl) false
class ReadForeignRecord
∷ RowList Type → Row Type → Constraint
class ReadForeignRecord rl row | rl → row where
readForeignRecordImpl
∷ ∀ proxy m
. Monad m
⇒ proxy rl
→ Boolean
→ Object Foreign
→ FT m (Builder {} { | row })
class WriteForeignRecord
∷ RowList Type → Row Type → Constraint
class WriteForeignRecord rl row | rl → row where
writeForeignRecordImpl ∷ ∀ proxy. proxy rl → Record row → Object Foreign
instance readForeignRecordNil ∷ ReadForeignRecord RL.Nil () where
readForeignRecordImpl _ errorOnExtraKeys object = do
when errorOnExtraKeys $ forWithIndex_ object \k v → fail
(ErrorAtProperty k (TypeMismatch "undefined" (tagOf v)))
pure (identity ∷ Builder _ _)
instance writeForeignRecordNil ∷ WriteForeignRecord RL.Nil () where
writeForeignRecordImpl _ {} = Object.empty
instance readForeignRecordCons ∷
( IsSymbol key
, R.Cons key focus tail row
, R.Lacks key tail
, ReadForeignRecord rlTail tail
, ReadForeign focus
) ⇒
ReadForeignRecord (RL.Cons key focus rlTail) row where
readForeignRecordImpl _ errorOnExtraKeys object = do
let focusForeign = lookup key object # maybe undefined identity
focus ← readForeign focusForeign `catchError`
(head >>> ErrorAtProperty key >>> fail)
tail ←
readForeignRecordImpl rlTailP errorOnExtraKeys (Object.delete key object)
pure $ Builder.insert keyP focus <<< tail
where
key = reflectSymbol keyP
keyP = Proxy ∷ Proxy key
rlTailP = Proxy ∷ Proxy rlTail
instance writeForeignRecordCons ∷
( IsSymbol key
, R.Cons key focus tail row
, R.Lacks key tail
, WriteForeignRecord rlTail tail
, WriteForeign focus
) ⇒
WriteForeignRecord (RL.Cons key focus rlTail) row where
writeForeignRecordImpl _ record = do
let
focusForeign = writeForeign (Record.get keyP record)
tail = writeForeignRecordImpl rlTailP (Record.delete keyP record)
insert key focusForeign tail
where
key = reflectSymbol keyP
keyP = Proxy ∷ Proxy key
rlTailP = Proxy ∷ Proxy rlTail
readForeignRecord
∷ ∀ proxy rl row m
. Monad m
⇒ RL.RowToList row rl
⇒ ReadForeignRecord rl row
⇒ proxy rl
→ Boolean
→ Foreign
→ FT m (Record row)
readForeignRecord _ errorOnExtraKeys value = do
valueObject ← readObject value
values ← Builder.buildFromScratch <$>
readForeignRecordImpl (Proxy ∷ _ rl) errorOnExtraKeys valueObject
-- The reason we don't just return `values` is that it's alphabetical, so we
-- piggyback off of the order of `valueObject` to retain the original order,
-- while adding any keys that should be present in the final record, but were
-- missing in the foreign object (for example, the `Maybe` instance for
-- `ReadWriteForeign` allows its field/key to be missing).
-- This ranks high among the dirtiest stuff I've done in PureScript. Sorry.
let
allCorrectKeysAsObject ∷ Object Foreign
allCorrectKeysAsObject = unsafeCoerce values
allKeysInCorrectOrder ∷ Object Foreign
allKeysInCorrectOrder = runST do
obj ← thawST valueObject
forWithIndex_ allCorrectKeysAsObject \k v → peek k obj >>= case _ of
Just _ → poke k (unsafeGet k values) obj
Nothing → poke k v obj
pure obj
finalRecord ∷ Record row
finalRecord = unsafeCoerce allKeysInCorrectOrder
pure finalRecord
writeForeignRecord
∷ ∀ proxy rl row
. WriteForeignRecord rl row
⇒ proxy rl
→ Record row
→ Foreign
writeForeignRecord _ record = do
let
allCorrectKeysAsObject ∷ Object Foreign
allCorrectKeysAsObject = writeForeignRecordImpl (Proxy ∷ _ rl) record
allKeysInCorrectOrder ∷ Object Foreign
allKeysInCorrectOrder = unsafeCoerce record
finalObj ∷ Object Foreign
finalObj = runST do
obj ← new
forWithIndex_ allKeysInCorrectOrder \k _ →
case lookup k allCorrectKeysAsObject of
Just v → unless (isUndefined v) $ void (poke k v obj)
Nothing → pure unit
pure obj
(unsafeToForeign ∷ Object Foreign → Foreign) $ finalObj