Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
68a1e47
add node stuff to gitignore
shmish111 May 10, 2019
736a8dd
rebase upstream master and add spago
shmish111 Aug 14, 2020
090a0c9
add List decode/encode
shmish111 Aug 14, 2020
f34d0c3
encode/decode tuple using array as this is what is normally done
shmish111 Aug 14, 2020
9fcc6ee
encode/decode Map using array of tuples, not sure there is another way
shmish111 Aug 14, 2020
fd7f49f
encode/decode Set using array
shmish111 Aug 14, 2020
d356fd2
upgrade purs
shmish111 Aug 17, 2020
db3685a
use spago in travis
shmish111 Aug 17, 2020
e9064e2
Adding a roundtrip test for Map.
krisajenkins Sep 3, 2020
26bc66f
More flexible Map decoding.
krisajenkins Sep 3, 2020
401b78b
Merge pull request #1 from krisajenkins/fix-map
shmish111 Sep 3, 2020
16989ec
Adding encoders/decoders for Either.
krisajenkins Sep 3, 2020
c3c6431
Merge pull request #2 from krisajenkins/add-either
shmish111 Sep 3, 2020
1e0b95f
Adding Data.BigInt support.
krisajenkins Sep 3, 2020
3d70577
Merge pull request #3 from shmish111/add-big-ints
shmish111 Sep 3, 2020
ecd14ae
Adding a shell.nix file, for a repeatable env.
krisajenkins Sep 3, 2020
b01fe37
Merge pull request #4 from shmish111/nix-shell
shmish111 Sep 3, 2020
b77bf99
Adding extra map-decoding tests.
krisajenkins Sep 4, 2020
4963d86
Improving the resiliency of BigInt decoding.
krisajenkins Sep 4, 2020
bd0a108
Improved encoding/decoding for big integers.
krisajenkins Sep 8, 2020
1f6a328
Moving to a more formal test suite.
krisajenkins Sep 11, 2020
a2c5a0d
BigInteger support.
krisajenkins Sep 10, 2020
695e30b
Refactoring the test suite.
krisajenkins Sep 17, 2020
f5b1c43
Fixing a bug with aeson-style encoding on nested sum types.
krisajenkins Sep 17, 2020
57692ed
Simplifying a fromMaybe clause.
krisajenkins Sep 17, 2020
c7cb623
add node stuff to gitignore
shmish111 May 10, 2019
9054539
rebase upstream master and add spago
shmish111 Aug 14, 2020
57539e7
add List decode/encode
shmish111 Aug 14, 2020
209e040
encode/decode tuple using array as this is what is normally done
shmish111 Aug 14, 2020
33f2a7f
encode/decode Map using array of tuples, not sure there is another way
shmish111 Aug 14, 2020
60a7a0f
encode/decode Set using array
shmish111 Aug 14, 2020
0b95402
upgrade purs
shmish111 Aug 17, 2020
58a9b33
use spago in travis
shmish111 Aug 17, 2020
0377029
Adding a roundtrip test for Map.
krisajenkins Sep 3, 2020
961aa70
More flexible Map decoding.
krisajenkins Sep 3, 2020
652a993
Adding encoders/decoders for Either.
krisajenkins Sep 3, 2020
538d5e8
Adding Data.BigInt support.
krisajenkins Sep 3, 2020
b0584ad
Adding a shell.nix file, for a repeatable env.
krisajenkins Sep 3, 2020
bcd6e20
Adding extra map-decoding tests.
krisajenkins Sep 4, 2020
f840a43
Improving the resiliency of BigInt decoding.
krisajenkins Sep 4, 2020
2d2e351
Improved encoding/decoding for big integers.
krisajenkins Sep 8, 2020
cd8ed03
Moving to a more formal test suite.
krisajenkins Sep 11, 2020
040060d
BigInteger support.
krisajenkins Sep 10, 2020
2a62593
Refactoring the test suite.
krisajenkins Sep 17, 2020
22aac68
Fixing a bug with aeson-style encoding on nested sum types.
krisajenkins Sep 17, 2020
cc4519d
Simplifying a fromMaybe clause.
krisajenkins Sep 17, 2020
bd412a1
Merge pull request #7 from shmish111/rebase-upstream
shmish111 Oct 26, 2020
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
Prev Previous commit
Next Next commit
Adding Data.BigInt support.
  • Loading branch information
krisajenkins committed Sep 3, 2020
commit 1e0b95f167879e331e05c7b1f13d03cec95cfb2f
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