Skip to content

Commit

Permalink
Removed the toNonEmpty function. Now using List.uncons and Array.unco…
Browse files Browse the repository at this point in the history
…ns. If NonEmpty can't be decoded, explain why in terms of JSON.
  • Loading branch information
dgendill committed Nov 15, 2018
1 parent 2e3c7a6 commit b55a033
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 14 deletions.
19 changes: 6 additions & 13 deletions src/Data/Argonaut/Decode/Class.purs
Expand Up @@ -2,15 +2,15 @@ module Data.Argonaut.Decode.Class where

import Prelude

import Data.Array as Arr
import Control.Alternative (class Plus)
import Data.Argonaut.Core (Json, isNull, caseJsonNull, caseJsonBoolean, caseJsonNumber, caseJsonString, toArray, toObject, toString, stringify)
import Data.Bifunctor (lmap)
import Data.Either (Either(..))
import Data.Array as Arr
import Data.Bifunctor (lmap, rmap)
import Data.Either (Either(..), note)
import Data.Int (fromNumber)
import Data.List (List(..), (:), fromFoldable)
import Data.Map as M
import Data.List as L
import Data.Map as M
import Data.Maybe (maybe, Maybe(..))
import Data.NonEmpty (NonEmpty, singleton, (:|))
import Data.String (CodePoint, codePointAt)
Expand Down Expand Up @@ -70,22 +70,15 @@ instance decodeJsonString :: DecodeJson String where
instance decodeJsonJson :: DecodeJson Json where
decodeJson = Right


toNonEmpty :: forall a f. (Plus f) => ({ head :: f a -> Maybe a, tail :: f a -> Maybe (f a) }) -> (f a) -> Either String (NonEmpty f a)
toNonEmpty i a = case (Tuple (i.head a) (i.tail a)) of
(Tuple Nothing _) -> Left " is empty."
(Tuple (Just h) Nothing) -> Right $ singleton h
(Tuple (Just h) (Just t)) -> Right $ h :| t

instance decodeJsonNonEmptyArray :: (DecodeJson a) => DecodeJson (NonEmpty Array a) where
decodeJson
= lmap ("Couldn't decode NonEmpty Array: " <> _)
<<< (traverse decodeJson <=< (lmap ("Array" <> _) <<< toNonEmpty { head : Arr.head, tail : Arr.tail } ) <=< decodeJArray)
<<< (traverse decodeJson <=< (lmap ("JSON Array" <> _) <<< rmap (\x -> x.head :| x.tail) <<< note " is empty" <<< Arr.uncons) <=< decodeJArray)

instance decodeJsonNonEmptyList :: (DecodeJson a) => DecodeJson (NonEmpty List a) where
decodeJson
= lmap ("Couldn't decode NonEmpty List: " <> _)
<<< (traverse decodeJson <=< (lmap ("List" <> _) <<< toNonEmpty { head : L.head, tail : L.tail }) <=< map (map fromFoldable) decodeJArray)
<<< (traverse decodeJson <=< (lmap ("JSON Array" <> _) <<< rmap (\x -> x.head :| x.tail) <<< note " is empty" <<< L.uncons) <=< map (map fromFoldable) decodeJArray)

instance decodeJsonChar :: DecodeJson CodePoint where
decodeJson j =
Expand Down
2 changes: 1 addition & 1 deletion test/Test/Main.purs
Expand Up @@ -13,7 +13,7 @@ import Data.Either (Either(..))
import Data.Foldable (foldl)
import Data.List (List)
import Data.Maybe (Maybe(..), isJust, isNothing, maybe)
import Data.NonEmpty (NonEmpty(..))
import Data.NonEmpty (NonEmpty)
import Data.String.Gen (genUnicodeString)
import Data.Tuple (Tuple(..))
import Effect (Effect)
Expand Down

0 comments on commit b55a033

Please sign in to comment.