Skip to content

Commit

Permalink
Merge pull request #30 from purescript-contrib/0.9-updates
Browse files Browse the repository at this point in the history
Updates for PureScript 0.9.1
  • Loading branch information
garyb authored Jun 11, 2016
2 parents 24db0e5 + 548da98 commit 59d8798
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 101 deletions.
12 changes: 5 additions & 7 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
/.*
!/.gitignore
!/.jscsrc
!/.jshintrc
!/.travis.yml
bower_components/
node_modules/
output/
dist/
tmp/
npm-debug.log
test/test.js
/bower_components/
/node_modules/
/output/
18 changes: 9 additions & 9 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
language: node_js
sudo: false
node_js:
- 5
dist: trusty
sudo: required
node_js: 6
install:
- npm install -g bower
- npm install
script:
- npm test
- bower install --production
- npm run -s build
- bower install
- npm -s test
after_success:
- >-
test $TRAVIS_TAG &&
node_modules/.bin/psc-publish > .pursuit.json &&
curl -X POST http://pursuit.purescript.org/packages \
-d @.pursuit.json \
-H 'Accept: application/json' \
-H "Authorization: token ${GITHUB_TOKEN}"
echo $GITHUB_TOKEN | pulp login &&
echo y | pulp publish --no-push
12 changes: 6 additions & 6 deletions bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "purescript-argonaut",
"description": "PureScript version of Argonaut",
"description": "PureScript's implementation of Argonaut",
"authors": [
"Hardy Jones <>",
"John A. De Goes <[email protected]>"
Expand All @@ -11,12 +11,12 @@
},
"license": "MIT",
"dependencies": {
"purescript-argonaut-traversals": "^0.7.0",
"purescript-argonaut-core": "^0.2.1",
"purescript-argonaut-codecs": "^0.6.0"
"purescript-argonaut-codecs": "^1.0.0",
"purescript-argonaut-core": "^1.0.0",
"purescript-argonaut-traversals": "^1.0.0"
},
"devDependencies": {
"purescript-console": "^0.1.0",
"purescript-strongcheck": "^0.14.3"
"purescript-console": "^1.0.0",
"purescript-strongcheck": "^1.1.1"
}
}
71 changes: 37 additions & 34 deletions examples/Examples/Data/Argonaut/Record.purs
Original file line number Diff line number Diff line change
@@ -1,36 +1,39 @@
module Examples.Data.Argonaut.Record where

import Data.Argonaut ((~>), (:=), (.?), jsonEmptyObject, printJson)
import Data.Argonaut.Encode (EncodeJson, encodeJson)
import Data.Argonaut.Decode (DecodeJson, decodeJson)
import Data.Maybe (Maybe(..))

import Debug.Trace (print)

newtype Foo = Foo
{ foo :: Maybe Number
, bar :: Maybe String
}

instance decodeJsonFoo :: DecodeJson Foo where
decodeJson json = do
obj <- decodeJson json
foo <- obj .? "foo"
bar <- obj .? "bar"
pure $ Foo {foo: foo, bar: bar}

instance encodeJsonFoo :: EncodeJson Foo where
encodeJson (Foo f)
= "bar" := f.bar
~> "foo" := f.foo
~> jsonEmptyObject

instance showFoo :: Show Foo where
show (Foo f) = "Foo {foo: " ++ show f.foo ++ ", bar:" ++ show f.bar ++ "}"

foo :: Foo
foo = Foo {foo: Just 42, bar: Nothing}

main = do
print $ "raw foo is: " ++ show foo
print $ "encoded foo is: " ++ printJson (encodeJson foo)
import Prelude

import Data.Argonaut (class EncodeJson, class DecodeJson, Json, encodeJson, fromArray, decodeJson, jsonEmptyObject, (~>), (:=), (.?))
import Data.Either (Either)
import Data.Traversable (traverse)

newtype BlogPost = BlogPost
{ id :: Int
, title :: String
, categories :: String
, content :: String
}

