Skip to content

Commit b55a033

Browse files
committed
Removed the toNonEmpty function. Now using List.uncons and Array.uncons. If NonEmpty can't be decoded, explain why in terms of JSON.
1 parent 2e3c7a6 commit b55a033

File tree

2 files changed

+7
-14
lines changed

2 files changed

+7
-14
lines changed

src/Data/Argonaut/Decode/Class.purs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ module Data.Argonaut.Decode.Class where
22

33
import Prelude
44

5-
import Data.Array as Arr
65
import Control.Alternative (class Plus)
76
import Data.Argonaut.Core (Json, isNull, caseJsonNull, caseJsonBoolean, caseJsonNumber, caseJsonString, toArray, toObject, toString, stringify)
8-
import Data.Bifunctor (lmap)
9-
import Data.Either (Either(..))
7+
import Data.Array as Arr
8+
import Data.Bifunctor (lmap, rmap)
9+
import Data.Either (Either(..), note)
1010
import Data.Int (fromNumber)
1111
import Data.List (List(..), (:), fromFoldable)
12-
import Data.Map as M
1312
import Data.List as L
13+
import Data.Map as M
1414
import Data.Maybe (maybe, Maybe(..))
1515
import Data.NonEmpty (NonEmpty, singleton, (:|))
1616
import Data.String (CodePoint, codePointAt)
@@ -70,22 +70,15 @@ instance decodeJsonString :: DecodeJson String where
7070
instance decodeJsonJson :: DecodeJson Json where
7171
decodeJson = Right
7272

73-
74-
toNonEmpty :: forall a f. (Plus f) => ({ head :: f a -> Maybe a, tail :: f a -> Maybe (f a) }) -> (f a) -> Either String (NonEmpty f a)
75-
toNonEmpty i a = case (Tuple (i.head a) (i.tail a)) of
76-
(Tuple Nothing _) -> Left " is empty."
77-
(Tuple (Just h) Nothing) -> Right $ singleton h
78-
(Tuple (Just h) (Just t)) -> Right $ h :| t
79-
8073
instance decodeJsonNonEmptyArray :: (DecodeJson a) => DecodeJson (NonEmpty Array a) where
8174
decodeJson
8275
= lmap ("Couldn't decode NonEmpty Array: " <> _)
83-
<<< (traverse decodeJson <=< (lmap ("Array" <> _) <<< toNonEmpty { head : Arr.head, tail : Arr.tail } ) <=< decodeJArray)
76+
<<< (traverse decodeJson <=< (lmap ("JSON Array" <> _) <<< rmap (\x -> x.head :| x.tail) <<< note " is empty" <<< Arr.uncons) <=< decodeJArray)
8477

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

9083
instance decodeJsonChar :: DecodeJson CodePoint where
9184
decodeJson j =

test/Test/Main.purs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import Data.Either (Either(..))
1313
import Data.Foldable (foldl)
1414
import Data.List (List)
1515
import Data.Maybe (Maybe(..), isJust, isNothing, maybe)
16-
import Data.NonEmpty (NonEmpty(..))
16+
import Data.NonEmpty (NonEmpty)
1717
import Data.String.Gen (genUnicodeString)
1818
import Data.Tuple (Tuple(..))
1919
import Effect (Effect)

0 commit comments

Comments
 (0)