Skip to content

Commit 90be05a

Browse files
committed
Test the readme examples
1 parent 0807876 commit 90be05a

File tree

9 files changed

+139
-3
lines changed

9 files changed

+139
-3
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,11 @@ codec =
115115
Objects with optional properties can be defined using the [`CAR.optional`](https://pursuit.purescript.org/packages/purescript-codec-argonaut/docs/Data.Codec.Argonaut.Record#v:optional):
116116

117117
```purescript
118-
type Person =
118+
import Data.Codec.Argonaut as CA
119+
import Data.Codec.Argonaut.Record as CAR
120+
import Data.Maybe (Maybe)
121+
122+
type Person =
119123
{ name ∷ String
120124
, age ∷ Int
121125
, active ∷ Boolean
@@ -258,7 +262,7 @@ import Data.String.NonEmpty (NonEmptyString)
258262
import Data.String.NonEmpty as NES
259263
260264
codec ∷ CA.JsonCodec NonEmptyString
261-
codec = CA.prismaticCodec NES.fromString NES.toString CA.string
265+
codec = CA.prismaticCodec "NonEmptyString" NES.fromString NES.toString CA.string
262266
```
263267

264268
See the documentation for [another example](https://pursuit.purescript.org/packages/purescript-codec-argonaut/docs/Data.Codec.Argonaut#v:prismaticCodec) of how [`CA.prismaticCodec`](https://pursuit.purescript.org/packages/purescript-codec-argonaut/docs/Data.Codec.Argonaut#v:prismaticCodec) might be used. The main downside to [`CA.prismaticCodec`](https://pursuit.purescript.org/packages/purescript-codec-argonaut/docs/Data.Codec.Argonaut#v:prismaticCodec) is the error reporting for the `Nothing` case might not be good as it otherwise could be, since [`UnexpectedValue`](https://pursuit.purescript.org/packages/purescript-codec-argonaut/docs/Data.Codec.Argonaut#t:JsonDecodeError) is the only information we have at that point.

test/Test/Example/Newtype.purs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
module Test.Example.Newtype where
2+
3+
import Data.Codec.Argonaut as CA
4+
import Data.Codec.Argonaut.Record as CAR
5+
import Data.Newtype (class Newtype)
6+
import Data.Profunctor (wrapIso)
7+
8+
type PersonRec = { "Name" String, age Int, "is active"Boolean }
9+
10+
newtype Person = Person PersonRec
11+
12+
derive instance newtypePersonNewtype Person _
13+
14+
codec CA.JsonCodec Person
15+
codec =
16+
CA.coercible "Person"
17+
(CAR.record
18+
{ "Name": CA.string
19+
, age: CA.int
20+
, "is active": CA.boolean
21+
})

test/Test/Example/Object1.purs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module Test.Example.Object1 where
2+
3+
import Data.Codec.Argonaut as CA
4+
import Data.Codec.Argonaut.Record as CAR
5+
6+
type Person = { "Name" String, age Int, "is active"Boolean }
7+
8+
codec CA.JsonCodec Person
9+
codec =
10+
CA.object "Person"
11+
(CAR.record
12+
{ "Name": CA.string
13+
, age: CA.int
14+
, "is active": CA.boolean
15+
})

test/Test/Example/Object2.purs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module Test.Example.Object2 where
2+
3+
import Data.Codec.Argonaut as CA
4+
import Data.Codec.Argonaut.Record as CAR
5+
6+
type Person = { name String, age Int, active Boolean }
7+
8+
codec CA.JsonCodec Person
9+
codec =
10+
CA.object "Person"
11+
(CAR.record
12+
{ name: CA.string
13+
, age: CA.int
14+
, active: CA.boolean
15+
})

test/Test/Example/Object3.purs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
module Test.Example.Object3 where
2+
3+
import Data.Codec.Argonaut as CA
4+
import Data.Codec.Argonaut.Record as CAR
5+
import Data.Maybe (Maybe)
6+
7+
type Person =
8+
{ name String
9+
, age Int
10+
, active Boolean
11+
, email Maybe String
12+
}
13+
14+
codec CA.JsonCodec Person
15+
codec =
16+
CA.object "Person"
17+
( CAR.record
18+
{ name: CA.string
19+
, age: CA.int
20+
, active: CA.boolean
21+
, email: CAR.optional CA.string
22+
}
23+
)

test/Test/Example/Prismatic1.purs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module Test.Example.Prismatic1 where
2+
3+
import Data.Codec.Argonaut as CA
4+
import Data.String.NonEmpty (NonEmptyString)
5+
import Data.String.NonEmpty as NES
6+
7+
codec CA.JsonCodec NonEmptyString
8+
codec = CA.prismaticCodec "NonEmptyString" NES.fromString NES.toString CA.string

test/Test/Example/Sum1.purs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
module Test.Example.Sum2 where
2+
3+
import Prelude
4+
5+
import Data.Codec.Argonaut as CA
6+
import Data.Codec.Argonaut.Variant as CAV
7+
import Data.Either (Either(..))
8+
import Data.Variant as V
9+
10+
type SomeValue = V.Variant
11+
( str String
12+
, int Int
13+
, neither Unit
14+
)
15+
16+
codec CA.JsonCodec SomeValue
17+
codec = CAV.variantMatch
18+
{ str: Right CA.string
19+
, int: Right CA.int
20+
, neither: Left unit
21+
}

test/Test/Example/Sum2.purs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
module Test.Example.Sum2 where
2+
3+
import Prelude
4+
5+
import Data.Codec.Argonaut as CA
6+
import Data.Codec.Argonaut.Variant as CAV
7+
import Data.Either (Either(..))
8+
import Data.Profunctor (dimap)
9+
import Data.Variant as V
10+
import Type.Proxy (Proxy(..))
11+
12+
data SomeValue2 = Str String | Int Int | Neither
13+
14+
codec CA.JsonCodec SomeValue2
15+
codec =
16+
dimap toVariant fromVariant $ CAV.variantMatch
17+
{ str: Right CA.string
18+
, int: Right CA.int
19+
, neither: Left unit
20+
}
21+
where
22+
toVariant = case _ of
23+
Str s → V.inj (Proxy _ "str") s
24+
Int i → V.inj (Proxy _ "int") i
25+
NeitherV.inj (Proxy _ "neither") unit
26+
fromVariant = V.match
27+
{ str: Str
28+
, int: Int
29+
, neither: \_ → Neither
30+
}

test/Test/Prim.purs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import Data.Codec.Argonaut.Common as CA
1212
import Data.Either (Either(..), either, note)
1313
import Data.Generic.Rep (class Generic)
1414
import Data.Int as Int
15-
import Data.Maybe (Maybe(..))
1615
import Data.Maybe (Maybe(..), maybe)
1716
import Data.Newtype (class Newtype, unwrap, wrap)
1817
import Data.Profunctor (dimap)

0 commit comments

Comments
 (0)