Skip to content

Commit e404cf9

Browse files
authored
Merge pull request #16 from kRITZCREEK/compiler/0.12
Updates for 0.12
2 parents 723e0dd + 0f5818c commit e404cf9

18 files changed

+176
-150
lines changed

bower.json

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,16 @@
1616
"package.json"
1717
],
1818
"dependencies": {
19-
"purescript-argonaut-core": "^3.1.1",
20-
"purescript-codec": "^2.1.0",
21-
"purescript-generics-rep": "^5.4.0",
22-
"purescript-variant": "^4.0.0",
23-
"purescript-typelevel-prelude": "^2.6.0",
24-
"purescript-type-equality": "^2.1.0"
19+
"purescript-argonaut-core": "^4.0.0",
20+
"purescript-codec": "v3.0.0",
21+
"purescript-generics-rep": "^6.0.0",
22+
"purescript-variant": "^5.0.0",
23+
"purescript-ordered-collections": "^1.0.0",
24+
"purescript-typelevel-prelude": "^3.0.0",
25+
"purescript-type-equality": "^3.0.0"
2526
},
2627
"devDependencies": {
27-
"purescript-argonaut-codecs": "^3.2.0",
28-
"purescript-quickcheck": "^4.6.1"
28+
"purescript-argonaut-codecs": "^4.0.0",
29+
"purescript-quickcheck": "^5.0.0"
2930
}
3031
}

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
"test": "pulp test"
77
},
88
"devDependencies": {
9-
"pulp": "^11.0.0",
10-
"purescript": "^0.11.5",
11-
"purescript-psa": "^0.5.1",
12-
"rimraf": "^2.6.1"
9+
"pulp": "^12.0.0",
10+
"purescript": "^0.12.0",
11+
"purescript-psa": "^0.6.0",
12+
"rimraf": "^2.6.2"
1313
}
1414
}

