Skip to content

Commit

Permalink
Merge pull request #71 from purescript/compiler/0.12
Browse files Browse the repository at this point in the history
Update for PureScript 0.12
  • Loading branch information
garyb authored May 24, 2018
2 parents 1de4a2e + fb51a84 commit 3b8f3ae
Show file tree
Hide file tree
Showing 20 changed files with 114 additions and 142 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
/bower_components/
/node_modules/
/output/
package-lock.json
38 changes: 22 additions & 16 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
The MIT License (MIT)
Copyright 2018 PureScript

Copyright (c) 2014 PureScript
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22 changes: 11 additions & 11 deletions bower.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"name": "purescript-foreign",
"homepage": "https://github.com/purescript/purescript-foreign",
"description": "PureScript library for dealing with foreign data (JSON and JavaScript objects)",
"authors": [
"Gary Burgess <gary.burgess@gmail.com>",
"Phil Freeman <paf31@cantab.net>"
Expand All @@ -10,7 +9,7 @@
"type": "git",
"url": "git://github.com/purescript/purescript-foreign.git"
},
"license": "MIT",
"license": "BSD-3-Clause",
"ignore": [
"**/.*",
"bower_components",
Expand All @@ -21,16 +20,17 @@
"package.json"
],
"dependencies": {
"purescript-arrays": "^4.0.0",
"purescript-either": "^3.0.0",
"purescript-foldable-traversable": "^3.0.0",
"purescript-functions": "^3.0.0",
"purescript-integers": "^3.0.0",
"purescript-lists": "^4.0.0",
"purescript-strings": "^3.0.0",
"purescript-transformers": "^3.0.0"
"purescript-either": "^4.0.0",
"purescript-functions": "^4.0.0",
"purescript-identity": "^4.0.0",
"purescript-integers": "^4.0.0",
"purescript-lists": "^5.0.0",
"purescript-maybe": "^4.0.0",
"purescript-prelude": "^4.0.0",
"purescript-strings": "^4.0.0",
"purescript-transformers": "^4.0.0"
},
"devDependencies": {
"purescript-console": "^3.0.0"
"purescript-console": "^4.0.0"
}
}
12 changes: 5 additions & 7 deletions examples/Applicative.purs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ module Example.Applicative where

import Prelude

import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Console (CONSOLE, logShow)
import Control.Monad.Except (runExcept)

import Data.Foreign (F, Foreign, readNumber)
import Data.Foreign.Index ((!))

import Effect (Effect)
import Effect.Console (logShow)
import Example.Util.Value (foreignValue)
import Foreign (F, Foreign, readNumber)
import Foreign.Index ((!))

data Point = Point Number Number Number

Expand All @@ -23,7 +21,7 @@ readPoint value = do
<*> (value ! "y" >>= readNumber)
<*> (value ! "z" >>= readNumber)

main :: Eff (console :: CONSOLE) Unit
main :: Effect Unit
main =
logShow $ runExcept $
readPoint =<< foreignValue """{ "x": 1, "y": 2, "z": 3 }"""
10 changes: 4 additions & 6 deletions examples/Arrays.purs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@ module Example.Arrays where

import Prelude

import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Console (CONSOLE, logShow)
import Control.Monad.Except (runExcept)

import Data.Foreign (readArray, readNumber, readString)
import Data.Traversable (traverse)

import Effect (Effect)
import Effect.Console (logShow)
import Example.Util.Value (foreignValue)
import Foreign (readArray, readNumber, readString)

main :: Eff (console :: CONSOLE) Unit
main :: Effect Unit
main = do
logShow $ runExcept $
traverse readString =<< readArray =<< foreignValue """["hello", "world"]"""
Expand Down
57 changes: 19 additions & 38 deletions examples/Complex.purs
Original file line number Diff line number Diff line change
Expand Up @@ -2,63 +2,44 @@ module Example.Complex where

import Prelude

