Skip to content

Commit 3f6ec25

Browse files
authored
Merge pull request #35 from thomashoneyman/master
Update for PureScript 0.14
2 parents d4d67bb + c0b0e81 commit 3f6ec25

File tree

10 files changed

+77
-77
lines changed

10 files changed

+77
-77
lines changed

README.md

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ bower install purescript-codec-argonaut
2121

2222
As [`JsonCodec`](https://pursuit.purescript.org/packages/purescript-codec-argonaut/docs/Data.Codec.Argonaut#t:JsonCodec)s are values, they need to be fed into the [`encode`](https://pursuit.purescript.org/packages/purescript-codec/docs/Data.Codec/#v:encode) or [`decode`](https://pursuit.purescript.org/packages/purescript-codec/docs/Data.Codec/#v:decode) function provided by [`Data.Codec`](https://pursuit.purescript.org/packages/purescript-codec/docs/Data.Codec) (and re-exported by [`Data.Codec.Argonaut`](https://pursuit.purescript.org/packages/purescript-codec-argonaut/docs/Data.Codec.Argonaut)):
2323

24-
``` purescript
24+
```purescript
2525
import Data.Argonaut.Core as J
2626
import Data.Codec.Argonaut as CA
2727
import Data.Either (Either)
@@ -39,7 +39,7 @@ To parse a serialized `String` into a `J.Json` structure use the [`Parser.jsonPa
3939

4040
To /"stringify"/ (serialize) your `Array String` to a serialized JSON `String` we would use the [`stringify`](https://pursuit.purescript.org/packages/purescript-argonaut-core/5.1.0/docs/Data.Argonaut.Core#v:stringify) like so:
4141

42-
``` purescript
42+
```purescript
4343
import Control.Category ((>>>))
4444
4545
serialize :: Array String -> String
@@ -60,7 +60,7 @@ So far so boring. Things only start getting interesting or useful when we can bu
6060

6161
The simplest compound codec provided by the library is [`CA.array`](https://pursuit.purescript.org/packages/purescript-codec-argonaut/docs/Data.Codec.Argonaut#v:array), which accepts another codec, and encodes/decodes an arbitrary length array where all the items match the inner codec. For example:
6262

63-
``` purescript
63+
```purescript
6464
import Data.Codec.Argonaut as CA
6565
6666
codec ∷ CA.JsonCodec (Array String)
@@ -71,7 +71,7 @@ codec = CA.array CA.string
7171

7272
Probably the most useful compound codec is for `Record`, this will generally be the building block of most codecs. There are a few different ways to define these codecs, but the most convenient is the [`record`](https://pursuit.purescript.org/packages/purescript-codec-argonaut/docs/Data.Codec.Argonaut.Record#v:record) function provided by [`Data.Codec.Argonaut.Record`](https://pursuit.purescript.org/packages/purescript-codec-argonaut/docs/Data.Codec.Argonaut.Record):
7373

74-
``` purescript
74+
```purescript
7575
import Data.Codec.Argonaut as CA
7676
import Data.Codec.Argonaut.Record as CAR
7777
@@ -91,13 +91,13 @@ Note we also used a [`CA.object`](https://pursuit.purescript.org/packages/puresc
9191

9292
The codec will encode/decode JSON objects of the same shape as the defining record. For example:
9393

94-
``` json
94+
```json
9595
{ "name": "Rashida", "age": 37, "active": true }
9696
```
9797

9898
It's possible to encode/decode records that include properties with spaces and/or symbols in the name, or reserved names, by quoting the fields in the type and definition:
9999

100-
``` purescript
100+
```purescript
101101
type Person = { "Name" ∷ String, age ∷ Int, "is active" ∷ Boolean }
102102
103103
codec ∷ CA.JsonCodec Person
@@ -116,7 +116,7 @@ This library comes with codec support for [`purescript-variant`](https://github.
116116

117117
First of all, variants. Similar to the object/record case there are a few options for defining variant codecs, but most commonly they will be defined with [`variantMatch`](https://pursuit.purescript.org/packages/purescript-codec-argonaut/docs/Data.Codec.Argonaut.Variant#v:variantMatch) provided by [`Data.Codec.Argonaut.Variant`](https://pursuit.purescript.org/packages/purescript-codec-argonaut/docs/Data.Codec.Argonaut.Variant):
118118

119-
``` purescript
119+
```purescript
120120
import Prelude
121121
122122
import Data.Codec.Argonaut as CA
@@ -142,23 +142,23 @@ The fields in the record passed to [`CAV.variantMatch`](https://pursuit.purescri
142142

143143
The variant codec is a little opinionated since there's no exactly corresponding JSON structure for sums. The encoding looks something like:
144144

145-
``` json
145+
```json
146146
{ "tag": <constructorName>, "value": <value> }
147147
```
148148

149149
`value` will be omitted for nullary / `Left`-defined constructors. At the moment it is not possible to customise the encoding for variant types, so they may not be suitable if you are not in control of the serialization format.
150150

151151
Sum type encoding is usually handled by building a variant codec, and then using [`dimap`](https://pursuit.purescript.org/packages/purescript-profunctor/docs/Data.Profunctor#v:dimap) to inject into/project out of a corresponding sum type:
152152

153-
``` purescript
153+
```purescript
154154
import Prelude
155155
156156
import Data.Codec.Argonaut as CA
157157
import Data.Codec.Argonaut.Variant as CAV
158158
import Data.Either (Either(..))
159159
import Data.Profunctor (dimap)
160-
import Data.Symbol (SProxy(..))
161160
import Data.Variant as V
161+
import Type.Proxy (Proxy(..))
162162
163163
data SomeValue2 = Str String | Int Int | Neither
164164
@@ -171,17 +171,17 @@ codec =
171171
}
172172
where
173173
toVariant = case _ of
174-
Str s → V.inj (SProxy ∷ _ "str") s
175-
Int i → V.inj (SProxy ∷ _ "int") i
176-
Neither → V.inj (SProxy ∷ _ "neither") unit
174+
Str s → V.inj (Proxy ∷ _ "str") s
175+
Int i → V.inj (Proxy ∷ _ "int") i
176+
Neither → V.inj (Proxy ∷ _ "neither") unit
177177
fromVariant = V.match
178178
{ str: Str
179179
, int: Int
180180
, neither: \_ → Neither
181181
}
182182
```
183183

184-
This certainly is a little boilerplate-y, but at least when defining codecs this way you do gain the benefits of having a single definition that aligns the encoding and decoding behaviour. This means, assuming there are no mixups in `toVariant`/`fromVariant`, the guaranteed roundtripping is preserved. Often it's not even possible to have mixups during `dimap`, since the sum constructor types will all differ.
184+
This certainly is a little boilerplate-y, but at least when defining codecs this way you do gain the benefits of having a single definition that aligns the encoding and decoding behaviour. This means, assuming there are no mixups in `toVariant`/`fromVariant`, the guaranteed roundtripping is preserved. Often it's not even possible to have mixups during `dimap`, since the sum constructor types will all differ.
185185

186186
If you have a sum type that only consists of nullary constructors and it has a [`Generic`](https://pursuit.purescript.org/packages/purescript-generics-rep/docs/Data.Generic.Rep#t:Generic) instance defined, [`nullarySum`](https://pursuit.purescript.org/packages/purescript-codec-argonaut/docs/Data.Codec.Argonaut.Generic#v:nullarySum) provided by [`Data.Codec.Argonaut.Generic`](https://pursuit.purescript.org/packages/purescript-codec-argonaut/docs/Data.Codec.Argonaut.Generic) can generate a codec that will encode the constructors as string values matching the constructor names in the JSON.
187187

@@ -197,7 +197,7 @@ There is also a [`Data.Codec.Argonaut.Compat`](https://pursuit.purescript.org/pa
197197

198198
If you have a codec for a `newtype` with a [`Newtype`](https://pursuit.purescript.org/packages/purescript-newtype/docs/Data.Newtype#t:Newtype) instance, you can use the [`wrapIso`](https://pursuit.purescript.org/packages/purescript-profunctor/docs/Data.Profunctor#v:wrapIso) function from [`purescript-profunctor`](https://github.com/purescript/purescript-profunctor) to adapt a codec to work with the `newtype`. For example:
199199

200-
``` purescript
200+
```purescript
201201
import Data.Codec.Argonaut.Common as CA
202202
import Data.Codec.Argonaut.Record as CAR
203203
import Data.Newtype (class Newtype)
@@ -225,7 +225,7 @@ If you have a type with a pair of functions like the `preview` and `view` that m
225225

226226
For example, to adapt the [`CA.string`](https://pursuit.purescript.org/packages/purescript-codec-argonaut/docs/Data.Codec.Argonaut#v:string) codec to only work for `NonEmptyString`s:
227227

228-
``` purescript
228+
```purescript
229229
import Data.Codec.Argonaut as CA
230230
import Data.String.NonEmpty (NonEmptyString)
231231
import Data.String.NonEmpty as NES

bower.json

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"license": "MIT",
55
"repository": {
66
"type": "git",
7-
"url": "git://github.com/garyb/purescript-codec-argonaut.git"
7+
"url": "https://github.com/garyb/purescript-codec-argonaut.git"
88
},
99
"ignore": [
1010
"**/.*",
@@ -16,15 +16,14 @@
1616
"package.json"
1717
],
1818
"dependencies": {
19-
"purescript-argonaut-core": "^5.0.0",
20-
"purescript-codec": "v3.0.0",
21-
"purescript-generics-rep": "^6.0.0",
22-
"purescript-variant": "^6.0.0",
23-
"purescript-ordered-collections": "^1.0.0",
24-
"purescript-type-equality": "^3.0.0"
19+
"purescript-argonaut-core": "^6.0.0",
20+
"purescript-codec": "^4.0.0",
21+
"purescript-variant": "^7.0.1",
22+
"purescript-ordered-collections": "^2.0.0",
23+
"purescript-type-equality": "^4.0.0"
2524
},
2625
"devDependencies": {
27-
"purescript-argonaut-codecs": "^6.0.0",
28-
"purescript-quickcheck": "^6.1.0"
26+
"purescript-argonaut-codecs": "^8.0.0",
27+
"purescript-quickcheck": "^7.0.0"
2928
}
3029
}

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": "^13.0.0",
10-
"purescript": "^0.13.0",
11-
"purescript-psa": "^0.7.3",
12-
"rimraf": "^2.6.2"
9+
"pulp": "^15.0.0",
10+
"purescript": "^0.14.0",
11+
"purescript-psa": "^0.8.2",
12+
"rimraf": "^3.0.0"
1313
}
1414
}

src/Data/Codec/Argonaut.purs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ import Data.Maybe (Maybe(..), maybe, fromJust)
4545
import Data.Profunctor.Star (Star(..))
4646
import Data.String as S
4747
import Data.String.CodeUnits as SCU
48-
import Data.Symbol (class IsSymbol, SProxy, reflectSymbol)
48+
import Data.Symbol (class IsSymbol, reflectSymbol)
4949
import Data.Traversable (traverse)
5050
import Data.Tuple (Tuple(..))
5151
import Foreign.Object as FO
@@ -234,15 +234,15 @@ prop key codec = GCodec dec enc
234234
-- | `{ "name": "Karl", "age": 25 }` we would define a codec like this:
235235
-- | ```
236236
-- | import Data.Codec.Argonaut as CA
237-
-- | import Data.Symbol (SProxy(..))
237+
-- | import Type.Proxy (Proxy(..))
238238
-- |
239239
-- | type Person = { name ∷ String, age ∷ Int }
240240
-- |
241241
-- | codecPerson ∷ CA.JsonCodec Person
242242
-- | codecPerson =
243243
-- | CA.object "Person" $ CA.record
244-
-- | # CA.recordProp (SProxy :: _ "name") CA.string
245-
-- | # CA.recordProp (SProxy :: _ "age") CA.int
244+
-- | # CA.recordProp (Proxy :: _ "name") CA.string
245+
-- | # CA.recordProp (Proxy :: _ "age") CA.int
246246
-- | ```
247247
-- |
248248
-- | See also `Data.Codec.Argonaut.Record.object` for a more commonly useful
@@ -253,10 +253,10 @@ record = GCodec (pure {}) (Star \val → writer (Tuple val L.Nil))
253253
-- | Used with `record` to define codecs for record types that encode into JSON
254254
-- | objects of the same shape. See the comment on `record` for an example.
255255
recordProp
256-
p a r r'
256+
proxy p a r r'
257257
. IsSymbol p
258258
Row.Cons p a r r'
259-
SProxy p
259+
proxy p
260260
JsonCodec a
261261
JPropCodec (Record r)
262262
JPropCodec (Record r')

src/Data/Codec/Argonaut/Generic.purs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import Data.Codec as C
88
import Data.Codec.Argonaut as CA
99
import Data.Either (Either(..), note)
1010
import Data.Generic.Rep (class Generic, Constructor(..), NoArguments(..), Sum(..), from, to)
11-
import Data.Symbol (class IsSymbol, SProxy(..), reflectSymbol)
11+
import Data.Symbol (class IsSymbol, reflectSymbol)
12+
import Type.Proxy (Proxy(..))
1213

1314
-- | Encodes nullary sums with a Generic instance as strings that match the constructor names.
1415
-- |
@@ -41,9 +42,9 @@ instance nullarySumCodecSum ∷ (NullarySumCodec a, NullarySumCodec b) ⇒ Nulla
4142

4243
instance nullarySumCodecCtorIsSymbol name NullarySumCodec (Constructor name NoArguments) where
4344
nullarySumEncode _ =
44-
J.fromString $ reflectSymbol (SProxy SProxy name)
45+
J.fromString $ reflectSymbol (Proxy Proxy name)
4546
nullarySumDecode name j = do
4647
tag ← note (CA.Named name (CA.TypeMismatch "String")) (J.toString j)
47-
if tag /= reflectSymbol (SProxy SProxy name)
48+
if tag /= reflectSymbol (Proxy Proxy name)
4849
then Left (CA.Named name (CA.UnexpectedValue j))
4950
else Right (Constructor NoArguments)

src/Data/Codec/Argonaut/Record.purs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
module Data.Codec.Argonaut.Record where
22

33
import Data.Codec.Argonaut as CA
4-
import Data.Symbol (class IsSymbol, SProxy(..))
4+
import Data.Symbol (class IsSymbol)
55
import Prim.Row as R
66
import Prim.RowList as RL
77
import Record as Rec
8-
import Type.Data.RowList (RLProxy(..))
98
import Type.Equality as TE
9+
import Type.Proxy (Proxy(..))
1010
import Unsafe.Coerce (unsafeCoerce)
1111

1212
-- | Constructs a `JsonCodec` for a `Record` from a name and a record of codecs.
@@ -36,12 +36,12 @@ record
3636
RowListCodec rl ri ro
3737
Record ri
3838
CA.JPropCodec (Record ro)
39-
record = rowListCodec (RLProxy RLProxy rl)
39+
record = rowListCodec (Proxy Proxy rl)
4040

4141
-- | The class used to enable the building of `Record` codecs by providing a
4242
-- | record of codecs.
43-
class RowListCodec (rlRL.RowList) (ri# Type) (ro# Type) | rl ri ro where
44-
rowListCodec RLProxy rl Record ri CA.JPropCodec (Record ro)
43+
class RowListCodec (rlRL.RowList Type) (riRow Type) (roRow Type) | rl ri ro where
44+
rowListCodec forall proxy. proxy rl Record ri CA.JPropCodec (Record ro)
4545

4646
instance rowListCodecNilRowListCodec RL.Nil () () where
4747
rowListCodec _ _ = CA.record
@@ -54,10 +54,10 @@ instance rowListCodecCons ∷
5454
, TE.TypeEquals co (CA.JsonCodec a)
5555
) RowListCodec (RL.Cons sym co rs) ri ro where
5656
rowListCodec _ codecs =
57-
CA.recordProp (SProxy SProxy sym) codec tail
57+
CA.recordProp (Proxy Proxy sym) codec tail
5858
where
5959
codec CA.JsonCodec a
60-
codec = TE.from (Rec.get (SProxy SProxy sym) codecs)
60+
codec = TE.from (Rec.get (Proxy Proxy sym) codecs)
6161

6262
tail CA.JPropCodec (Record ro')
63-
tail = rowListCodec (RLProxy RLProxy rs) ((unsafeCoerce Record ri Record ri') codecs)
63+
tail = rowListCodec (Proxy Proxy rs) ((unsafeCoerce Record ri Record ri') codecs)

src/Data/Codec/Argonaut/Variant.purs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@ import Data.Codec.Argonaut (JsonCodec, JsonDecodeError(..), decode, encode, jobj
1010
import Data.Either (Either(..))
1111
import Data.Newtype (un)
1212
import Data.Profunctor.Star (Star(..))
13-
import Data.Symbol (class IsSymbol, reflectSymbol, SProxy(..))
13+
import Data.Symbol (class IsSymbol, reflectSymbol)
1414
import Data.Tuple (Tuple(..))
15-
import Data.Variant (SProxy, Variant, case_, inj, on)
15+
import Data.Variant (Variant, case_, inj, on)
1616
import Foreign.Object as FO
1717
import Foreign.Object.ST as FOST
1818
import Prim.Row as R
1919
import Prim.RowList as RL
2020
import Record as Rec
21-
import Type.Data.RowList (RLProxy(..))
2221
import Type.Equality as TE
2322
import Unsafe.Coerce (unsafeCoerce)
23+
import Type.Proxy (Proxy(..))
2424

2525
-- | Builds a codec for a variant from a record, similar to the way
2626
-- | `Variant.match` works to pattern match on a variant.
@@ -47,8 +47,8 @@ import Unsafe.Coerce (unsafeCoerce)
4747
-- | })
4848
-- | where
4949
-- | toVariant = case _ of
50-
-- | Just a → V.inj (SProxy ∷ _ "just") a
51-
-- | Nothing → V.inj (SProxy ∷ _ "nothing") unit
50+
-- | Just a → V.inj (Proxy ∷ _ "just") a
51+
-- | Nothing → V.inj (Proxy ∷ _ "nothing") unit
5252
-- | fromVariant = V.match
5353
-- | { just: Just
5454
-- | , nothing: \_ → Nothing
@@ -60,7 +60,7 @@ variantMatch
6060
VariantCodec rl ri ro
6161
Record ri
6262
JsonCodec (Variant ro)
63-
variantMatch = variantCodec (RLProxy RLProxy rl)
63+
variantMatch = variantCodec (Proxy Proxy rl)
6464

6565
-- | Builds codecs for variants in combination with `variantCase`.
6666
-- |
@@ -82,17 +82,17 @@ variantMatch = variantCodec (RLProxy ∷ RLProxy rl)
8282
-- | fromVariant = V.case_
8383
-- | # V.on _Just Just
8484
-- | # V.on _Nothing (const Nothing)
85-
-- | _Just = SProxySProxy "just"
86-
-- | _Nothing = SProxySProxy "nothing"
85+
-- | _Just = ProxyProxy "just"
86+
-- | _Nothing = ProxyProxy "nothing"
8787
-- |```
8888
variant JsonCodec (Variant ())
8989
variant = GCodec (ReaderT (Left <<< UnexpectedValue)) (Star case_)
9090

9191
variantCase
92-
l a r r'
92+
proxy l a r r'
9393
. IsSymbol l
9494
R.Cons l a r r'
95-
SProxy l
95+
proxy l
9696
Either a (JsonCodec a)
9797
JsonCodec (Variant r)
9898
JsonCodec (Variant r')
@@ -128,8 +128,8 @@ variantCase proxy eacodec (GCodec dec enc) = GCodec dec' enc'
128128

129129
-- | The class used to enable the building of `Variant` codecs from a record of
130130
-- | codecs.
131-
class VariantCodec (rlRL.RowList) (ri# Type) (ro# Type) | rl ri ro where
132-
variantCodec RLProxy rl Record ri JsonCodec (Variant ro)
131+
class VariantCodec (rlRL.RowList Type) (riRow Type) (roRow Type) | rl ri ro where
132+
variantCodec forall proxy. proxy rl Record ri JsonCodec (Variant ro)
133133

134134
instance variantCodecNilVariantCodec RL.Nil () () where
135135
variantCodec _ _ = variant
@@ -142,10 +142,10 @@ instance variantCodecCons ∷
142142
, TE.TypeEquals co (Either a (JsonCodec a))
143143
) VariantCodec (RL.Cons sym co rs) ri ro where
144144
variantCodec _ codecs =
145-
variantCase (SProxy SProxy sym) codec tail
145+
variantCase (Proxy Proxy sym) codec tail
146146
where
147147
codec Either a (JsonCodec a)
148-
codec = TE.from (Rec.get (SProxy SProxy sym) codecs)
148+
codec = TE.from (Rec.get (Proxy Proxy sym) codecs)
149149

150150
tail JsonCodec (Variant ro')
151-
tail = variantCodec (RLProxy RLProxy rs) ((unsafeCoerce Record ri Record ri') codecs)
151+
tail = variantCodec (Proxy Proxy rs) ((unsafeCoerce Record ri Record ri') codecs)

test/Test/Generic.purs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import Prelude
44

55
import Data.Codec.Argonaut.Generic (nullarySum)
66
import Data.Generic.Rep (class Generic)
7-
import Data.Generic.Rep.Show (genericShow)
7+
import Data.Show.Generic (genericShow)
88
import Effect (Effect)
99
import Effect.Console (log)
1010
import Test.QuickCheck (quickCheck)

0 commit comments

Comments
 (0)