git.iko.soy/rowtype-yoga/purescript-yoga-json
on main help sign in
Postel's law for NonEmpty* (#18)
commitccbde17dffb99eda79c001b3164d8417a4319df5 authorMark Eibes <mark.eibes@gmail.com> dateWed 23 Nov 2022, 19:08 (4 years ago) parentbdb2316
2 files changed+17−4
src/Yoga/JSON.purs+11−4 test/BasicsSpec.purs+6
src/Yoga/JSON.purs+11−4
@@ -36,6 +36,7 @@ import Control.Comonad.Cofree as Cofree
3636import Control.Monad.Except (ExceptT(..), except, runExcept, throwError, withExcept)
3737import Data.Array as Array
3838import Data.Array.NonEmpty (NonEmptyArray, fromArray, toArray)
39+import Data.Array.NonEmpty as NonEmptyArray
3940import Data.Bifunctor (lmap)
4041import Data.DateTime (DateTime)
4142import Data.DateTime.Instant (Instant, instant, unInstant)
@@ -54,10 +55,10 @@ import Data.Maybe (Maybe(..), fromMaybe, fromMaybe', maybe)
5455import Data.Newtype (class Newtype, unwrap, wrap)
5556import Data.Nullable (Nullable, toMaybe, toNullable)
5657import Data.Number as Number
57−import Data.String.NonEmpty.Internal (NonEmptyString)
58−import Data.String.NonEmpty.Internal as NonEmptyString
58+import Data.String.NonEmpty (NonEmptyString)
59+import Data.String.NonEmpty as NonEmptyString
5960import Data.Symbol (class IsSymbol, reflectSymbol)
60−import Data.Time.Duration (Days(..), Hours(..), Milliseconds(..), Minutes(..), Seconds(..))
61+import Data.Time.Duration (Days, Hours, Milliseconds, Minutes, Seconds)
6162import Data.Traversable (traverse)
6263import Data.Tuple (Tuple(..))
6364import Data.Variant (Variant, inj, on)
@@ -228,7 +229,13 @@ instance ReadForeign Boolean where
228229instance ReadForeign a ⇒ ReadForeign (Array a) where
229230 readImpl = sequenceCombining <<< mapWithIndex readAtIdx <=< readArray
230231
231−instance ReadForeign a ⇒ ReadForeign (Maybe a) where
232+instance ReadForeign (Maybe (NonEmptyString)) where
233+ readImpl = readImpl >=> pure <<< NonEmptyString.fromString
234+
235+else instance ReadForeign a ⇒ ReadForeign (Maybe (NonEmptyArray a)) where
236+ readImpl = readImpl >=> pure <<< NonEmptyArray.fromArray
237+
238+else instance ReadForeign a ⇒ ReadForeign (Maybe a) where
232239 readImpl = readNullOrUndefined readImpl
233240 where
234241 readNullOrUndefined _ value | isNull value || isUndefined value = pure
test/BasicsSpec.purs+6
@@ -2,6 +2,7 @@ module Test.BasicsSpec where
22
33import Prelude
44
5+import Data.Array.NonEmpty (NonEmptyArray)
56import Data.Array.NonEmpty as NEA
67import Data.Either (Either(..))
78import Data.Foldable (traverse_)
@@ -59,6 +60,8 @@ spec = describe "En- and decoding" $ do
5960 it "roundtrips LazyList" $ traverse_ roundtrips (LazyList.fromFoldable [ [ "A", "B" ], [] ])
6061 it "roundtrips List" $ traverse_ roundtrips (List.fromFoldable [ [ "A", "B" ], [] ])
6162 it "roundtrips NonEmptyArray" $ roundtrips (NEA.cons' "A" [ "B" ])
63+ it "decodes Maybe NonEmptyArray" $
64+ readJSON "[]" `shouldEqual` Right (Nothing ∷ _ (NonEmptyArray Int))
6265 it "roundtrips Object" $ roundtrips (Object.fromHomogeneous { a: 12, b: 54 })
6366 it "roundtrips String Map" $ roundtrips (Map.fromFoldable [ ("A" /\ 8), ("C" /\ 7) ])
6467 it "roundtrips Int Map" $ roundtrips (Map.fromFoldable [ (4 /\ "B"), (8 /\ "D") ])
@@ -172,6 +175,9 @@ spec = describe "En- and decoding" $ do
172175 it "fails to decode empty strings" do
173176 let (result ∷ (Either _ NonEmptyString)) = readJSON (show "")
174177 result `shouldEqual` Left (pure $ ForeignError "String must not be empty")
178+ it "succeeds decoding optional empty strings" do
179+ let (result ∷ (Either _ (Maybe NonEmptyString))) = readJSON (show "")
180+ result `shouldEqual` Right Nothing
175181
176182 describe "works on trees" do
177183 it "roundtrips" do