Skip to content
This repository was archived by the owner on Jun 15, 2023. It is now read-only.

Commit 0c276b5

Browse files
committed
EJson language (#1)
1 parent 01c3366 commit 0c276b5

File tree

11 files changed

+762
-0
lines changed

11 files changed

+762
-0
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/bower_components/
2+
/node_modules/
3+
/.pulp-cache/
4+
/output/
5+
/.psci*
6+
/src/.webpack.js

.travis.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
language: node_js
2+
dist: trusty
3+
sudo: required
4+
node_js:
5+
- 5
6+
install:
7+
- npm install pulp bower -g
8+
- npm install && bower install
9+
script:
10+
- pulp test
11+
12+
after_success:
13+
- >-
14+
test $TRAVIS_TAG &&
15+
psc-publish > .pursuit.json &&
16+
curl -X POST http://pursuit.purescript.org/packages \
17+
-d @.pursuit.json \
18+
-H 'Accept: application/json' \
19+
-H "Authorization: token ${GITHUB_TOKEN}"

bower.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "purescript-ejson",
3+
"version": "0.1.0",
4+
"moduleType": [
5+
"node"
6+
],
7+
"ignore": [
8+
"**/.*",
9+
"node_modules",
10+
"bower_components",
11+
"output"
12+
],
13+
"dependencies": {
14+
"purescript-argonaut-codecs": "^0.6.1",
15+
"purescript-argonaut-core": "^0.2.3",
16+
"purescript-bifunctors": "^0.4.0",
17+
"purescript-fixed-points": "^0.1.0",
18+
"purescript-hugenums": "^1.3.1",
19+
"purescript-maps": "^0.5.7",
20+
"purescript-strongcheck": "^0.14.7"
21+
}
22+
}

package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"private": true,
3+
"devDependencies": {
4+
"purescript": "^0.8.2"
5+
}
6+
}

