Skip to content

Commit ad0d657

Browse files
authored
Updates for 0.11 (#21)
* Updates for 0.11 * Rename things, add docs * Add NullOrUndefined, refactor module and function names * Update README * Remove old docs
1 parent 6574592 commit ad0d657

File tree

20 files changed

+554
-142
lines changed

20 files changed

+554
-142
lines changed

.travis.yml

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,11 @@ language: node_js
22
dist: trusty
33
sudo: required
44
node_js: 6
5-
env:
6-
- PATH=$HOME/purescript:$PATH
75
install:
8-
- TAG=$(wget -q -O - https://github.com/purescript/purescript/releases/latest --server-response --max-redirect 0 2>&1 | sed -n -e 's/.*Location:.*tag\///p')
9-
- wget -O $HOME/purescript.tar.gz https://github.com/purescript/purescript/releases/download/$TAG/linux64.tar.gz
10-
- tar -xvf $HOME/purescript.tar.gz -C $HOME/
11-
- chmod a+x $HOME/purescript
126
- npm install -g bower
137
- npm install
148
script:
159
- bower install --production
1610
- npm run -s build
1711
- bower install
18-
- npm -s test
12+
- npm run -s test

LICENSE

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2017 Phil Freeman
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
this software and associated documentation files (the "Software"), to deal in
7+
the Software without restriction, including without limitation the rights to
8+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9+
the Software, and to permit persons to whom the Software is furnished to do so,
10+
subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
purescript-foreign-generic uses code taken from the purescript-foreign library,
23+
which is used under the terms of the MIT license, below:
24+
25+
The MIT License (MIT)
26+
27+
Copyright (c) 2014 PureScript
28+
29+
Permission is hereby granted, free of charge, to any person obtaining a copy of
30+
this software and associated documentation files (the "Software"), to deal in
31+
the Software without restriction, including without limitation the rights to
32+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
33+
the Software, and to permit persons to whom the Software is furnished to do so,
34+
subject to the following conditions:
35+
36+
The above copyright notice and this permission notice shall be included in all
37+
copies or substantial portions of the Software.
38+
39+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
40+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
41+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
42+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
43+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
44+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,52 @@
44

55
Generic deriving for `purescript-foreign`.
66

7-
- [Module Documentation](docs/Data/Foreign/Generic.md)
7+
- [Module Documentation](generated-docs/Data/Foreign/Generic.md)
88
- [Example](test/Main.purs)
99
- [Further examples in this repo](https://github.com/justinwoo/purescript-howto-foreign-generic)
1010

1111
## Example Usage
1212

13+
First, define some data type and derive `Generic`:
14+
1315
```purescript
14-
import Data.Foreign.Class (class AsForeign, class IsForeign, readJSON, write)
15-
import Data.Foreign.Generic (defaultOptions, readGeneric, toForeignGeneric)
16-
import Data.Generic.Rep (class Generic)
17-
import Data.Generic.Rep.Show (genericShow)
16+
> import Prelude
17+
> import Data.Generic.Rep (class Generic)
18+
> import Data.Generic.Rep.Show (genericShow)
19+
20+
> newtype MyRecord = MyRecord { a :: Int }
21+
> derive instance genericMyRecord :: Generic MyRecord _
22+
> instance showMyRecord :: Show MyRecord where show = genericShow
23+
```
1824

19-
newtype MyRecord = MyRecord {a :: Int}
25+
To encode JSON, use `genericEncodeJSON`:
2026

21-
derive instance genericMyRecord :: Generic MyRecord _
27+
```purescript
28+
> import Data.Foreign.Class (class Encode, class Decode, encode, decode)
29+
> import Data.Foreign.Generic (defaultOptions, genericDecodeJSON, genericEncodeJSON)
2230
23-
instance isForeignMyRecord :: IsForeign MyRecord where
24-
read = readGeneric $ defaultOptions {unwrapSingleConstructors = true}
31+
> opts = defaultOptions { unwrapSingleConstructors = true }
2532
26-
instance asForeignMyRecord :: AsForeign MyRecord where
27-
write = toForeignGeneric $ defaultOptions {unwrapSingleConstructors = true}
33+
> genericEncodeJSON opts (MyRecord { a: 1 })
34+
"{\"a\":1}"
35+
```
2836

29-
toJSONString = write >>> unsafeStringify
30-
fromJSONString = readJSON >>> runExcept
37+
And to decode JSON, use `genericDecodeJSON`:
3138

32-
main :: forall e. Eff (console :: CONSOLE | e) Unit
33-
main = do
34-
log $ toJSONString (MyRecord {a: 1})
35-
-- {a: 1}
39+
```purescript
40+
> import Control.Monad.Except
3641
37-
log $ show eMyRecord
38-
-- Right (MyRecord {a: 1})
39-
where
40-
eMyRecord :: Either _ MyRecord
41-
eMyRecord = fromJSONString """{"a": 1}"""
42-
```
42+
> runExcept (genericDecodeJSON opts "{\"a\":1}" :: _ MyRecord)
43+
(Right (MyRecord { a: 1 }))
44+
```
45+
46+
Badly formed JSON will result in a useful error, which can be inspected or pretty-printed:
47+
48+
```purescript
49+
> lmap (map renderForeignError) $ runExcept (genericDecodeJSON opts "{\"a\":\"abc\"}" :: _ MyRecord)
50+
(Left
51+
(NonEmptyList
52+
(NonEmpty
53+
"Error at array index 0: (ErrorAtProperty \"a\" (TypeMismatch \"Int\" \"String\"))"
54+
Nil)))
55+
```

bower.json

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,18 @@
1515
"url": "git://github.com/paf31/purescript-foreign-generic.git"
1616
},
1717
"dependencies": {
18-
"purescript-console": "^2.0.0",
19-
"purescript-eff": "^2.0.0",
20-
"purescript-exceptions": "^2.0.0",
21-
"purescript-foreign": "^3.0.0",
22-
"purescript-generics-rep": "^4.0.0",
23-
"purescript-globals": "^2.0.0",
24-
"purescript-maps": "^2.0.0",
25-
"purescript-nullable": "^2.0.0",
26-
"purescript-symbols": "^2.0.0"
18+
"purescript-console": "^3.0.0",
19+
"purescript-eff": "^3.0.0",
20+
"purescript-exceptions": "^3.0.0",
21+
"purescript-foreign": "^4.0.0",
22+
"purescript-generics-rep": "^5.0.0",
23+
"purescript-globals": "^3.0.0",
24+
"purescript-maps": "^3.0.0",
25+
"purescript-nullable": "^3.0.0",
26+
"purescript-proxy": "^2.0.0",
27+
"purescript-symbols": "^3.0.0"
2728
},
2829
"devDependencies": {
29-
"purescript-assert": "^2.0.0"
30+
"purescript-assert": "^3.0.0"
3031
}
3132
}

docs/Data/Foreign/Generic.md

Lines changed: 0 additions & 41 deletions
This file was deleted.
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
## Module Data.Foreign.Class
2+
3+
#### `Decode`
4+
5+
``` purescript
6+
class Decode a where
7+
decode :: Foreign -> F a
8+
```
9+
10+
The `Decode` class is used to generate decoding functions
11+
of the form `Foreign -> F a` using `generics-rep` deriving.
12+
13+
First, derive `Generic` for your data:
14+
15+
```purescript
16+
import Data.Generic.Rep
17+
18+
data MyType = MyType ...
19+
20+
derive instance genericMyType :: Generic MyType _
21+
```
22+
23+
You can then use the `genericDecode` and `genericDecodeJSON` functions
24+
to decode your foreign/JSON-encoded data.
25+
26+
##### Instances
27+
``` purescript
28+
Decode Foreign
29+
Decode String
30+
Decode Char
31+
Decode Boolean
32+
Decode Number
33+
Decode Int
34+
(Decode a) => Decode (Array a)
35+
```
36+
37+
#### `Encode`
38+
39+
``` purescript
40+
class Encode a where
41+
encode :: a -> Foreign
42+
```
43+
44+
The `Encode` class is used to generate encoding functions
45+
of the form `a -> Foreign` using `generics-rep` deriving.
46+
47+
First, derive `Generic` for your data:
48+
49+
```purescript
50+
import Data.Generic.Rep
51+
52+
data MyType = MyType ...
53+
54+
derive instance genericMyType :: Generic MyType _
55+
```
56+
57+
You can then use the `genericEncode` and `genericEncodeJSON` functions
58+
to encode your data as JSON.
59+
60+
##### Instances
61+
``` purescript
62+
Encode Foreign
63+
Encode String
64+
Encode Char
65+
Encode Boolean
66+
Encode Number
67+
Encode Int
68+
(Encode a) => Encode (Array a)
69+
```
70+
71+
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
## Module Data.Foreign.Generic
2+
3+
#### `defaultOptions`
4+
5+
``` purescript
6+
defaultOptions :: Options
7+
```
8+
9+
Default decoding/encoding options:
10+
11+
- Represent sum types as records with `tag` and `contents` fields
12+
- Unwrap single arguments
13+
- Don't unwrap single constructors
14+
15+
#### `genericDecode`
16+
17+
``` purescript
18+
genericDecode :: forall a rep. Generic a rep => GenericDecode rep => Options -> Foreign -> F a
19+
```
20+
21+
Read a value which has a `Generic` type.
22+
23+
#### `genericEncode`
24+
25+
``` purescript
26+
genericEncode :: forall a rep. Generic a rep => GenericEncode rep => Options -> a -> Foreign
27+
```
28+
29+
Generate a `Foreign` value compatible with the `readGeneric` function.
30+
31+
#### `decodeJSON`
32+
33+
``` purescript
34+
decodeJSON :: forall a. Decode a => String -> F a
35+
```
36+
37+
Decode a JSON string using a `Decode` instance.
38+
39+
#### `encodeJSON`
40+
41+
``` purescript
42+
encodeJSON :: forall a. Encode a => a -> String
43+
```
44+
45+
Decode a JSON string using a `Decode` instance.
46+
47+
#### `genericDecodeJSON`
48+
49+
``` purescript
50+
genericDecodeJSON :: forall a rep. Generic a rep => GenericDecode rep => Options -> String -> F a
51+
```
52+
53+
Read a value which has a `Generic` type from a JSON String
54+
55+
#### `genericEncodeJSON`
56+
57+
``` purescript
58+
genericEncodeJSON :: forall a rep. Generic a rep => GenericEncode rep => Options -> a -> String
59+
```
60+
61+
Write a value which has a `Generic` type as a JSON String
62+
63+

docs/Data/Foreign/Generic/Classes.md renamed to generated-docs/Data/Foreign/Generic/Class.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## Module Data.Foreign.Generic.Classes
1+
## Module Data.Foreign.Generic.Class
22

33
#### `GenericDecode`
44

@@ -38,7 +38,7 @@ class GenericDecodeArgs a where
3838
##### Instances
3939
``` purescript
4040
GenericDecodeArgs NoArguments
41-
(IsForeign a) => GenericDecodeArgs (Argument a)
41+
(Decode a) => GenericDecodeArgs (Argument a)
4242
(GenericDecodeArgs a, GenericDecodeArgs b) => GenericDecodeArgs (Product a b)
4343
(GenericDecodeFields fields) => GenericDecodeArgs (Rec fields)
4444
```
@@ -53,7 +53,7 @@ class GenericEncodeArgs a where
5353
##### Instances
5454
``` purescript
5555
GenericEncodeArgs NoArguments
56-
(AsForeign a) => GenericEncodeArgs (Argument a)
56+
(Encode a) => GenericEncodeArgs (Argument a)
5757
(GenericEncodeArgs a, GenericEncodeArgs b) => GenericEncodeArgs (Product a b)
5858
(GenericEncodeFields fields) => GenericEncodeArgs (Rec fields)
5959
```
@@ -67,7 +67,7 @@ class GenericDecodeFields a where
6767

6868
##### Instances
6969
``` purescript
70-
(IsSymbol name, IsForeign a) => GenericDecodeFields (Field name a)
70+
(IsSymbol name, Decode a) => GenericDecodeFields (Field name a)
7171
(GenericDecodeFields a, GenericDecodeFields b) => GenericDecodeFields (Product a b)
7272
```
7373

@@ -80,7 +80,7 @@ class GenericEncodeFields a where
8080

8181
##### Instances
8282
``` purescript
83-
(IsSymbol name, AsForeign a) => GenericEncodeFields (Field name a)
83+
(IsSymbol name, Encode a) => GenericEncodeFields (Field name a)
8484
(GenericEncodeFields a, GenericEncodeFields b) => GenericEncodeFields (Product a b)
8585
```
8686

File renamed without changes.

0 commit comments

Comments
 (0)