Skip to content

Commit e35698c

Browse files
abacopaf31
authored andcommitted
Fix bug constructorTagTransform (paf31#41)
* Add failing test for constructorTagTransform * Fix bug with some constructoTagTransform functions
1 parent 9ff97e6 commit e35698c

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

src/Data/Foreign/Generic/Class.purs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ instance genericDecodeConstructor
5656
then Constructor <$> readArguments f
5757
else case opts.sumEncoding of
5858
TaggedObject { tagFieldName, contentsFieldName, constructorTagTransform } -> do
59-
tag <- mapExcept (lmap (map (ErrorAtProperty contentsFieldName))) do
59+
tag <- mapExcept (lmap (map (ErrorAtProperty tagFieldName))) do
6060
tag <- index f tagFieldName >>= readString
6161
let expected = constructorTagTransform ctorName
62-
unless (constructorTagTransform tag == expected) $
62+
unless (tag == expected) $
6363
fail (ForeignError ("Expected " <> show expected <> " tag"))
6464
pure tag
6565
args <- mapExcept (lmap (map (ErrorAtProperty contentsFieldName)))

test/Types.purs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import Data.Foreign (ForeignError(ForeignError), fail, readArray, toForeign)
77
import Data.Foreign.Class (class Encode, class Decode, encode, decode)
88
import Data.Foreign.Generic (defaultOptions, genericDecode, genericEncode)
99
import Data.Foreign.Generic.EnumEncoding (defaultGenericEnumOptions, genericDecodeEnum, genericEncodeEnum)
10+
import Data.Foreign.Generic.Types (Options, SumEncoding(..))
1011
import Data.Foreign.NullOrUndefined (NullOrUndefined)
1112
import Data.Generic.Rep (class Generic)
1213
import Data.Generic.Rep.Eq (genericEq)
@@ -67,11 +68,22 @@ instance showIntList :: Show IntList where
6768
instance eqIntList :: Eq IntList where
6869
eq x y = genericEq x y
6970

71+
intListOptions :: Options
72+
intListOptions =
73+
defaultOptions { unwrapSingleConstructors = true
74+
, sumEncoding = TaggedObject { tagFieldName: "tag"
75+
, contentsFieldName: "contents"
76+
, constructorTagTransform: \tag -> case tag of
77+
"Cons" -> "cOnS"
78+
_ -> ""
79+
}
80+
}
81+
7082
instance decodeIntList :: Decode IntList where
71-
decode x = genericDecode (defaultOptions { unwrapSingleConstructors = true }) x
83+
decode x = genericDecode intListOptions x
7284

7385
instance encodeIntList :: Encode IntList where
74-
encode x = genericEncode (defaultOptions { unwrapSingleConstructors = true }) x
86+
encode x = genericEncode intListOptions x
7587

7688
-- | Balanced binary leaf trees
7789
data Tree a = Leaf a | Branch (Tree (TupleArray a a))

0 commit comments

Comments
 (0)