import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Console (CONSOLE, logShow)
import Control.Monad.Except (runExcept)

import Data.Foreign (F, Foreign, readArray, readBoolean, readNumber, readString, readNullOrUndefined)
import Data.Foreign.Index ((!))
import Data.Traversable (traverse)
import Data.Maybe (Maybe)

import Data.Traversable (traverse)
import Effect (Effect)
import Effect.Console (logShow)
import Example.Util.Value (foreignValue)
import Foreign (F, Foreign, readArray, readBoolean, readNumber, readString, readNullOrUndefined)
import Foreign.Index ((!))

newtype SomeObject =
SomeObject
{ foo :: String
, bar :: Boolean
, baz :: Number
, list :: Array ListItem
}

instance showSomeObject :: Show SomeObject where
show (SomeObject o) =
"(SomeObject { foo: " <> show o.foo <>
", bar: " <> show o.bar <>
", baz: " <> show o.baz <>
", list: " <> show o.list <>
"})"
type SomeObject =
{ foo :: String
, bar :: Boolean
, baz :: Number
, list :: Array ListItem
}

readSomeObject :: Foreign -> F SomeObject
readSomeObject value = do
foo <- value ! "foo" >>= readString
bar <- value ! "bar" >>= readBoolean
baz <- value ! "baz" >>= readNumber
list <- value ! "list" >>= readArray >>= traverse readListItem
pure $ SomeObject { foo, bar, baz, list }

newtype ListItem =
ListItem
{ x :: Number
, y :: Number
, z :: Maybe Number
}
pure { foo, bar, baz, list }

instance showListItem :: Show ListItem where
show (ListItem o) =
"(ListItem { x: " <> show o.x <>
", y: " <> show o.y <>
", z: " <> show o.z <>
" })"
type ListItem =
{ x :: Number
, y :: Number
, z :: Maybe Number
}

readListItem :: Foreign -> F ListItem
readListItem value = do
x <- value ! "x" >>= readNumber
y <- value ! "y" >>= readNumber
z <- value ! "z" >>= readNullOrUndefined >>= traverse readNumber
pure $ ListItem { x, y, z }
pure { x, y, z }

main :: Eff (console :: CONSOLE) Unit
main :: Effect Unit
main = do
let json = """{"foo":"hello","bar":true,"baz":1,"list":[{"x":1,"y":2},{"x":3,"y":4,"z":999}]}"""
logShow $ runExcept $ readSomeObject =<< foreignValue json
8 changes: 4 additions & 4 deletions examples/MaybeNullable.purs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ module Example.MaybeNullable where

import Prelude

import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Console (CONSOLE, logShow)
import Effect (Effect)
import Effect.Console (logShow)
import Control.Monad.Except (runExcept)

import Data.Foreign (readBoolean, readNull)
import Foreign (readBoolean, readNull)
import Data.Traversable (traverse)

import Example.Util.Value (foreignValue)

-- Parsing values that are allowed to null or undefined is possible by
-- using Maybe types.
main :: Eff (console :: CONSOLE) Unit
main :: Effect Unit
main = do
logShow $ runExcept $
traverse readBoolean =<< readNull =<< foreignValue "null"
Expand Down
12 changes: 5 additions & 7 deletions examples/Nested.purs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ module Example.Nested where

import Prelude

import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Console (CONSOLE, logShow)
import Control.Monad.Except (runExcept)

import Data.Foreign (F, Foreign, readNumber, readString)
import Data.Foreign.Index ((!))

import Effect (Effect)
import Effect.Console (logShow)
import Example.Util.Value (foreignValue)
import Foreign (F, Foreign, readNumber, readString)
import Foreign.Index ((!))

data Foo = Foo Bar Baz

Expand All @@ -32,7 +30,7 @@ readFoo value = do
n <- value ! "foo" ! "baz" >>= readNumber
pure $ Foo (Bar s) (Baz n)

