Skip to content

Commit 95a324d

Browse files
author
Serhii Khoma
authored
add decoder for NonEmptyString (#94)
1 parent 9ce8e17 commit 95a324d

File tree

8 files changed

+62
-50
lines changed

8 files changed

+62
-50
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Notable changes to this project are documented in this file. The format is based
77
Breaking changes (😱!!!):
88

99
New features:
10+
- 2020-12-23: Per #91 - add decoders for NonEmptyString and add decodeNonempty function
1011

1112
Bugfixes:
1213

@@ -181,4 +182,4 @@ Updated dependencies
181182

182183
## [v0.1.0](https://github.com/purescript-contrib/purescript-argonaut-codecs/releases/tag/v0.1.0) - 2015-07-13
183184

184-
- Initial release
185+
- Initial release

src/Data/Argonaut/Decode.purs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module Data.Argonaut.Decode
66
) where
77

88
import Data.Argonaut.Decode.Class (class DecodeJson, decodeJson)
9-
import Data.Argonaut.Decode.Combinators
9+
import Data.Argonaut.Decode.Combinators
1010
( getField
1111
, getFieldOptional
1212
, getFieldOptional'

src/Data/Argonaut/Decode/Class.purs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import Data.Either (Either(..))
1010
import Data.Identity (Identity)
1111
import Data.List (List)
1212
import Data.List.NonEmpty (NonEmptyList)
13+
import Data.String.NonEmpty (NonEmptyString)
1314
import Data.Map as M
1415
import Data.Maybe (Maybe(..))
1516
import Data.NonEmpty (NonEmpty)
@@ -54,6 +55,9 @@ instance decodeJsonInt :: DecodeJson Int where
5455
instance decodeJsonString :: DecodeJson String where
5556
decodeJson = decodeString
5657

58+
instance decodeJsonNonEmptyString :: DecodeJson NonEmptyString where
59+
decodeJson = decodeNonEmptyString
60+
5761
instance decodeJsonJson :: DecodeJson Json where
5862
decodeJson = Right
5963

src/Data/Argonaut/Decode/Decoders.purs

Lines changed: 49 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import Data.List (List, fromFoldable)
1515
import Data.List as L
1616
import Data.List.NonEmpty (NonEmptyList)
1717
import Data.List.NonEmpty as NEL
18+
import Data.String.NonEmpty (NonEmptyString)
19+
import Data.String.NonEmpty as NonEmptyString
1820
import Data.Map as M
1921
import Data.Maybe (maybe, Maybe(..))
2022
import Data.NonEmpty (NonEmpty, (:|))
@@ -25,14 +27,14 @@ import Data.TraversableWithIndex (traverseWithIndex)
2527
import Data.Tuple (Tuple(..))
2628
import Foreign.Object as FO
2729

28-
decodeIdentity
30+
decodeIdentity
2931
:: forall a
3032
. (Json -> Either JsonDecodeError a)
3133
-> Json
3234
-> Either JsonDecodeError (Identity a)
3335
decodeIdentity decoder json = Identity <$> decoder json
3436

35-
decodeMaybe
37+
decodeMaybe
3638
:: forall a
3739
. (Json -> Either JsonDecodeError a)
3840
-> Json
@@ -41,26 +43,26 @@ decodeMaybe decoder json
4143
| isNull json = pure Nothing
4244
| otherwise = Just <$> decoder json
4345

44-
decodeTuple
46+
decodeTuple
4547
:: forall a b
46-
. (Json -> Either JsonDecodeError a)
48+
. (Json -> Either JsonDecodeError a)
4749
-> (Json -> Either JsonDecodeError b)
48-
-> Json
50+
-> Json
4951
-> Either JsonDecodeError (Tuple a b)
5052
decodeTuple decoderA decoderB json = decodeArray Right json >>= f
5153
where
5254
f :: Array Json -> Either JsonDecodeError (Tuple a b)
53-
f = case _ of
55+
f = case _ of
5456
[a, b] -> Tuple <$> decoderA a <*> decoderB b
5557
_ -> Left $ TypeMismatch "Tuple"
5658

57-
decodeEither
59+
decodeEither
5860
:: forall a b
5961
. (Json -> Either JsonDecodeError a)
6062
-> (Json -> Either JsonDecodeError b)
6163
-> Json
6264
-> Either JsonDecodeError (Either a b)
63-
decodeEither decoderA decoderB json =
65+
decodeEither decoderA decoderB json =
6466
lmap (Named "Either") $ decodeJObject json >>= \obj -> do
6567
tag <- note (AtKey "tag" MissingValue) $ FO.lookup "tag" obj
6668
val <- note (AtKey "value" MissingValue) $ FO.lookup "value" obj
@@ -84,61 +86,66 @@ decodeInt = note (TypeMismatch "Integer") <<< fromNumber <=< decodeNumber
8486
decodeString :: Json -> Either JsonDecodeError String
8587
decodeString = caseJsonString (Left $ TypeMismatch "String") Right
8688

87-
decodeNonEmpty_Array
89+
decodeNonEmptyString :: Json -> Either JsonDecodeError NonEmptyString
90+
decodeNonEmptyString json =
91+
note (Named "NonEmptyString" $ UnexpectedValue json)
92+
=<< map (NonEmptyString.fromString) (decodeString json)
93+
94+
decodeNonEmpty_Array
8895
:: forall a
8996
. (Json -> Either JsonDecodeError a)
9097
-> Json
9198
-> Either JsonDecodeError (NonEmpty Array a)
92-
decodeNonEmpty_Array decoder =
93-
lmap (Named "NonEmpty Array")
94-
<<< traverse decoder
95-
<=< map (\x -> x.head :| x.tail)
96-
<<< note (TypeMismatch "NonEmpty Array")
99+
decodeNonEmpty_Array decoder =
100+
lmap (Named "NonEmpty Array")
101+
<<< traverse decoder
102+
<=< map (\x -> x.head :| x.tail)
103+
<<< note (TypeMismatch "NonEmpty Array")
97104
<<< Arr.uncons
98105
<=< decodeJArray
99106

100-
decodeNonEmptyArray
107+
decodeNonEmptyArray
101108
:: forall a
102109
. (Json -> Either JsonDecodeError a)
103-
-> Json
110+
-> Json
104111
-> Either JsonDecodeError (NonEmptyArray a)
105112
decodeNonEmptyArray decoder =
106113
lmap (Named "NonEmptyArray")
107-
<<< traverse decoder
114+
<<< traverse decoder
108115
<=< map (\x -> NEA.cons' x.head x.tail)
109-
<<< note (TypeMismatch "NonEmptyArray")
116+
<<< note (TypeMismatch "NonEmptyArray")
110117
<<< Arr.uncons
111118
<=< decodeJArray
112119

113-
decodeNonEmpty_List
120+
decodeNonEmpty_List
114121
:: forall a
115122
. (Json -> Either JsonDecodeError a)
116123
-> Json
117124
-> Either JsonDecodeError (NonEmpty List a)
118125
decodeNonEmpty_List decoder =
119126
lmap (Named "NonEmpty List")
120-
<<< traverse decoder
127+
<<< traverse decoder
121128
<=< map (\x -> x.head :| x.tail)
122129
<<< note (TypeMismatch "NonEmpty List")
123130
<<< L.uncons
124131
<=< map (map fromFoldable) decodeJArray
125132

126-
decodeNonEmptyList
133+
decodeNonEmptyList
127134
:: forall a
128135
. (Json -> Either JsonDecodeError a)
129136
-> Json
130137
-> Either JsonDecodeError (NonEmptyList a)
131138
decodeNonEmptyList decoder =
132139
lmap (Named "NonEmptyList")
133-
<<< traverse decoder
140+
<<< traverse decoder
134141
<=< map (\x -> NEL.cons' x.head x.tail)
135142
<<< note (TypeMismatch "NonEmptyList")
136143
<<< L.uncons
137144
<=< map (map fromFoldable) decodeJArray
138145

139146
decodeCodePoint :: Json -> Either JsonDecodeError CodePoint
140-
decodeCodePoint json =
141-
note (Named "CodePoint" $ UnexpectedValue json)
147+
decodeCodePoint json =
148+
note (Named "CodePoint" $ UnexpectedValue json)
142149
=<< map (codePointAt 0) (decodeString json)
143150

144151
decodeForeignObject
@@ -147,47 +154,47 @@ decodeForeignObject
147154
-> Json
148155
-> Either JsonDecodeError (FO.Object a)
149156
decodeForeignObject decoder =
150-
lmap (Named "ForeignObject")
151-
<<< traverse decoder
157+
lmap (Named "ForeignObject")
158+
<<< traverse decoder
152159
<=< decodeJObject
153160

154-
decodeArray
161+
decodeArray
155162
:: forall a
156163
. (Json -> Either JsonDecodeError a)
157164
-> Json
158165
-> Either JsonDecodeError (Array a)
159166
decodeArray decoder =
160167
lmap (Named "Array")
161-
<<< traverseWithIndex (\i -> lmap (AtIndex i) <<< decoder)
168+
<<< traverseWithIndex (\i -> lmap (AtIndex i) <<< decoder)
162169
<=< decodeJArray
163170

164-
decodeList
171+
decodeList
165172
:: forall a
166173
. (Json -> Either JsonDecodeError a)
167174
-> Json
168175
-> Either JsonDecodeError (List a)
169176
decodeList decoder =
170177
lmap (Named "List")
171-
<<< traverse decoder
178+
<<< traverse decoder
172179
<=< map (map fromFoldable) decodeJArray
173180

174-
decodeSet
181+
decodeSet
175182
:: forall a
176-
. Ord a
183+
. Ord a
177184
=> (Json -> Either JsonDecodeError a)
178185
-> Json
179186
-> Either JsonDecodeError (S.Set a)
180-
decodeSet decoder =
187+
decodeSet decoder =
181188
map (S.fromFoldable :: List a -> S.Set a) <<< decodeList decoder
182189

183-
decodeMap
190+
decodeMap
184191
:: forall a b
185-
. Ord a
192+
. Ord a
186193
=> (Json -> Either JsonDecodeError a)
187194
-> (Json -> Either JsonDecodeError b)
188195
-> Json
189196
-> Either JsonDecodeError (M.Map a b)
190-
decodeMap decoderA decoderB =
197+
decodeMap decoderA decoderB =
191198
map (M.fromFoldable :: List (Tuple a b) -> M.Map a b)
192199
<<< decodeList (decodeTuple decoderA decoderB)
193200

@@ -200,7 +207,7 @@ decodeJArray = note (TypeMismatch "Array") <<< toArray
200207
decodeJObject :: Json -> Either JsonDecodeError (FO.Object Json)
201208
decodeJObject = note (TypeMismatch "Object") <<< toObject
202209

203-
getField
210+
getField
204211
:: forall a
205212
. (Json -> Either JsonDecodeError a)
206213
-> FO.Object Json
@@ -212,7 +219,7 @@ getField decoder obj str =
212219
(lmap (AtKey str) <<< decoder)
213220
(FO.lookup str obj)
214221

215-
getFieldOptional
222+
getFieldOptional
216223
:: forall a
217224
. (Json -> Either JsonDecodeError a)
218225
-> FO.Object Json
@@ -223,7 +230,7 @@ getFieldOptional decoder obj str =
223230
where
224231
decode = lmap (AtKey str) <<< decoder
225232

226-
getFieldOptional'
233+
getFieldOptional'
227234
:: forall a
228235
. (Json -> Either JsonDecodeError a)
229236
-> FO.Object Json
@@ -232,8 +239,8 @@ getFieldOptional'
232239
getFieldOptional' decoder obj str =
233240
maybe (pure Nothing) decode (FO.lookup str obj)
234241
where
235-
decode json =
236-
if isNull json then
242+
decode json =
243+
if isNull json then
237244
pure Nothing
238-
else
245+
else
239246
Just <$> (lmap (AtKey str) <<< decoder) json

src/Data/Argonaut/Decode/Error.purs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
-- | Originally implemented in:
1+
-- | Originally implemented in:
22
-- | https://github.com/garyb/purescript-codec-argonaut
33
module Data.Argonaut.Decode.Error where
44

src/Data/Argonaut/Decode/Parser.purs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ import Data.Either (Either)
1111
-- | Attempt to parse a string as `Json`, failing with a typed error if the
1212
-- | JSON string is malformed.
1313
parseJson :: String -> Either JsonDecodeError Json
14-
parseJson = lmap (\_ -> TypeMismatch "JSON") <<< jsonParser
14+
parseJson = lmap (\_ -> TypeMismatch "JSON") <<< jsonParser

src/Data/Argonaut/Encode/Combinators.purs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import Data.Maybe (Maybe)
1515
import Data.Tuple (Tuple)
1616
import Data.Argonaut.Encode.Encoders as Encoders
1717

18-
-- | Creates a `Tuple String Json` entry, representing a key/value pair for
18+
-- | Creates a `Tuple String Json` entry, representing a key/value pair for
1919
-- | an object.
2020
infix 7 assoc as :=
2121

src/Data/Argonaut/Encode/Encoders.purs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ encodeSet :: forall a. Ord a => (a -> Json) -> S.Set a -> Json
8888
encodeSet encoder = encodeList encoder <<< (S.toUnfoldable :: S.Set a -> List a)
8989

9090
encodeMap :: forall a b. Ord a => (a -> Json) -> (b -> Json) -> M.Map a b -> Json
91-
encodeMap encoderA encoderB =
92-
encodeList (encodeTuple encoderA encoderB)
91+
encodeMap encoderA encoderB =
92+
encodeList (encodeTuple encoderA encoderB)
9393
<<< (M.toUnfoldable :: M.Map a b -> List (Tuple a b))
9494

9595
encodeVoid :: Void -> Json
@@ -113,6 +113,6 @@ extend encoder (Tuple k v) =
113113

114114
-- | The named Encoders of the `(~>?)` operator.
115115
extendOptional :: forall a. (a -> Json) -> Maybe (Tuple String Json) -> a -> Json
116-
extendOptional encoder = case _ of
116+
extendOptional encoder = case _ of
117117
Just kv -> extend encoder kv
118118
Nothing -> encoder

0 commit comments

Comments
 (0)