src/Data/Codec/Argonaut.purs

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,19 @@ import Data.Codec (BasicCodec, Codec, GCodec(..), basicCodec, bihoistGCodec, dec
3737
import Data.Codec (decode, encode, (~), (<~<)) as Exports
3838
import Data.Either (Either(..), note)
3939
import Data.Generic.Rep (class Generic)
40-
import Data.Generic.Rep.Show (genericShow)
4140
import Data.Int as I
4241
import Data.List ((:))
4342
import Data.List as L
4443
import Data.Maybe (Maybe(..), maybe, fromJust)
4544
import Data.Profunctor.Star (Star(..))
46-
import Data.StrMap as SM
4745
import Data.String as S
46+
import Data.String.CodeUnits as SCU
4847
import Data.Symbol (class IsSymbol, SProxy, reflectSymbol)
4948
import Data.Traversable (traverse)
5049
import Data.Tuple (Tuple(..))
50+
import Foreign.Object as FO
5151
import Partial.Unsafe (unsafePartial)
52+
import Prim.Row as Row
5253
import Unsafe.Coerce (unsafeCoerce)
5354

5455
-- | Codec type for `Json` values.
@@ -68,7 +69,13 @@ derive instance ordJsonDecodeError ∷ Ord JsonDecodeError
6869
derive instance genericJsonDecodeErrorGeneric JsonDecodeError _
6970

7071
instance showJsonDecodeErrorShow JsonDecodeError where
71-
show err = genericShow err
72+
show = case _ of
73+
TypeMismatch s -> "(TypeMismatch " <> show s <> ")"
74+
UnexpectedValue j -> "(UnexpectedValue " <> J.stringify j <> ")"
75+
AtIndex i e -> "(AtIndex " <> show i <> " " <> show e <> ")"
76+
AtKey k e -> "(AtKey " <> show k <> " " <> show e <> ")"
77+
Named s e -> "(Named " <> show s <> " " <> show e <> ")"
78+
MissingValue -> "MissingValue"
7279

7380
-- | Prints a `JsonDecodeError` as a somewhat readable error message.
7481
printJsonDecodeError JsonDecodeError String
@@ -85,11 +92,11 @@ printJsonDecodeError err =
8592

8693
-- | The "identity codec" for `Json` values.
8794
json JsonCodec J.Json
88-
json = basicCodec pure id
95+
json = basicCodec pure identity
8996

9097
-- | A codec for `null` values in `Json`.
91-
null JsonCodec J.JNull
92-
null = jsonPrimCodec "Null" J.toNull J.fromNull
98+
null JsonCodec Unit
99+
null = jsonPrimCodec "Null" J.toNull (const J.jsonNull)
93100

94101
-- | A codec for `Boolean` values in `Json`.
95102
boolean JsonCodec Boolean
@@ -107,22 +114,26 @@ int = jsonPrimCodec "Int" (I.fromNumber <=< J.toNumber) (J.fromNumber <<< I.toNu
107114
string JsonCodec String
108115
string = jsonPrimCodec "String" J.toString J.fromString
109116

117+
-- | A codec for `Codepoint` values in `Json`.
118+
codePoint JsonCodec S.CodePoint
119+
codePoint = jsonPrimCodec "CodePoint" (S.codePointAt 0 <=< J.toString) (J.fromString <<< S.singleton)
120+
110121
-- | A codec for `Char` values in `Json`.
111122
char JsonCodec Char
112-
char = jsonPrimCodec "Char" (S.toChar <=< J.toString) (J.fromString <<< S.singleton)
123+
char = jsonPrimCodec "Char" (SCU.toChar <=< J.toString) (J.fromString <<< SCU.singleton)
113124

114125
-- | A codec for `Void` values.
115126
void JsonCodec Void
116127
void = jsonPrimCodec "Void" (const Nothing) absurd
117128

118-
-- | A codec for a `JArray` values in `Json`. This does not decode the values
129+
-- | A codec for `Array Json` values in `Json`. This does not decode the values
119130
-- | of the array, for that use `array` for a general array decoder, or
120131
-- | `indexedArray` with `index` to decode fixed length array encodings.
121-
jarray JsonCodec J.JArray
132+
jarray JsonCodec (Array J.Json)
122133
jarray = jsonPrimCodec "Array" J.toArray J.fromArray
123134

124135
-- | A codec for `JObject` values in `Json`.
125-
jobject JsonCodec J.JObject
136+
jobject JsonCodec (FO.Object J.Json)
126137
jobject = jsonPrimCodec "Object" J.toObject J.fromObject
127138

128139
-- | A codec for `Array` values.
@@ -144,7 +155,7 @@ array codec = GCodec dec enc
144155
type JIndexedCodec a =
145156
Codec
146157
(Either JsonDecodeError)
147-
J.JArray
158+
(Array J.Json)
148159
(L.List J.Json)
149160
a a
150161

@@ -181,27 +192,27 @@ index ix codec = GCodec dec enc
181192
type JPropCodec a =
182193
Codec
183194
(Either JsonDecodeError)
184-
J.JObject
185-
(L.List J.JAssoc)
195+
(FO.Object J.Json)
196+
(L.List (Tuple String J.Json))
186197
a a
187198

188199
-- | A codec for objects that are encoded with specific properties.
189200
object a. String JPropCodec a JsonCodec a
190201
object name =
191202
bihoistGCodec
192203
(\r → ReaderT (BF.lmap (Named name) <<< runReaderT r <=< decode jobject))
193-
(mapWriter (BF.rmap (J.fromObject <<< SM.fromFoldable)))
204+
(mapWriter (BF.rmap (J.fromObject <<< FO.fromFoldable)))
194205

195206
-- | A codec for a property of an object.
196207
prop a. String JsonCodec a JPropCodec a
197208
prop key codec = GCodec dec enc
198209
where
199-
dec ReaderT J.JObject (Either JsonDecodeError) a
210+
dec ReaderT (FO.Object J.Json) (Either JsonDecodeError) a
200211
dec = ReaderT \obj →
201-
BF.lmap (AtKey key) case SM.lookup key obj of
212+
BF.lmap (AtKey key) case FO.lookup key obj of
202213
Just val → decode codec val
203214
NothingLeft MissingValue
204-
enc Star (Writer (L.List J.JAssoc)) a a
215+
enc Star (Writer (L.List (Tuple String J.Json))) a a
205216
enc = Star \val → writer $ Tuple val (pure (Tuple key (encode codec val)))
206217

207218
-- | The starting value for a object-record codec. Used with `recordProp` it
@@ -223,32 +234,32 @@ record = GCodec (pure {}) (Star \val → writer (Tuple val L.Nil))
223234
recordProp
224235
p a r r'
225236
. IsSymbol p
226-
RowCons p a r r'
237+
Row.Cons p a r r'
227238
SProxy p
228239
JsonCodec a
229240
JPropCodec (Record r)
230241
JPropCodec (Record r')
231242
recordProp p codecA codecR =
232243
let key = reflectSymbol p in GCodec (dec' key) (enc' key)
233244
where
234-
dec' String ReaderT J.JObject (Either JsonDecodeError) (Record r')
245+
dec' String ReaderT (FO.Object J.Json) (Either JsonDecodeError) (Record r')
235246
dec' key = ReaderT \obj → do
236247
r ← decode codecR obj
237-
a ← BF.lmap (AtKey key) case SM.lookup key obj of
248+
a ← BF.lmap (AtKey key) case FO.lookup key obj of
238249
Just val → decode codecA val
239250
NothingLeft MissingValue
240251
pure $ unsafeSet key a r
241-
enc' String Star (Writer (L.List J.JAssoc)) (Record r') (Record r')
252+
enc' String Star (Writer (L.List (Tuple String J.Json))) (Record r') (Record r')
242253
enc' key = Star \val →
243254
writer $ Tuple val
244255
$ Tuple key (encode codecA (unsafeGet key val))
245256
: encode codecR (unsafeForget val)
246257
unsafeForget Record r' Record r
247258
unsafeForget = unsafeCoerce
248259
unsafeSet String a Record r Record r'
249-
unsafeSet key a = unsafeCoerce <<< SM.insert key a <<< unsafeCoerce
260+
unsafeSet key a = unsafeCoerce <<< FO.insert key a <<< unsafeCoerce
250261
unsafeGet String Record r' a
251-
unsafeGet s = unsafePartial fromJust <<< SM.lookup s <<< unsafeCoerce
262+
unsafeGet s = unsafePartial fromJust <<< FO.lookup s <<< unsafeCoerce
252263

253264
jsonPrimCodec
254265
a

src/Data/Codec/Argonaut/Common.purs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import Data.List as L
1414
import Data.Map as M
1515
import Data.Maybe (Maybe(..))
1616
import Data.Profunctor (dimap)
17-
import Data.StrMap as SM
17+
import Foreign.Object as FO
1818
import Data.Tuple (Tuple(..), fst, snd)
1919

2020
-- | A codec for `Maybe` values.
@@ -80,5 +80,5 @@ map codecA = dimap M.toUnfoldable M.fromFoldable <<< array <<< tuple codecA
8080
-- | A codec for `StrMap` values.
8181
-- |
8282
-- | Encodes as an array of two-element key/value arrays in JSON.
83-
strMap a. JsonCodec a JsonCodec (SM.StrMap a)
84-
strMap = dimap SM.toUnfoldable SM.fromFoldable <<< array <<< tuple string
83+
foreignObject a. JsonCodec a JsonCodec (FO.Object a)
84+
foreignObject = dimap FO.toUnfoldable FO.fromFoldable <<< array <<< tuple string

src/Data/Codec/Argonaut/Compat.purs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import Data.Codec.Argonaut.Common (either, list, map, tuple) as Common
1515
import Data.Either (Either)
1616
import Data.Functor as F
1717
import Data.Maybe (Maybe(..))
18-
import Data.StrMap as SM
18+
import Foreign.Object as FO
1919
import Data.Traversable (traverse)
2020
import Data.Tuple (Tuple(..))
2121

@@ -42,17 +42,17 @@ maybe codec = basicCodec dec enc
4242
-- | Encodes as a JSON object with the keys as properties.
4343
-- |
4444
-- | ```purescript
45-
-- | encode (strMap int) (Data.StrMap.fromFoldable [Tuple "a" 1, Tuple "b" 2]) == "{ \"a\": 1, \"b\": 2}"
45+
-- | encode (foreignObject int) (Foreign.Object.fromFoldable [Tuple "a" 1, Tuple "b" 2]) == "{ \"a\": 1, \"b\": 2}"
4646
-- | ```
47-
strMap a. JsonCodec a JsonCodec (SM.StrMap a)
48-
strMap codec =
47+
foreignObject a. JsonCodec a JsonCodec (FO.Object a)
48+
foreignObject codec =
4949
mapCodec
50-
(BF.lmap (Named "StrMap") <<< F.map fromArray <<< traverse decodeItem <<< SM.toUnfoldable)
50+
(BF.lmap (Named "StrMap") <<< F.map fromArray <<< traverse decodeItem <<< FO.toUnfoldable)
5151
(F.map (encode codec))
5252
jobject
5353
where
54-
fromArray v. Array (Tuple String v) SM.StrMap v
55-
fromArray = SM.fromFoldable
54+
fromArray v. Array (Tuple String v) FO.Object v
55+
fromArray = FO.fromFoldable
5656
decodeItem Tuple String J.Json Either JsonDecodeError (Tuple String a)
5757
decodeItem (Tuple key value) =
5858
BF.bimap (AtKey key) (Tuple key) (decode codec value)

src/Data/Codec/Argonaut/Migration.purs

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import Data.Argonaut.Core as J
1212
import Data.Codec (basicCodec)
1313
import Data.Codec.Argonaut (JsonCodec)
1414
import Data.Maybe (Maybe(..), maybe, fromMaybe)
15-
import Data.StrMap as SM
16-
import Data.StrMap.ST as SMST
15+
import Foreign.Object as FO
16+
import Foreign.Object.ST as FOST
1717
import Data.Tuple (Tuple(..), uncurry)
1818

1919
-- | When dealing with a JSON object that may be missing a field, this codec
@@ -37,12 +37,12 @@ addDefaultOrUpdateField field = alterField field <<< map Just
3737
-- | codec can be used to alter the JSON before parsing to ensure the new field
3838
-- | name is used instead
3939
renameField String String JsonCodec J.Json
40-
renameField oldName newName = basicCodec (pure <<< dec) id
40+
renameField oldName newName = basicCodec (pure <<< dec) identity
4141
where
4242
dec J.Json J.Json
43-
dec j = J.foldJsonObject j (J.fromObject <<< rename) j
44-
rename J.JObject J.JObject
45-
rename obj = maybe obj (uncurry (SM.insert newName)) (SM.pop oldName obj)
43+
dec j = J.caseJsonObject j (J.fromObject <<< rename) j
44+
rename FO.Object J.Json FO.Object J.Json
45+
rename obj = maybe obj (uncurry (FO.insert newName)) (FO.pop oldName obj)
4646

4747
-- | Prepares an object from a legacy codec for use in a `Variant` or
4848
-- | `taggedSum` codec.
@@ -65,29 +65,29 @@ renameField oldName newName = basicCodec (pure <<< dec) id
6565
-- | If the tag field is missing from the input, it will also be missing in the
6666
-- | output.
6767
nestForTagged JsonCodec J.Json
68-
nestForTagged = basicCodec (pure <<< dec) id
68+
nestForTagged = basicCodec (pure <<< dec) identity
6969
where
7070
dec J.Json J.Json
71-
dec j = J.foldJsonObject j (J.fromObject <<< rewrite) j
72-
rewrite J.JObject J.JObject
71+
dec j = J.caseJsonObject j (J.fromObject <<< rewrite) j
72+
rewrite FO.Object J.Json FO.Object J.Json
7373
rewrite obj =
74-
case SM.pop "tag" obj of
75-
NothingSM.pureST do
76-
result ← SMST.new
77-
SMST.poke result "value" (mkValue obj)
78-
Just (Tuple tagValue obj') → SM.pureST do
79-
result ← SMST.new
80-
_ ← SMST.poke result "tag" tagValue
81-
SMST.poke result "value" (mkValue obj')
82-
mkValue J.JObject J.Json
83-
mkValue obj = case SM.pop "value" obj of
84-
Just (Tuple valueValue obj') | SM.isEmpty obj' → valueValue
74+
case FO.pop "tag" obj of
75+
NothingFO.runST do
76+
result ← FOST.new
77+
FOST.poke "value" (mkValue obj) result
78+
Just (Tuple tagValue obj') → FO.runST do
79+
result ← FOST.new
80+
_ ← FOST.poke "tag" tagValue result
81+
FOST.poke "value" (mkValue obj') result
82+
mkValue FO.Object J.Json J.Json
83+
mkValue obj = case FO.pop "value" obj of
84+
Just (Tuple valueValue obj') | FO.isEmpty obj' → valueValue
8585
_ → J.fromObject obj
8686

8787
alterField String (Maybe J.Json Maybe J.Json) JsonCodec J.Json
88-
alterField field f = basicCodec (pure <<< dec) id
88+
alterField field f = basicCodec (pure <<< dec) identity
8989
where
9090
dec J.Json J.Json
91-
dec j = J.foldJsonObject j (J.fromObject <<< setDefault) j
92-
setDefault J.JObject J.JObject
93-
setDefault = SM.alter f field
91+
dec j = J.caseJsonObject j (J.fromObject <<< setDefault) j
92+
setDefault FO.Object J.Json FO.Object J.Json
93+
setDefault = FO.alter f field

src/Data/Codec/Argonaut/Record.purs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ module Data.Codec.Argonaut.Record
55
) where
66

77
import Data.Codec.Argonaut as CA
8-
import Data.Record as Rec
98
import Data.Symbol (class IsSymbol, SProxy(..))
9+
import Prim.Row as Row
10+
import Record as Rec
1011
import Type.Equality as TE
1112
import Type.Row as R
1213
import Unsafe.Coerce (unsafeCoerce)
@@ -19,8 +20,8 @@ instance rowListCodecNil ∷ RowListCodec R.Nil () () where
1920

2021
instance rowListCodecCons
2122
( RowListCodec rs ri' ro'
22-
, RowCons sym (CA.JsonCodec a) ri' ri
23-
, RowCons sym a ro' ro
23+
, Row.Cons sym (CA.JsonCodec a) ri' ri
24+
, Row.Cons sym a ro' ro
2425
, IsSymbol sym
2526
, TE.TypeEquals co (CA.JsonCodec a)
2627
) RowListCodec (R.Cons sym co rs) ri ro where

src/Data/Codec/Argonaut/Sum.purs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ import Data.Codec.Argonaut (JsonCodec, JsonDecodeError(..), jobject, json, prop,
1414
import Data.Either (Either(..))
1515
import Data.Maybe (Maybe(..), maybe)
1616
import Data.Profunctor.Star (Star(..))
17-
import Data.StrMap as SM
18-
import Data.StrMap.ST as SMST
17+
import Foreign.Object as FO
18+
import Foreign.Object.ST as FOST
1919
import Data.Tuple (Tuple(..))
2020

2121
-- | A helper for defining JSON codecs for "enum" sum types, where every
@@ -74,7 +74,7 @@ taggedSum name printTag parseTag f g = GCodec decodeCase encodeCase
7474
encodeCase = Star case _ of
7575
a | Tuple tag value ← g a →
7676
writer $ Tuple a $ encode jobject $
77-
SM.pureST do
78-
obj ← SMST.new
79-
_ ← SMST.poke obj "tag" (encode string (printTag tag))
80-
maybe (pure obj) (SMST.poke obj "value") value
77+
FO.runST do
78+
obj ← FOST.new
79+
_ ← FOST.poke "tag" (encode string (printTag tag)) obj
80+
maybe (pure obj) (\v -> FOST.poke "value" v obj) value

0 commit comments

Comments
 (0)