src/Data/Json/Extended.purs

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
module Data.Json.Extended
2+
( module Sig
3+
4+
, EJson(..)
5+
, getEJson
6+
, roll
7+
, unroll
8+
9+
, null
10+
, boolean
11+
, integer
12+
, decimal
13+
, string
14+
, timestamp
15+
, date
16+
, time
17+
, interval
18+
, objectId
19+
, orderedSet
20+
, object
21+
, object'
22+
, array
23+
24+
, renderEJson
25+
26+
, arbitraryEJsonOfSize
27+
, arbitraryJsonEncodableEJsonOfSize
28+
) where
29+
30+
import Prelude
31+
32+
import Data.Eq1 (eq1)
33+
import Data.Ord1 (compare1)
34+
import Data.Argonaut.Encode (class EncodeJson, encodeJson)
35+
import Data.Argonaut.Decode (class DecodeJson, decodeJson)
36+
37+
import Data.Functor.Mu as Mu
38+
import Data.HugeNum as HN
39+
import Data.List as L
40+
import Data.Map as Map
41+
import Data.Maybe as M
42+
import Data.StrMap as SM
43+
import Data.Tuple as T
44+
45+
import Test.StrongCheck as SC
46+
import Test.StrongCheck.Gen as Gen
47+
48+
import Data.Json.Extended.Signature as Sig
49+
50+
newtype EJson = EJson (Mu.Mu Sig.EJsonF)
51+
52+
getEJson
53+
EJson
54+
Mu.Mu Sig.EJsonF
55+
getEJson (EJson x) =
56+
x
57+
58+
roll
59+
Sig.EJsonF EJson
60+
EJson
61+
roll =
62+
EJson
63+
<<< Mu.roll
64+
<<< map getEJson
65+
66+
unroll
67+
EJson
68+
Sig.EJsonF EJson
69+
unroll =
70+
getEJson
71+
>>> Mu.unroll
72+
>>> map EJson
73+
74+
instance eqEJsonEq EJson where
75+
eq (EJson a) (EJson b) =
76+
eq1 (Mu.unroll a) (Mu.unroll b)
77+
78+
instance ordEJsonOrd EJson where
79+
compare (EJson a) (EJson b) =
80+
compare1 (Mu.unroll a) (Mu.unroll b)
81+
82+
instance showEJsonShow EJson where
83+
show = renderEJson
84+
85+
instance decodeJsonEJsonDecodeJson EJson where
86+
decodeJson json =
87+
roll <$>
88+
Sig.decodeJsonEJsonF
89+
decodeJson
90+
(Sig.String >>> roll)
91+
json
92+
93+
-- | This is a _lossy_ encoding of EJSON to JSON; JSON only supports objects with strings
94+
-- as keys.
95+
instance encodeJsonEJsonEncodeJson EJson where
96+
encodeJson (EJson x) =
97+
Sig.encodeJsonEJsonF
98+
encodeJson
99+
asKey
100+
(EJson <$> Mu.unroll x)
101+
102+
where
103+
asKey
104+
EJson
105+
M.Maybe String
106+
asKey (EJson x) =
107+
case Mu.unroll x of
108+
Sig.String k → pure k
109+
_ → M.Nothing
110+
111+
112+
arbitraryEJsonOfSize
113+
Gen.Size
114+
Gen.Gen EJson
115+
arbitraryEJsonOfSize size =
116+
roll <$>
117+
case size of
118+
0Sig.arbitraryBaseEJsonF
119+
n → Sig.arbitraryEJsonF $ arbitraryEJsonOfSize (n - 1)
120+
121+
-- | Generate only JSON-encodable objects
122+
arbitraryJsonEncodableEJsonOfSize
123+
Gen.Size
124+
Gen.Gen EJson
125+
arbitraryJsonEncodableEJsonOfSize size =
126+
roll <$>
127+
case size of
128+
0Sig.arbitraryBaseEJsonF
129+
n → Sig.arbitraryEJsonFWithKeyGen keyGen $ arbitraryJsonEncodableEJsonOfSize (n - 1)
130+
where
131+
keyGen =
132+
roll <<< Sig.String <$>
133+
SC.arbitrary
134+
135+
renderEJson
136+
EJson
137+
String
138+
renderEJson (EJson x) =
139+
Sig.renderEJsonF
140+
renderEJson
141+
(EJson <$> Mu.unroll x)
142+
143+
null EJson
144+
null = roll Sig.Null
145+
146+
boolean Boolean EJson
147+
boolean = roll <<< Sig.Boolean
148+
149+
integer Int EJson
150+
integer = roll <<< Sig.Integer
151+
152+
decimal HN.HugeNum EJson
153+
decimal = roll <<< Sig.Decimal
154+
155+
string String EJson
156+
string = roll <<< Sig.String
157+
158+
timestamp String EJson
159+
timestamp = roll <<< Sig.Timestamp
160+
161+
date String EJson
162+
date = roll <<< Sig.Date
163+
164+
time String EJson
165+
time = roll <<< Sig.Time
166+
167+
interval String EJson
168+
interval = roll <<< Sig.Interval
169+
170+
objectId String EJson
171+
objectId = roll <<< Sig.ObjectId
172+
173+
orderedSet Array EJson EJson
174+
orderedSet = roll <<< Sig.OrderedSet
175+
176+
array Array EJson EJson
177+
array = roll <<< Sig.Array
178+
179+
object Map.Map EJson EJson EJson
180+
object = roll <<< Sig.Object <<< L.fromList <<< Map.toList
181+
182+
object' SM.StrMap EJson EJson
183+
object' = roll <<< Sig.Object <<< map go <<< L.fromList <<< SM.toList
184+
where
185+
go (T.Tuple a b) = T.Tuple (string a) b
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module Data.Json.Extended.Signature
2+
( module Core
3+
, module Render
4+
, module Gen
5+
, module Json
6+
) where
7+
8+
import Data.Json.Extended.Signature.Core as Core
9+
import Data.Json.Extended.Signature.Render as Render
10+
import Data.Json.Extended.Signature.Gen as Gen
11+
import Data.Json.Extended.Signature.Json as Json
12+

0 commit comments

Comments
 (0)