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
Fixing a bug with aeson-style encoding on nested sum types.
And adding tests.
  • Loading branch information
krisajenkins committed Sep 17, 2020
commit f5b1c439c0c97d15051a3779f3a09f6f4f80a10d
9 changes: 6 additions & 3 deletions src/Foreign/Generic/Class.purs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module Foreign.Generic.Class where

import Prelude

import Control.Alt ((<|>))
import Control.Monad.Except (except, mapExcept)
import Data.Array ((..), zipWith, length)
Expand Down Expand Up @@ -420,8 +419,12 @@ instance genericEncodeConstructor
then maybe (unsafeToForeign {}) unsafeToForeign (encodeArgsArray args)
else case opts.sumEncoding of
TaggedObject { tagFieldName, contentsFieldName, constructorTagTransform } ->
unsafeToForeign (Object.singleton tagFieldName (unsafeToForeign $ constructorTagTransform ctorName)
`Object.union` objectFromArgs opts.sumEncoding (encodeArgsArray args))
unsafeToForeign $
let tagPart = Object.singleton tagFieldName (unsafeToForeign $ constructorTagTransform ctorName)
contentPart = objectFromArgs opts.sumEncoding (encodeArgsArray args)
in if Object.member tagFieldName contentPart
then Object.insert contentsFieldName (unsafeToForeign contentPart) tagPart
else tagPart `Object.union` contentPart
where
ctorName = reflectSymbol (SProxy :: SProxy name)

Expand Down
99 changes: 99 additions & 0 deletions test/AesonEncodingTests.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
module AesonEncodingTests (all) where

import Prelude
import Data.Generic.Rep (class Generic)
import Data.Generic.Rep.Show (genericShow)
import Foreign.Generic (class Decode, class Encode, Options, defaultOptions, encodeJSON, genericDecode, genericEncode)
import Foreign.Generic.Class (aesonSumEncoding)
import Test.Unit (TestSuite, suite, test)
import Test.Unit.Assert (equal)
import TestUtils (testRoundTrip)

all :: TestSuite
all =
suite "Aeson Encoding" do
suite "Records" do
test "Record" do
equal "{\"name\":\"Tester\",\"age\":13}"
(encodeJSON (ARecord { name: "Tester", age: 13 }))
testRoundTrip (ARecord { name: "Tester", age: 13 })
suite "Sum Types" do
test "Sum type, no arg constructor." do
equal "{\"tag\":\"NoShipperChoice\"}"
(encodeJSON NoShipperChoice)
test "Sum type, 1 arg constructor." do
equal "{\"contents\":\"Test\",\"tag\":\"ShipperChoice\"}"
(encodeJSON (ShipperChoice "Test"))
testRoundTrip NoShipperChoice
testRoundTrip (ShipperChoice "Test")
suite "Nesting" do
test "Nested no arg constructor" do
equal "{\"tag\":\"FreightForwarderShipper\",\"contents\":{\"tag\":\"NoShipperChoice\"}}"
(encodeJSON (FreightForwarderShipper NoShipperChoice))
test "Nested 1 arg constructor" do
equal "{\"tag\":\"FreightForwarderShipper\",\"contents\":{\"contents\":\"Test\",\"tag\":\"ShipperChoice\"}}"
(encodeJSON (FreightForwarderShipper (ShipperChoice "Test")))
testRoundTrip (FreightForwarderShipper NoShipperChoice)
testRoundTrip (FreightForwarderShipper (ShipperChoice "Test"))

------------------------------------------------------------
opts :: Options
opts =
defaultOptions
{ sumEncoding = aesonSumEncoding
, unwrapSingleConstructors = true
}

------------------------------------------------------------
newtype ARecord
= ARecord
{ name :: String
, age :: Int
}

derive instance eqARecord :: Eq ARecord

derive instance genericARecord :: Generic ARecord _

instance showARecord :: Show ARecord where
show = genericShow

instance decodeARecord :: Decode ARecord where
decode value = genericDecode opts value

instance encodeARecord :: Encode ARecord where
encode value = genericEncode opts value

data ShipperChoice
= ShipperChoice String
| NoShipperChoice

derive instance eqShipperChoice :: Eq ShipperChoice

derive instance genericShipperChoice :: Generic ShipperChoice _

instance showShipperChoice :: Show ShipperChoice where
show = genericShow

instance decodeShipperChoice :: Decode ShipperChoice where
decode value = genericDecode opts value

instance encodeShipperChoice :: Encode ShipperChoice where
encode value = genericEncode opts value

data FreightForwarderChoice
= FreightForwarderContact Int
| FreightForwarderShipper ShipperChoice

derive instance eqFreightForwarderChoice :: Eq FreightForwarderChoice

derive instance genericFreightForwarderChoice :: Generic FreightForwarderChoice _

instance showFreightForwarderChoice :: Show FreightForwarderChoice where
show = genericShow

instance decodeFreightForwarderChoice :: Decode FreightForwarderChoice where
decode value = genericDecode opts value

instance encodeFreightForwarderChoice :: Encode FreightForwarderChoice where
encode value = genericEncode opts value
2 changes: 2 additions & 0 deletions test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module Test.Main where
import Prelude
import TestUtils (testGenericRoundTrip, testOption, testRoundTrip)
import BigIntegerTests as BigIntegerTests
import AesonEncodingTests as AesonEncodingTests
import Control.Monad.Except (runExcept)
import Data.Bifunctor (bimap)
import Data.BigInteger as BigInteger
Expand Down Expand Up @@ -79,6 +80,7 @@ main =
runTest do
roundTripTests
BigIntegerTests.all
AesonEncodingTests.all

roundTripTests :: TestSuite
roundTripTests =
Expand Down