Skip to content

Const support #118

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 26, 2025
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ New features:
Bugfixes:

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

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

Expand Down
1 change: 1 addition & 0 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"purescript-argonaut-core": "^7.0.0",
"purescript-arrays": "^7.0.0",
"purescript-bifunctors": "^6.0.0",
"purescript-const": "^6.0.0",
"purescript-effect": "^4.0.0",
"purescript-either": "^6.0.0",
"purescript-foldable-traversable": "^6.0.0",
Expand Down
1 change: 1 addition & 0 deletions spago.dhall
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
, "assert"
, "bifunctors"
, "console"
, "const"
, "effect"
, "either"
, "exceptions"
Expand Down
4 changes: 4 additions & 0 deletions src/Data/Argonaut/Decode/Class.purs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Data.Argonaut.Core (Json, toObject)
import Data.Argonaut.Decode.Error (JsonDecodeError(..))
import Data.Array.NonEmpty (NonEmptyArray)
import Data.Bifunctor (lmap)
import Data.Const (Const)
import Data.Either (Either(..))
import Data.Identity (Identity)
import Data.List (List)
Expand All @@ -31,6 +32,9 @@ class DecodeJson a where
instance decodeIdentity :: DecodeJson a => DecodeJson (Identity a) where
decodeJson = decodeIdentity decodeJson

instance decodeConst :: DecodeJson a => DecodeJson (Const a b) where
decodeJson = decodeConst decodeJson

instance decodeJsonMaybe :: DecodeJson a => DecodeJson (Maybe a) where
decodeJson = decodeMaybe decodeJson

Expand Down
8 changes: 8 additions & 0 deletions src/Data/Argonaut/Decode/Decoders.purs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Data.Array as Arr
import Data.Array.NonEmpty (NonEmptyArray)
import Data.Array.NonEmpty as NEA
import Data.Bifunctor (lmap)
import Data.Const (Const(..))
import Data.Either (Either(..), note)
import Data.Identity (Identity(..))
import Data.Int (fromNumber)
Expand Down Expand Up @@ -35,6 +36,13 @@ decodeIdentity
-> Either JsonDecodeError (Identity a)
decodeIdentity decoder json = Identity <$> decoder json

decodeConst
:: forall a b
. (Json -> Either JsonDecodeError a)
-> Json
-> Either JsonDecodeError (Const a b)
decodeConst decoder json = Const <$> decoder json

decodeMaybe
:: forall a
. (Json -> Either JsonDecodeError a)
Expand Down
4 changes: 4 additions & 0 deletions src/Data/Argonaut/Encode/Class.purs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Data.Argonaut.Encode.Encoders
import Data.Argonaut.Core (Json, fromObject)
import Data.Array.NonEmpty (NonEmptyArray)
import Data.String.NonEmpty (NonEmptyString)
import Data.Const (Const)
import Data.Either (Either)
import Data.Identity (Identity)
import Data.List (List)
Expand All @@ -29,6 +30,9 @@ class EncodeJson a where
instance encodeIdentity :: EncodeJson a => EncodeJson (Identity a) where
encodeJson = encodeIdentity encodeJson

instance encodeConst :: EncodeJson a => EncodeJson (Const a b) where
encodeJson = encodeConst encodeJson

instance encodeJsonMaybe :: EncodeJson a => EncodeJson (Maybe a) where
encodeJson = encodeMaybe encodeJson

Expand Down
4 changes: 4 additions & 0 deletions src/Data/Argonaut/Encode/Encoders.purs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Data.Argonaut.Core (Json, fromArray, fromBoolean, fromNumber, fromObject,
import Data.Array as Arr
import Data.Array.NonEmpty (NonEmptyArray)
import Data.Array.NonEmpty as NEA
import Data.Const (Const(..))
import Data.Either (Either, either)
import Data.Identity (Identity(..))
import Data.Int (toNumber)
Expand All @@ -27,6 +28,9 @@ import Foreign.Object as FO
encodeIdentity :: forall a. (a -> Json) -> Identity a -> Json
encodeIdentity encoder (Identity a) = encoder a

encodeConst :: forall a b. (a -> Json) -> Const a b -> Json
encodeConst encoder (Const a) = encoder a

encodeMaybe :: forall a. (a -> Json) -> Maybe a -> Json
encodeMaybe encoder = case _ of
Nothing -> jsonNull
Expand Down