Skip to content

Commit 1074929

Browse files
Const support (#118)
* Const instance. * Update changelog.
1 parent b83d2d2 commit 1074929

File tree

7 files changed

+23
-0
lines changed

7 files changed

+23
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ New features:
1212
Bugfixes:
1313

1414
Other improvements:
15+
- Decoding and encoding of the `Const` functor. (#118 by @bakhtiyarneyman)
1516

1617
## [v9.0.0](https://github.com/purescript-contrib/purescript-argonaut-codecs/releases/tag/v9.1.0) - 2022-06-23
1718

bower.json

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"purescript-argonaut-core": "^7.0.0",
1818
"purescript-arrays": "^7.0.0",
1919
"purescript-bifunctors": "^6.0.0",
20+
"purescript-const": "^6.0.0",
2021
"purescript-effect": "^4.0.0",
2122
"purescript-either": "^6.0.0",
2223
"purescript-foldable-traversable": "^6.0.0",

spago.dhall

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
, "assert"
99
, "bifunctors"
1010
, "console"
11+
, "const"
1112
, "effect"
1213
, "either"
1314
, "exceptions"

src/Data/Argonaut/Decode/Class.purs

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import Data.Argonaut.Core (Json, toObject)
66
import Data.Argonaut.Decode.Error (JsonDecodeError(..))
77
import Data.Array.NonEmpty (NonEmptyArray)
88
import Data.Bifunctor (lmap)
9+
import Data.Const (Const)
910
import Data.Either (Either(..))
1011
import Data.Identity (Identity)
1112
import Data.List (List)
@@ -31,6 +32,9 @@ class DecodeJson a where
3132
instance decodeIdentity :: DecodeJson a => DecodeJson (Identity a) where
3233
decodeJson = decodeIdentity decodeJson
3334

35+
instance decodeConst :: DecodeJson a => DecodeJson (Const a b) where
36+
decodeJson = decodeConst decodeJson
37+
3438
instance decodeJsonMaybe :: DecodeJson a => DecodeJson (Maybe a) where
3539
decodeJson = decodeMaybe decodeJson
3640

src/Data/Argonaut/Decode/Decoders.purs

+8
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import Data.Array as Arr
88
import Data.Array.NonEmpty (NonEmptyArray)
99
import Data.Array.NonEmpty as NEA
1010
import Data.Bifunctor (lmap)
11+
import Data.Const (Const(..))
1112
import Data.Either (Either(..), note)
1213
import Data.Identity (Identity(..))
1314
import Data.Int (fromNumber)
@@ -35,6 +36,13 @@ decodeIdentity
3536
-> Either JsonDecodeError (Identity a)
3637
decodeIdentity decoder json = Identity <$> decoder json
3738

39+
decodeConst
40+
:: forall a b
41+
. (Json -> Either JsonDecodeError a)
42+
-> Json
43+
-> Either JsonDecodeError (Const a b)
44+
decodeConst decoder json = Const <$> decoder json
45+
3846
decodeMaybe
3947
:: forall a
4048
. (Json -> Either JsonDecodeError a)

src/Data/Argonaut/Encode/Class.purs

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import Data.Argonaut.Encode.Encoders
55
import Data.Argonaut.Core (Json, fromObject)
66
import Data.Array.NonEmpty (NonEmptyArray)
77
import Data.String.NonEmpty (NonEmptyString)
8+
import Data.Const (Const)
89
import Data.Either (Either)
910
import Data.Identity (Identity)
1011
import Data.List (List)
@@ -29,6 +30,9 @@ class EncodeJson a where
2930
instance encodeIdentity :: EncodeJson a => EncodeJson (Identity a) where
3031
encodeJson = encodeIdentity encodeJson
3132

33+
instance encodeConst :: EncodeJson a => EncodeJson (Const a b) where
34+
encodeJson = encodeConst encodeJson
35+
3236
instance encodeJsonMaybe :: EncodeJson a => EncodeJson (Maybe a) where
3337
encodeJson = encodeMaybe encodeJson
3438

src/Data/Argonaut/Encode/Encoders.purs

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import Data.Argonaut.Core (Json, fromArray, fromBoolean, fromNumber, fromObject,
66
import Data.Array as Arr
77
import Data.Array.NonEmpty (NonEmptyArray)
88
import Data.Array.NonEmpty as NEA
9+
import Data.Const (Const(..))
910
import Data.Either (Either, either)
1011
import Data.Identity (Identity(..))
1112
import Data.Int (toNumber)
@@ -27,6 +28,9 @@ import Foreign.Object as FO
2728
encodeIdentity :: forall a. (a -> Json) -> Identity a -> Json
2829
encodeIdentity encoder (Identity a) = encoder a
2930

31+
encodeConst :: forall a b. (a -> Json) -> Const a b -> Json
32+
encodeConst encoder (Const a) = encoder a
33+
3034
encodeMaybe :: forall a. (a -> Json) -> Maybe a -> Json
3135
encodeMaybe encoder = case _ of
3236
Nothing -> jsonNull

0 commit comments

Comments
 (0)