From 92a520a3d37b8921d472a19b80e0a2c66dff7726 Mon Sep 17 00:00:00 2001 From: Phil Freeman Date: Sun, 8 Oct 2017 13:58:37 -0700 Subject: [PATCH] updates for aff-4.0 and use purescript-records --- bower.json | 7 +++--- generated-docs/QuickServe.md | 2 +- generated-docs/QuickServe/Internal.md | 26 ---------------------- src/QuickServe.purs | 14 ++++++------ src/QuickServe/Internal.purs | 31 --------------------------- 5 files changed, 12 insertions(+), 68 deletions(-) delete mode 100644 generated-docs/QuickServe/Internal.md delete mode 100644 src/QuickServe/Internal.purs diff --git a/bower.json b/bower.json index 7c1d2ea..16a059f 100644 --- a/bower.json +++ b/bower.json @@ -12,9 +12,10 @@ "url": "git://github.com/paf31/purescript-quickserve.git" }, "dependencies": { - "purescript-aff": "^3.1.0", + "purescript-aff": "^4.0.0", "purescript-node-http": "^4.0.0", - "purescript-foreign-generic": "^4.1.0", - "purescript-typelevel-prelude": "^2.3.0" + "purescript-foreign-generic": "^5.0.0", + "purescript-typelevel-prelude": "^2.4.0", + "purescript-record": "^0.2.3" } } diff --git a/generated-docs/QuickServe.md b/generated-docs/QuickServe.md index b701191..26fb508 100644 --- a/generated-docs/QuickServe.md +++ b/generated-docs/QuickServe.md @@ -193,7 +193,7 @@ Newtype Capture _ ``` purescript class ServableList eff (l :: RowList) (r :: # Type) | l -> r where - serveListWith :: LProxy l -> { | r } -> Request -> Response -> List String -> Maybe (Eff (http :: HTTP | eff) Unit) + serveListWith :: RLProxy l -> { | r } -> Request -> Response -> List String -> Maybe (Eff (http :: HTTP | eff) Unit) ``` ##### Instances diff --git a/generated-docs/QuickServe/Internal.md b/generated-docs/QuickServe/Internal.md deleted file mode 100644 index 53c7500..0000000 --- a/generated-docs/QuickServe/Internal.md +++ /dev/null @@ -1,26 +0,0 @@ -## Module QuickServe.Internal - -#### `LProxy` - -``` purescript -data LProxy (l :: RowList) - = LProxy -``` - -#### `get` - -``` purescript -get :: forall l a r1 r2. IsSymbol l => RowCons l a r1 r2 => SProxy l -> { | r2 } -> a -``` - -Get a property from a record by providing a label as a type-level string. - -Note: the `RowCons` constraint makes this operation safe. - -#### `rowToList` - -``` purescript -rowToList :: forall proxy r l. RowToList r l => proxy r -> LProxy l -``` - - diff --git a/src/QuickServe.purs b/src/QuickServe.purs index 18ea97e..4a56e38 100644 --- a/src/QuickServe.purs +++ b/src/QuickServe.purs @@ -33,7 +33,7 @@ import Control.Monad.Eff.Ref.Unsafe (unsafeRunRef) import Control.Monad.Eff.Unsafe (unsafeCoerceEff) import Control.Monad.Except (runExcept) import Data.Bifunctor (bimap) -import Data.Either (Either(..)) +import Data.Either (Either(..), either) import Data.Foreign (renderForeignError) import Data.Foreign.Class (class Decode, class Encode) import Data.Foreign.Generic (decodeJSON, encodeJSON) @@ -42,15 +42,15 @@ import Data.Maybe (Maybe(Nothing, Just), maybe) import Data.Monoid (mempty) import Data.Newtype (class Newtype, unwrap, wrap) import Data.Nullable (toMaybe) +import Data.Record (get) import Data.String (split) import Data.Symbol (class IsSymbol, SProxy(..), reflectSymbol) import Node.Encoding (Encoding(..)) import Node.HTTP (HTTP, ListenOptions, Request, Response, createServer, listen, requestAsStream, requestMethod, requestURL, responseAsStream, setHeader, setStatusCode, setStatusMessage) import Node.Stream (end, onDataString, onEnd, onError, writeString) import Node.URL (parse) -import QuickServe.Internal (LProxy(..), get, rowToList) import Type.Proxy (Proxy(..)) -import Type.Row (class RowToList, Cons, Nil, kind RowList) +import Type.Row (class RowToList, Cons, Nil, RLProxy(..), kind RowList) import Unsafe.Coerce (unsafeCoerce) -- | A type class for types of values which define @@ -179,7 +179,7 @@ instance servableMethod let actual = requestMethod req expected = reflectSymbol (SProxy :: SProxy method) if actual == expected - then void $ runAff handleError handleResponse (unwrap respond) + then void $ runAff (either handleError handleResponse) (unwrap respond) else sendError res 405 "Method not allowed" ("Expected " <> expected) serveWith _ _ _ _ = Nothing @@ -265,11 +265,11 @@ badRoute :: forall eff. Response -> Eff (http :: HTTP | eff) Unit badRoute res = sendError res 400 "Bad Request" "No such route" instance servableRecord :: (RowToList r l, ServableList eff l r) => Servable eff (Record r) where - serveWith r = serveListWith (rowToList r) r + serveWith r = serveListWith (RLProxy :: RLProxy l) r class ServableList eff (l :: RowList) (r :: # Type) | l -> r where serveListWith - :: LProxy l + :: RLProxy l -> Record r -> Request -> Response @@ -285,4 +285,4 @@ instance servableListCons serveListWith _ rec req res (actual : xs) | actual == reflectSymbol (SProxy :: SProxy route) = serveWith (get (SProxy :: SProxy route) rec :: s) req res xs - serveListWith _ rec req res xs = serveListWith (LProxy :: LProxy l) (unsafeCoerce rec) req res xs + serveListWith _ rec req res xs = serveListWith (RLProxy :: RLProxy l) (unsafeCoerce rec) req res xs diff --git a/src/QuickServe/Internal.purs b/src/QuickServe/Internal.purs deleted file mode 100644 index 4392ca3..0000000 --- a/src/QuickServe/Internal.purs +++ /dev/null @@ -1,31 +0,0 @@ -module QuickServe.Internal where - -import Prelude - -import Data.Maybe (fromMaybe') -import Data.StrMap (lookup) -import Data.Symbol (class IsSymbol, SProxy, reflectSymbol) -import Partial.Unsafe (unsafeCrashWith) -import Type.Row (class RowToList, kind RowList) -import Unsafe.Coerce (unsafeCoerce) - -data LProxy (l :: RowList) = LProxy - --- | Get a property from a record by providing a label as a type-level string. --- | --- | Note: the `RowCons` constraint makes this operation safe. -get :: forall l a r1 r2 - . IsSymbol l - => RowCons l a r1 r2 - => SProxy l - -> Record r2 - -> a -get l r = - fromMaybe' - (\_ -> unsafeCrashWith ("unsafeGet: missing key " <> show s)) - (lookup s (unsafeCoerce r)) - where - s = reflectSymbol l - -rowToList :: forall proxy r l. RowToList r l => proxy r -> LProxy l -rowToList _ = LProxy