Skip to content
Merged
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
3 changes: 2 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"purescript-foreign": "^5.0.0",
"purescript-generics-rep": "^6.1.1",
"purescript-foreign-object": "^2.0.3",
"purescript-record": "^2.0.1"
"purescript-record": "^2.0.1",
"purescript-bigints": "^4.0.0"
},
"devDependencies": {
"purescript-assert": "^4.1.0",
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,8 @@
"purescript": "^0.13.3",
"purescript-psa": "^0.7.3",
"rimraf": "^3.0.0"
},
"dependencies": {
"big-integer": "^1.6.48"
}
}
1 change: 1 addition & 0 deletions spago.dhall
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ You can edit this file as you like.
{ name = "foreign-generic"
, dependencies =
[ "assert"
, "bigints"
, "console"
, "effect"
, "exceptions"
Expand Down
12 changes: 11 additions & 1 deletion src/Foreign/Generic/Class.purs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import Control.Alt ((<|>))
import Control.Monad.Except (except, mapExcept)
import Data.Array ((..), zipWith, length)
import Data.Bifunctor (lmap)
import Data.Either (Either(..))
import Data.BigInt (BigInt)
import Data.BigInt as BigInt
import Data.Either (Either(..), note)
import Data.Generic.Rep (Argument(..), Constructor(..), NoArguments(..), NoConstructors, Product(..), Sum(..))
import Data.Identity (Identity(..))
import Data.List (List(..), (:))
Expand Down Expand Up @@ -204,6 +206,11 @@ instance eitherDecode :: (Decode a, Decode b) => Decode (Either a b) where
<|>
(readProp "Right" value >>= (map Right <<< decode))

instance bigIntDecode :: Decode BigInt where
decode value = do
str <- readString value
except $ note (pure (ForeignError ("Expected BigInt"))) $ BigInt.fromString str

-- | The `Encode` class is used to generate encoding functions
-- | of the form `a -> Foreign` using `generics-rep` deriving.
-- |
Expand Down Expand Up @@ -278,6 +285,9 @@ instance encodeEither :: (Encode a, Encode b) => Encode (Either a b) where
encode (Left a) = encode $ Object.singleton "Left" a
encode (Right b) = encode $ Object.singleton "Right" b

instance bigIntEncode :: Encode BigInt where
encode = encode <<< BigInt.toString

-- | When deriving `En`/`Decode` instances using `Generic`, we want
-- | the `Options` object to apply to the outermost record type(s)
-- | under the data constructors.
Expand Down
2 changes: 2 additions & 0 deletions test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Prelude

import Control.Monad.Except (runExcept)
import Data.Bifunctor (bimap)
import Data.BigInt as BigInt
import Data.Either (Either(..))
import Data.Generic.Rep (class Generic)
import Data.Map as Map
Expand Down Expand Up @@ -141,6 +142,7 @@ main = do
testRoundTrip (Object.fromFoldable [Tuple "one" 1, Tuple "two" 2])
testRoundTrip (Map.fromFoldable [Tuple "one" 1, Tuple "two" 2])
testRoundTrip [ Left 5, Right "Test" ]
testRoundTrip (BigInt.pow (BigInt.fromInt 2) (BigInt.fromInt 60)) -- 2^60. Anything over 2^32 will confuse JavaScript.
testUnaryConstructorLiteral
let opts = defaultOptions { fieldTransform = toUpper }
testGenericRoundTrip opts (RecordTest { foo: 1, bar: "test", baz: 'a' })
Expand Down