-
Notifications
You must be signed in to change notification settings - Fork 46
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
feat(#91): add decoder for NonEmptyString #94
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,8 @@ import Data.List (List, fromFoldable) | |
import Data.List as L | ||
import Data.List.NonEmpty (NonEmptyList) | ||
import Data.List.NonEmpty as NEL | ||
import Data.String.NonEmpty (NonEmptyString) | ||
import Data.String.NonEmpty as NonEmptyString | ||
import Data.Map as M | ||
import Data.Maybe (maybe, Maybe(..)) | ||
import Data.NonEmpty (NonEmpty, (:|)) | ||
|
@@ -25,14 +27,14 @@ import Data.TraversableWithIndex (traverseWithIndex) | |
import Data.Tuple (Tuple(..)) | ||
import Foreign.Object as FO | ||
|
||
decodeIdentity | ||
decodeIdentity | ||
:: forall a | ||
. (Json -> Either JsonDecodeError a) | ||
-> Json | ||
-> Either JsonDecodeError (Identity a) | ||
decodeIdentity decoder json = Identity <$> decoder json | ||
|
||
decodeMaybe | ||
decodeMaybe | ||
:: forall a | ||
. (Json -> Either JsonDecodeError a) | ||
-> Json | ||
|
@@ -41,26 +43,26 @@ decodeMaybe decoder json | |
| isNull json = pure Nothing | ||
| otherwise = Just <$> decoder json | ||
|
||
decodeTuple | ||
decodeTuple | ||
:: forall a b | ||
. (Json -> Either JsonDecodeError a) | ||
. (Json -> Either JsonDecodeError a) | ||
-> (Json -> Either JsonDecodeError b) | ||
-> Json | ||
-> Json | ||
-> Either JsonDecodeError (Tuple a b) | ||
decodeTuple decoderA decoderB json = decodeArray Right json >>= f | ||
where | ||
f :: Array Json -> Either JsonDecodeError (Tuple a b) | ||
f = case _ of | ||
f = case _ of | ||
[a, b] -> Tuple <$> decoderA a <*> decoderB b | ||
_ -> Left $ TypeMismatch "Tuple" | ||
|
||
decodeEither | ||
decodeEither | ||
:: forall a b | ||
. (Json -> Either JsonDecodeError a) | ||
-> (Json -> Either JsonDecodeError b) | ||
-> Json | ||
-> Either JsonDecodeError (Either a b) | ||
decodeEither decoderA decoderB json = | ||
decodeEither decoderA decoderB json = | ||
lmap (Named "Either") $ decodeJObject json >>= \obj -> do | ||
tag <- note (AtKey "tag" MissingValue) $ FO.lookup "tag" obj | ||
val <- note (AtKey "value" MissingValue) $ FO.lookup "value" obj | ||
|
@@ -84,61 +86,69 @@ decodeInt = note (TypeMismatch "Integer") <<< fromNumber <=< decodeNumber | |
decodeString :: Json -> Either JsonDecodeError String | ||
decodeString = caseJsonString (Left $ TypeMismatch "String") Right | ||
|
||
decodeNonEmpty_Array | ||
decodeNonEmptyString :: Json -> Either JsonDecodeError NonEmptyString | ||
decodeNonEmptyString json = | ||
note (Named "NonEmptyString" $ UnexpectedValue json) | ||
=<< map (NonEmptyString.fromString) (decodeString json) | ||
|
||
decodeNonempty :: Json -> Either JsonDecodeError String | ||
decodeNonempty = map NonEmptyString.toString <<< decodeNonEmptyString | ||
|
||
decodeNonEmpty_Array | ||
:: forall a | ||
. (Json -> Either JsonDecodeError a) | ||
-> Json | ||
-> Either JsonDecodeError (NonEmpty Array a) | ||
decodeNonEmpty_Array decoder = | ||
lmap (Named "NonEmpty Array") | ||
<<< traverse decoder | ||
<=< map (\x -> x.head :| x.tail) | ||
<<< note (TypeMismatch "NonEmpty Array") | ||
decodeNonEmpty_Array decoder = | ||
lmap (Named "NonEmpty Array") | ||
<<< traverse decoder | ||
<=< map (\x -> x.head :| x.tail) | ||
<<< note (TypeMismatch "NonEmpty Array") | ||
<<< Arr.uncons | ||
<=< decodeJArray | ||
|
||
decodeNonEmptyArray | ||
decodeNonEmptyArray | ||
:: forall a | ||
. (Json -> Either JsonDecodeError a) | ||
-> Json | ||
-> Json | ||
-> Either JsonDecodeError (NonEmptyArray a) | ||
decodeNonEmptyArray decoder = | ||
lmap (Named "NonEmptyArray") | ||
<<< traverse decoder | ||
<<< traverse decoder | ||
<=< map (\x -> NEA.cons' x.head x.tail) | ||
<<< note (TypeMismatch "NonEmptyArray") | ||
<<< note (TypeMismatch "NonEmptyArray") | ||
<<< Arr.uncons | ||
<=< decodeJArray | ||
|
||
decodeNonEmpty_List | ||
decodeNonEmpty_List | ||
:: forall a | ||
. (Json -> Either JsonDecodeError a) | ||
-> Json | ||
-> Either JsonDecodeError (NonEmpty List a) | ||
decodeNonEmpty_List decoder = | ||
lmap (Named "NonEmpty List") | ||
<<< traverse decoder | ||
<<< traverse decoder | ||
<=< map (\x -> x.head :| x.tail) | ||
<<< note (TypeMismatch "NonEmpty List") | ||
<<< L.uncons | ||
<=< map (map fromFoldable) decodeJArray | ||
|
||
decodeNonEmptyList | ||
decodeNonEmptyList | ||
:: forall a | ||
. (Json -> Either JsonDecodeError a) | ||
-> Json | ||
-> Either JsonDecodeError (NonEmptyList a) | ||
decodeNonEmptyList decoder = | ||
lmap (Named "NonEmptyList") | ||
<<< traverse decoder | ||
<<< traverse decoder | ||
<=< map (\x -> NEL.cons' x.head x.tail) | ||
<<< note (TypeMismatch "NonEmptyList") | ||
<<< L.uncons | ||
<=< map (map fromFoldable) decodeJArray | ||
|
||
decodeCodePoint :: Json -> Either JsonDecodeError CodePoint | ||
decodeCodePoint json = | ||
note (Named "CodePoint" $ UnexpectedValue json) | ||
decodeCodePoint json = | ||
note (Named "CodePoint" $ UnexpectedValue json) | ||
=<< map (codePointAt 0) (decodeString json) | ||
|
||
decodeForeignObject | ||
|
@@ -147,47 +157,47 @@ decodeForeignObject | |
-> Json | ||
-> Either JsonDecodeError (FO.Object a) | ||
decodeForeignObject decoder = | ||
lmap (Named "ForeignObject") | ||
<<< traverse decoder | ||
lmap (Named "ForeignObject") | ||
<<< traverse decoder | ||
<=< decodeJObject | ||
|
||
decodeArray | ||
decodeArray | ||
:: forall a | ||
. (Json -> Either JsonDecodeError a) | ||
-> Json | ||
-> Either JsonDecodeError (Array a) | ||
decodeArray decoder = | ||
lmap (Named "Array") | ||
<<< traverseWithIndex (\i -> lmap (AtIndex i) <<< decoder) | ||
<<< traverseWithIndex (\i -> lmap (AtIndex i) <<< decoder) | ||
<=< decodeJArray | ||
|
||
decodeList | ||
decodeList | ||
:: forall a | ||
. (Json -> Either JsonDecodeError a) | ||
-> Json | ||
-> Either JsonDecodeError (List a) | ||
decodeList decoder = | ||
lmap (Named "List") | ||
<<< traverse decoder | ||
<<< traverse decoder | ||
<=< map (map fromFoldable) decodeJArray | ||
|
||
decodeSet | ||
decodeSet | ||
:: forall a | ||
. Ord a | ||
. Ord a | ||
=> (Json -> Either JsonDecodeError a) | ||
-> Json | ||
-> Either JsonDecodeError (S.Set a) | ||
decodeSet decoder = | ||
decodeSet decoder = | ||
map (S.fromFoldable :: List a -> S.Set a) <<< decodeList decoder | ||
|
||
decodeMap | ||
decodeMap | ||
:: forall a b | ||
. Ord a | ||
. Ord a | ||
=> (Json -> Either JsonDecodeError a) | ||
-> (Json -> Either JsonDecodeError b) | ||
-> Json | ||
-> Either JsonDecodeError (M.Map a b) | ||
decodeMap decoderA decoderB = | ||
decodeMap decoderA decoderB = | ||
map (M.fromFoldable :: List (Tuple a b) -> M.Map a b) | ||
<<< decodeList (decodeTuple decoderA decoderB) | ||
|
||
|
@@ -200,7 +210,7 @@ decodeJArray = note (TypeMismatch "Array") <<< toArray | |
decodeJObject :: Json -> Either JsonDecodeError (FO.Object Json) | ||
decodeJObject = note (TypeMismatch "Object") <<< toObject | ||
|
||
getField | ||
getField | ||
:: forall a | ||
. (Json -> Either JsonDecodeError a) | ||
-> FO.Object Json | ||
|
@@ -212,7 +222,7 @@ getField decoder obj str = | |
(lmap (AtKey str) <<< decoder) | ||
(FO.lookup str obj) | ||
|
||
getFieldOptional | ||
getFieldOptional | ||
:: forall a | ||
. (Json -> Either JsonDecodeError a) | ||
-> FO.Object Json | ||
|
@@ -223,7 +233,7 @@ getFieldOptional decoder obj str = | |
where | ||
decode = lmap (AtKey str) <<< decoder | ||
|
||
getFieldOptional' | ||
getFieldOptional' | ||
:: forall a | ||
. (Json -> Either JsonDecodeError a) | ||
-> FO.Object Json | ||
|
@@ -232,8 +242,8 @@ getFieldOptional' | |
getFieldOptional' decoder obj str = | ||
maybe (pure Nothing) decode (FO.lookup str obj) | ||
where | ||
decode json = | ||
if isNull json then | ||
decode json = | ||
if isNull json then | ||
pure Nothing | ||
else | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where did all these come from? 😅 Thanks for trimming the whitespace. |
||
else | ||
Just <$> (lmap (AtKey str) <<< decoder) json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd rather not include this function; it's pretty easy to do
map NonEmptyString.toString
in your own code and we don't otherwise provide functions beyond the initial parse in the library.