Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes to compile with PureScript V0.15 #76

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ node_modules/
output/
.psc-package
.psc-ide-port
.purs-repl
.spago
5 changes: 5 additions & 0 deletions packages.dhall
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
let upstream =
https://github.com/purescript/package-sets/releases/download/psc-0.15.2-20220531/packages.dhall
sha256:278d3608439187e51136251ebf12fabda62d41ceb4bec9769312a08b56f853e3

in upstream
29 changes: 29 additions & 0 deletions spago.dhall
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{ name = "foreign-generic"
, dependencies =
[ "arrays"
, "assert"
, "bifunctors"
, "console"
, "control"
, "effect"
, "either"
, "exceptions"
, "foldable-traversable"
, "foreign"
, "foreign-object"
, "identity"
, "lists"
, "maybe"
, "newtype"
, "partial"
, "prelude"
, "record"
, "strings"
, "transformers"
, "tuples"
, "typelevel-prelude"
, "unsafe-coerce"
]
, packages = ./packages.dhall
, sources = [ "src/**/*.purs", "test/**/*.purs" ]
}
33 changes: 17 additions & 16 deletions src/Foreign/Generic/Class.purs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import Data.List (List(..), (:))
import Data.List as List
import Data.Maybe (Maybe(..), maybe)
import Data.Newtype (unwrap)
import Data.Symbol (class IsSymbol, SProxy(..), reflectSymbol)
import Data.Symbol (class IsSymbol, reflectSymbol)
import Data.Traversable (sequence)
import Foreign (F, Foreign, ForeignError(..), fail, readArray, readBoolean, readChar, readInt, readNumber, readString, unsafeToForeign)
import Foreign.Generic.Internal (readObject)
Expand All @@ -26,7 +26,6 @@ import Prim.RowList (class RowToList, Nil, Cons)
import Record as Record
import Record.Builder (Builder)
import Record.Builder as Builder
import Type.Data.RowList (RLProxy(..))
import Type.Proxy (Proxy(..))
import Unsafe.Coerce (unsafeCoerce)

Expand Down Expand Up @@ -206,20 +205,22 @@ class EncodeWithOptions a where
encodeWithOptions :: Options -> a -> Foreign

instance decodeWithOptionsRecord :: (RowToList r rl, DecodeRecord r rl) => DecodeWithOptions (Record r) where
decodeWithOptions opts = map (flip Builder.build {}) <$> decodeRecordWithOptions (RLProxy :: RLProxy rl) opts
decodeWithOptions opts = map (flip Builder.build {}) <$> decodeRecordWithOptions (Proxy :: Proxy rl) opts
else instance decodeWithOptionsOther :: Decode a => DecodeWithOptions a where
decodeWithOptions _ = decode

instance encodeWithOptionsRecord :: (RowToList r rl, EncodeRecord r rl) => EncodeWithOptions (Record r) where
encodeWithOptions opts = unsafeToForeign <<< encodeRecordWithOptions (RLProxy :: RLProxy rl) opts
encodeWithOptions opts = unsafeToForeign <<< encodeRecordWithOptions (Proxy :: Proxy rl) opts
else instance encodeWithOptionsOther :: Encode a => EncodeWithOptions a where
encodeWithOptions _ = encode

class DecodeRecord :: forall k. Row Type -> k -> Constraint
class DecodeRecord r rl | rl -> r where
decodeRecordWithOptions :: RLProxy rl -> Options -> Foreign -> F (Builder {} (Record r))
decodeRecordWithOptions :: Proxy rl -> Options -> Foreign -> F (Builder {} (Record r))

class EncodeRecord :: forall k. Row Type -> k -> Constraint
class EncodeRecord r rl | rl -> r where
encodeRecordWithOptions :: RLProxy rl -> Options -> Record r -> Object Foreign
encodeRecordWithOptions :: Proxy rl -> Options -> Record r -> Object Foreign

instance decodeRecordNil :: DecodeRecord () Nil where
decodeRecordWithOptions _ _ _ = pure identity
Expand All @@ -237,12 +238,12 @@ instance decodeRecordCons
=> DecodeRecord r (Cons l a rl_)
where
decodeRecordWithOptions _ opts f = do
builder <- decodeRecordWithOptions (RLProxy :: RLProxy rl_) opts f
let l = reflectSymbol (SProxy :: SProxy l)
builder <- decodeRecordWithOptions (Proxy :: Proxy rl_) opts f
let l = reflectSymbol (Proxy :: Proxy l)
l_transformed = (opts.fieldTransform l)
f_ <- index f l_transformed
a <- mapExcept (lmap (map (ErrorAtProperty l_transformed))) (decodeWithOptions opts f_)
pure (builder >>> Builder.insert (SProxy :: SProxy l) a)
pure (builder >>> Builder.insert (Proxy :: Proxy l) a)