main :: Eff (console :: CONSOLE) Unit
main :: Effect Unit
main =
logShow $ runExcept $
readFoo =<< foreignValue """{ "foo": { "bar": "bar", "baz": 1 } }"""
20 changes: 7 additions & 13 deletions examples/Objects.purs
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,22 @@ module Example.Objects where

import Prelude

import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Console (CONSOLE, logShow)
import Control.Monad.Except (runExcept)

import Data.Foreign (F, Foreign, readNumber)
import Data.Foreign.Index ((!))

import Effect (Effect)
import Effect.Console (logShow)
import Example.Util.Value (foreignValue)
import Foreign (F, Foreign, readNumber)
import Foreign.Index ((!))

newtype Point = Point { x :: Number, y :: Number }

instance showPoint :: Show Point where
show (Point { x, y }) =
"(Point { x: " <> show x <> ", y: " <> show y <> " })"
type Point = { x :: Number, y :: Number }

readPoint :: Foreign -> F Point
readPoint value = do
x <- value ! "x" >>= readNumber
y <- value ! "y" >>= readNumber
pure $ Point { x, y }
pure { x, y }

main :: Eff (console :: CONSOLE) Unit
main :: Effect Unit
main = do
logShow $ runExcept $
readPoint =<< foreignValue """{ "x": 1, "y": 2 }"""
12 changes: 5 additions & 7 deletions examples/ParseErrors.purs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@ module Example.ParseErrors where

import Prelude

import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Console (CONSOLE, logShow)
import Control.Monad.Except (runExcept)

import Data.Foreign (F, Foreign, readArray, readBoolean, readNumber, readString)
import Data.Foreign.Index ((!))
import Data.Traversable (traverse)

import Effect (Effect)
import Effect.Console (logShow)
import Example.Util.Value (foreignValue)
import Foreign (F, Foreign, readArray, readBoolean, readNumber, readString)
import Foreign.Index ((!))

newtype Point = Point { x :: Number, y :: Number }

Expand All @@ -23,7 +21,7 @@ readPoint value = do
y <- value ! "y" >>= readNumber
pure $ Point { x: x, y: y }

main :: Eff (console :: CONSOLE) Unit
main :: Effect Unit
main = do

-- When trying to parse invalid JSON we catch an exception from
Expand Down
10 changes: 4 additions & 6 deletions examples/SimpleTypes.purs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@ module Example.SimpleTypes where

import Prelude

import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Console (CONSOLE, logShow)
import Control.Monad.Except (runExcept)

import Data.Foreign (readString, readNumber, readBoolean)

import Effect (Effect)
import Effect.Console (logShow)
import Example.Util.Value (foreignValue)
import Foreign (readString, readNumber, readBoolean)

-- Parsing of the simple JSON String, Number and Boolean types is provided
-- out of the box.
main :: Eff (console :: CONSOLE) Unit
main :: Effect Unit
main = do
logShow $ runExcept $
readString =<< foreignValue "\"a JSON string\""
Expand Down
12 changes: 5 additions & 7 deletions examples/Union.purs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ module Example.Union where

import Prelude

import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Console (CONSOLE, logShow)
import Control.Monad.Except (runExcept)

import Data.Foreign (F, Foreign, readBoolean, readString)
import Data.Foreign.Index ((!))

import Effect (Effect)
import Effect.Console (logShow)
import Example.Util.Value (foreignValue)
import Foreign (F, Foreign, readBoolean, readString)
import Foreign.Index ((!))

data StringList = Nil | Cons String StringList

Expand All @@ -28,7 +26,7 @@ readStringList value =
<$> (value ! "head" >>= readString)
<*> (value ! "tail" >>= readStringList)

main :: Eff (console :: CONSOLE) Unit
main :: Effect Unit
main = do

logShow $ runExcept $
Expand Down
Loading

0 comments on commit 3b8f3ae

Please sign in to comment.