instance decodeJsonBlogPost :: DecodeJson BlogPost where
decodeJson json = do
obj <- decodeJson json
id <- obj .? "id"
title <- obj .? "title"
categories <- obj .? "categories"
content <- obj .? "content"
pure $ BlogPost { id, title, categories, content }

instance encodeJson :: EncodeJson BlogPost where
encodeJson (BlogPost post)
= "id" := post.id
~> "title" := post.title
~> "categories" := post.categories
~> "content" := post.content
~> jsonEmptyObject

type BlogPostArray = Array BlogPost

decodeBlogPostArray :: Json -> Either String BlogPostArray
decodeBlogPostArray json = decodeJson json >>= traverse decodeJson

encodeBlogPostArray :: BlogPostArray -> Json
encodeBlogPostArray bpa = fromArray $ encodeJson <$> bpa
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"private": true,
"scripts": {
"postinstall": "pulp dep install",
"clean": "rimraf output && rimraf .pulp-cache",
"build": "pulp build",
"test": "pulp test"
"build": "pulp build --censor-lib --strict",
"test": "pulp build --include examples --censor-lib --strict && pulp test"
},
"devDependencies": {
"pulp": "^7.0.0",
"purescript": "^0.7.6",
"pulp": "^9.0.1",
"purescript-psa": "^0.3.9",
"purescript": "^0.9.1",
"rimraf": "^2.4.4"
}
}
37 changes: 17 additions & 20 deletions src/Data/Argonaut.purs
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
module Data.Argonaut (
module Data.Argonaut.Combinators,
module Data.Argonaut.Core,
module Data.Argonaut.Decode,
module Data.Argonaut.Encode,
module Data.Argonaut.Parser,
module Data.Argonaut.Printer,
module Data.Argonaut.JCursor,
module Data.Argonaut.Prisms,
module Data.Argonaut.Traversals
module Data.Argonaut
( module Data.Argonaut.Core
, module Data.Argonaut.Decode
, module Data.Argonaut.Encode
, module Data.Argonaut.JCursor
, module Data.Argonaut.Parser
, module Data.Argonaut.Printer
, module Data.Argonaut.Prisms
, module Data.Argonaut.Traversals
) where

import Data.Argonaut.Combinators
import Data.Argonaut.Core
import Data.Argonaut.Decode
import Data.Argonaut.Encode
import Data.Argonaut.Parser
import Data.Argonaut.Printer
import Data.Argonaut.JCursor
import Data.Argonaut.Prisms
import Data.Argonaut.Traversals

import Data.Argonaut.Core (JArray, JAssoc, JBoolean, JNull, JNumber, JObject, JString, Json, foldJson, foldJsonArray, foldJsonBoolean, foldJsonNull, foldJsonNumber, foldJsonObject, foldJsonString, fromArray, fromBoolean, fromNull, fromNumber, fromObject, fromString, isArray, isBoolean, isNull, isNumber, isObject, isString, jsonEmptyArray, jsonEmptyObject, jsonFalse, jsonNull, jsonSingletonArray, jsonSingletonObject, jsonTrue, jsonZero, toArray, toBoolean, toNull, toNumber, toObject, toString)
import Data.Argonaut.Decode (class DecodeJson, decodeJson, gDecodeJson, gDecodeJson', getField, (.?))
import Data.Argonaut.Encode (class EncodeJson, assoc, encodeJson, extend, gEncodeJson, gEncodeJson', (:=), (~>))
import Data.Argonaut.JCursor (JCursor(..), JsonPrim(..), cursorGet, cursorSet, downField, downIndex, exactNull, fail, fromPrims, inferEmpty, insideOut, primBool, primNull, primNum, primStr, primToJson, runJsonPrim, toPrims)
import Data.Argonaut.Parser (jsonParser)
import Data.Argonaut.Printer (class Printer, printJson)
import Data.Argonaut.Prisms (_Array, _Boolean, _Null, _Number, _Object, _String)
import Data.Argonaut.Traversals (_JsonArray, _JsonBoolean, _JsonNull, _JsonNumber, _JsonObject, _JsonString)
44 changes: 24 additions & 20 deletions test/Test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@ module Test.Main where

import Prelude

import Data.Argonaut
import Data.Argonaut.JCursor
import Data.Argonaut.Core (Json())
import Data.Either
import Data.Tuple
import Data.Maybe
import Data.Array
import Data.List (toList)
import Control.Monad.Eff.Console
import qualified Data.StrMap as M

import Test.StrongCheck
import Test.StrongCheck.Gen
import Control.Monad.Eff.Console (log)

import Data.Argonaut (Json, fromString, encodeJson, decodeJson, fromObject, fromArray, fromNumber, fromBoolean, jsonNull, (.?))
import Data.Argonaut.JCursor (JCursor(..), toPrims, fromPrims)
import Data.Array (zipWith, nubBy, length)
import Data.Either (Either(..))
import Data.List (fromFoldable)
import Data.Maybe (Maybe(..))
import Data.StrMap as M
import Data.Tuple (Tuple(..), fst)

import Test.StrongCheck (SC, Result, assert, quickCheck', (<?>))
import Test.StrongCheck.Arbitrary (class Arbitrary, arbitrary)
import Test.StrongCheck.Data.AlphaNumString (AlphaNumString(..))
import Test.StrongCheck.Gen (Gen, Size, showSample, chooseInt, sized, frequency, oneOf, vectorOf)

newtype TestJson = TestJson Json

Expand All @@ -37,14 +39,15 @@ genJObject :: Size -> Gen Json
genJObject sz = do
v <- vectorOf sz (genJson $ sz - 1)
k <- vectorOf (length v) (arbitrary :: Gen AlphaNumString)
return $ let f (AlphaNumString s) = s ++ "x"
k' = f <$> k
in fromObject <<< M.fromList <<< toList <<< nubBy (\a b -> (fst a) == (fst b)) $ zipWith Tuple k' v
let
f (AlphaNumString s) = s <> "x"
k' = f <$> k
pure $ fromObject <<< M.fromList <<< fromFoldable <<< nubBy (\a b -> (fst a) == (fst b)) $ zipWith Tuple k' v

genJson :: Size -> Gen Json
genJson 0 = oneOf genJNull [genJBool, genJNumber, genJString]
genJson n = frequency (Tuple 1.0 genJNull) rest where
rest = toList [Tuple 2.0 genJBool,
rest = fromFoldable [Tuple 2.0 genJBool,
Tuple 2.0 genJNumber,
Tuple 3.0 genJString,
Tuple 1.0 (genJArray n),
Expand All @@ -65,7 +68,7 @@ prop_decode_then_encode (TestJson json) =

prop_toPrims_fromPrims :: TestJson -> Result
prop_toPrims_fromPrims (TestJson j) =
Just j == fromPrims (toPrims j) <?> "fromPrims.toPrims: " ++ show (toPrims j) ++ "\n\n" ++ show (fromPrims (toPrims j))
Just j == fromPrims (toPrims j) <?> "fromPrims.toPrims: " <> show (toPrims j) <> "\n\n" <> show (fromPrims (toPrims j))

newtype TestJCursor = TestJCursor JCursor

Expand All @@ -78,12 +81,13 @@ instance arbJCursor :: Arbitrary TestJCursor where
r <- if i == 0 then pure JCursorTop
else if i == 1 then JField <$> arbitrary <*> (runTestJCursor <$> arbitrary)
else JIndex <$> arbitrary <*> (runTestJCursor <$> arbitrary)
return $ TestJCursor r
pure $ TestJCursor r

prop_jcursor_serialization :: TestJCursor -> Result
prop_jcursor_serialization (TestJCursor c) =
(decodeJson (encodeJson c) == Right c) <?> "JCursor: " ++ show c
(decodeJson (encodeJson c) == Right c) <?> "JCursor: " <> show c

main :: SC () Unit
main = do
log "Showing small sample of JSON"
showSample (genJson 10)
Expand Down

0 comments on commit 59d8798

Please sign in to comment.