instance encodeRecordCons
:: ( Cons l a r_ r
Expand All @@ -253,9 +254,9 @@ instance encodeRecordCons
=> EncodeRecord r (Cons l a rl_)
where
encodeRecordWithOptions _ opts rec =
let obj = encodeRecordWithOptions (RLProxy :: RLProxy rl_) opts (unsafeCoerce rec)
l = reflectSymbol (SProxy :: SProxy l)
in Object.insert (opts.fieldTransform l) (encodeWithOptions opts (Record.get (SProxy :: SProxy l) rec)) obj
let obj = encodeRecordWithOptions (Proxy :: Proxy rl_) opts (unsafeCoerce rec)
l = reflectSymbol (Proxy :: Proxy l)
in Object.insert (opts.fieldTransform l) (encodeWithOptions opts (Record.get (Proxy :: Proxy l) rec)) obj

class GenericDecode a where
decodeOpts :: Options -> Foreign -> F a
Expand All @@ -276,7 +277,7 @@ class GenericCountArgs a where
countArgs :: Proxy a -> Either a Int

instance genericDecodeNoConstructors :: GenericDecode NoConstructors where
decodeOpts opts _ = fail (ForeignError "No constructors")
decodeOpts _ _ = fail (ForeignError "No constructors")

instance genericEncodeNoConstructors :: GenericEncode NoConstructors where
encodeOpts opts a = encodeOpts opts a
Expand All @@ -289,7 +290,7 @@ instance genericDecodeConstructor
then Constructor <$> readArguments f
else case opts.sumEncoding of
TaggedObject { tagFieldName, contentsFieldName, constructorTagTransform } -> do
tag <- mapExcept (lmap (map (ErrorAtProperty tagFieldName))) do
_ <- mapExcept (lmap (map (ErrorAtProperty tagFieldName))) do
tag <- index f tagFieldName >>= readString
let expected = constructorTagTransform ctorName
unless (tag == expected) $
Expand All @@ -299,7 +300,7 @@ instance genericDecodeConstructor
(index f contentsFieldName >>= readArguments)
pure (Constructor args)
where
ctorName = reflectSymbol (SProxy :: SProxy name)
ctorName = reflectSymbol (Proxy :: Proxy name)

numArgs = countArgs (Proxy :: Proxy rep)

Expand Down Expand Up @@ -329,7 +330,7 @@ instance genericEncodeConstructor
unsafeToForeign (Object.singleton tagFieldName (unsafeToForeign $ constructorTagTransform ctorName)
`Object.union` maybe Object.empty (Object.singleton contentsFieldName) (encodeArgsArray args))
where
ctorName = reflectSymbol (SProxy :: SProxy name)
ctorName = reflectSymbol (Proxy :: Proxy name)

encodeArgsArray :: rep -> Maybe Foreign
encodeArgsArray = unwrapArguments <<< List.toUnfoldable <<< encodeArgs opts
Expand Down
7 changes: 4 additions & 3 deletions src/Foreign/Generic/Enum.purs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import Prelude

import Control.Alt ((<|>))
import Data.Generic.Rep (class Generic, Argument, Constructor(..), NoArguments(..), Product, Sum(..), from, to)
import Data.Symbol (class IsSymbol, SProxy(..), reflectSymbol)
import Data.Symbol (class IsSymbol, reflectSymbol)
import Foreign (F, Foreign, ForeignError(..), fail, readString, unsafeToForeign)
import Partial.Unsafe (unsafeCrashWith)
import Prim.TypeError (class Fail, Text)
import Type.Prelude (Proxy(..))

type GenericEnumOptions =
{ constructorTagTransform :: String -> String
Expand Down Expand Up @@ -77,7 +78,7 @@ instance ctorNoArgsGenericDecodeEnum
fail (ForeignError ("Expected " <> show ctorName <> " tag for unary constructor literal " <> ctorName))
pure $ Constructor NoArguments
where
ctorName = constructorTagTransform $ reflectSymbol (SProxy :: SProxy name)
ctorName = constructorTagTransform $ reflectSymbol (Proxy :: Proxy name)

instance ctorArgumentGenericDecodeEnum
:: Fail (Text "genericEncode/DecodeEnum cannot be used on types that are not sums of constructors with no arguments.")
Expand All @@ -100,7 +101,7 @@ instance ctorNoArgsGenericEncodeEnum
=> GenericEncodeEnum (Constructor name NoArguments) where
encodeEnum {constructorTagTransform} _ = unsafeToForeign ctorName
where
ctorName = constructorTagTransform $ reflectSymbol (SProxy :: SProxy name)
ctorName = constructorTagTransform $ reflectSymbol (Proxy :: Proxy name)

instance ctorArgumentGenericEncodeEnum
:: Fail (Text "genericEncode/DecodeEnum cannot be used on types that are not sums of constructors with no arguments.")
Expand Down
4 changes: 2 additions & 2 deletions src/Foreign/Internal/Stringify.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
exports.unsafeStringify = function (x) {
export function unsafeStringify(x) {
return JSON.stringify(x);
};
}
4 changes: 2 additions & 2 deletions src/Foreign/JSON.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"use strict";

exports.parseJSONImpl = function (str) {
export function parseJSONImpl(str) {
return JSON.parse(str);
};
}
5 changes: 2 additions & 3 deletions src/Foreign/NullOrUndefined.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
exports['null'] = null;

exports['undefined'] = undefined;
export const nullImpl = null;
export const undefinedImpl = undefined;
10 changes: 7 additions & 3 deletions src/Foreign/NullOrUndefined.purs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@ module Foreign.NullOrUndefined where
import Prelude

import Data.Maybe (Maybe(..))
import Foreign (F, Foreign, isUndefined, isNull)
import Foreign (F, Foreign, isNull, isUndefined)

-- | Read a value which may be null or undefined.
readNullOrUndefined :: forall a. (Foreign -> F a) -> Foreign -> F (Maybe a)
readNullOrUndefined _ value | isNull value || isUndefined value = pure Nothing
readNullOrUndefined f value = Just <$> f value

foreign import undefined :: Foreign
foreign import undefinedImpl :: Foreign
undefined :: Foreign
undefined = undefinedImpl

foreign import null :: Foreign
foreign import nullImpl :: Foreign
null :: Foreign
null = nullImpl
15 changes: 8 additions & 7 deletions test/Types.purs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ module Test.Types where
import Prelude

import Data.Bifunctor (class Bifunctor)
import Foreign (ForeignError(..), fail, readArray, unsafeToForeign)
import Foreign.Generic (class Encode, class Decode, Options, SumEncoding(..), encode, decode, defaultOptions, genericDecode, genericEncode)
import Foreign.Generic.EnumEncoding (defaultGenericEnumOptions, genericDecodeEnum, genericEncodeEnum)
import Data.Generic.Rep (class Generic)
import Data.Eq.Generic (genericEq)
import Data.Show.Generic (genericShow)
import Data.Generic.Rep (class Generic)
import Data.Maybe (Maybe)
import Data.Show.Generic (genericShow)
import Data.Tuple (Tuple(..))
import Foreign (ForeignError(..), fail, readArray, unsafeToForeign)
import Foreign.Generic (genericDecode, genericEncode)
import Foreign.Generic.Class (class Decode, class DecodeWithOptions, class Encode, class EncodeWithOptions, SumEncoding(..), Options, decode, defaultOptions, encode)
import Foreign.Generic.EnumEncoding (defaultGenericEnumOptions, genericDecodeEnum, genericEncodeEnum)

newtype TupleArray a b = TupleArray (Tuple a b)

Expand Down Expand Up @@ -94,10 +95,10 @@ instance showTree :: Show a => Show (Tree a) where
instance eqTree :: Eq a => Eq (Tree a) where
eq x y = genericEq x y

instance decodeTree :: Decode a => Decode (Tree a) where
instance decodeTree :: (Decode a, DecodeWithOptions a) => Decode (Tree a) where
decode x = genericDecode defaultOptions x

instance encodeTree :: Encode a => Encode (Tree a) where
instance encodeTree :: (Encode a, EncodeWithOptions a) => Encode (Tree a) where
encode x = genericEncode defaultOptions x

newtype UndefinedTest = UndefinedTest
Expand Down