Skip to content

Commit 04d8dd0

Browse files
authored
Merge pull request #281 from mbj/fix/bounded-min-zero
[bounded] Fix 0 min bound JSON instance
2 parents 7059ed8 + cbd651e commit 04d8dd0

File tree

13 files changed

+35
-40
lines changed

13 files changed

+35
-40
lines changed

aws-secrets/test/stack-9.4-dependencies.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ bitvec 1.1.5.0
4040
blaze-builder 0.4.2.3
4141
blaze-html 0.9.1.2
4242
blaze-markup 0.8.2.8
43-
bounded 0.0.5
43+
bounded 0.0.6
4444
byteorder 1.0.4
4545
bytestring 0.11.5.2
4646
cabal-doctest 1.0.9

aws-temporary-ingress-rule/test/stack-9.4-dependencies.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ bitvec 1.1.5.0
4141
blaze-builder 0.4.2.3
4242
blaze-html 0.9.1.2
4343
blaze-markup 0.8.2.8
44-
bounded 0.0.5
44+
bounded 0.0.6
4545
byteorder 1.0.4
4646
bytestring 0.11.5.2
4747
cabal-doctest 1.0.9

bounded/bounded.cabal

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
cabal-version: 1.12
22

3-
-- This file has been generated from package.yaml by hpack version 0.35.2.
3+
-- This file has been generated from package.yaml by hpack version 0.35.5.
44
--
55
-- see: https://github.com/sol/hpack
66

77
name: bounded
8-
version: 0.0.5
8+
version: 0.0.6
99
synopsis: User definable Integral and Text bounded types
1010
homepage: https://github.com/mbj/mhs#readme
1111
bug-reports: https://github.com/mbj/mhs/issues

bounded/package.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ _common/package: !include "../common/package.yaml"
33
maintainer: Markus Schirp mbj@schirp-dso.com, Allan Lukwago <epicallan.al@gmail.com>
44
name: bounded
55
synopsis: User definable Integral and Text bounded types
6-
version: 0.0.5
6+
version: 0.0.6
77

88
<<: *defaults
99

bounded/src/Data/Bounded/JSON.hs

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,6 @@ import Data.Scientific (Scientific)
88
import qualified Data.Aeson as JSON
99
import qualified Data.Aeson.Types as JSON
1010
import qualified Data.Scientific as Scientific
11-
import qualified Data.Text as Text
12-
13-
parseJSONTextBoundedLength
14-
:: String
15-
-> (Natural, Natural)
16-
-> JSON.Value
17-
-> JSON.Parser Text
18-
parseJSONTextBoundedLength field (min, max) = JSON.withText field parseLength
19-
where
20-
parseLength text
21-
| length == 0 = failMessage "cannot be empty String"
22-
| length < min = failMessage $ "cannot have less than " <> show min <> " characters"
23-
| length > max = failMessage $ "cannot be longer than " <> show max <> " characters"
24-
| otherwise = pure text
25-
where
26-
length :: Natural
27-
length = convertImpure $ Text.length text
28-
29-
failMessage :: String -> JSON.Parser Text
30-
failMessage message = fail $ "parsing " <> field <> " failed, " <> message
3111

3212
parseJSONIntegralBounded
3313
:: forall b a . (a ~ ToBoundedIntegral b, Conversion b a, Bounded a, Integral a, Integral b, Show b)

bounded/src/Data/Bounded/Text.hs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ module Data.Bounded.Text
99
)
1010
where
1111

12-
import Data.Bounded.JSON
1312
import Data.Bounded.Prelude
1413
import Data.Bounded.TypeLevel
1514
import GHC.TypeLits (type (<=?))
@@ -57,11 +56,20 @@ instance (KnownSymbol a, Typeable b) => BoundTextLabel (a ++: b) where
5756
instance
5857
( KnownNat min, KnownNat max, BoundTextLabel label
5958
, HasValidTypeRange '(min, max) (min <=? max)
60-
)
61-
=> JSON.FromJSON (BoundText' label '( min, max) ) where
62-
parseJSON value
63-
= BoundText
64-
<$> parseJSONTextBoundedLength (labelName @label) (mkRange @min @max) value
59+
) => JSON.FromJSON (BoundText' label '( min, max) ) where
60+
parseJSON = JSON.withText field (either failError pure . convert)
61+
where
62+
field = labelName @label
63+
failError BoundTextError{..} =
64+
fail
65+
$ "parsing "
66+
<> field
67+
<> " failed, actual length: "
68+
<> show actual
69+
<> " needs to be within min: "
70+
<> show min
71+
<> " and max: "
72+
<> show max
6573

6674
instance
6775
( length ~ Length value

bounded/test/Test/Bounded/Text.hs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ import Test.Tasty.HUnit
1515
import Test.TypeSpec (Expect, ShouldBe, TypeSpec(..))
1616
import Test.TypedSpec
1717

18-
type Example = BoundText' "Example" '(2, 2)
18+
type Example = BoundText' "Example" '(2, 2)
19+
type Example0 = BoundText' "Example0" '(0, 2)
1920

2021
testTree :: TestTree
2122
testTree = testGroup "BoundText Tests"
@@ -26,12 +27,18 @@ testTree = testGroup "BoundText Tests"
2627
valueSpec :: TestTree
2728
valueSpec = testGroup "BoundText value tests"
2829
[ acceptsValidJson
30+
, acceptsValidJson0
2931
, invalidExamples
3032
, rejectsInvalidJson
3133
, truncateExamples
3234
, validExamples
3335
]
3436
where
37+
acceptsValidJson0 :: TestTree
38+
acceptsValidJson0 = testGroup "Accepts bound JSON 0 values"
39+
[ acceptJSONText ("", fromType @"" @Example0)
40+
]
41+
3542
acceptsValidJson :: TestTree
3643
acceptsValidJson = testGroup "Accepts bound JSON values"
3744
[ acceptJSONText ("CA", fromType @"CA" @Example)
@@ -40,8 +47,8 @@ valueSpec = testGroup "BoundText value tests"
4047

4148
rejectsInvalidJson :: TestTree
4249
rejectsInvalidJson = testGroup "Rejects out of bound JSON values"
43-
[ rejectJSONText @Example (mempty, "parsing Example failed, cannot be empty String")
44-
, rejectJSONText @Example ("Foo", "parsing Example failed, cannot be longer than 2 characters")
50+
[ rejectJSONText @Example ("", "parsing Example failed, actual length: 0 needs to be within min: 2 and max: 2")
51+
, rejectJSONText @Example ("Foo", "parsing Example failed, actual length: 3 needs to be within min: 2 and max: 2")
4552
]
4653

4754
invalidExamples :: TestTree

bounded/test/stack-9.4-dependencies.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ base-orphans 0.9.0
2020
bifunctors 5.5.15
2121
binary 0.8.9.1
2222
bitvec 1.1.5.0
23-
bounded 0.0.5
23+
bounded 0.0.6
2424
bytestring 0.11.5.2
2525
call-stack 0.4.0
2626
clock 0.8.4

lambda-alb/test/stack-9.4-dependencies.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ bifunctors 5.5.15
2222
binary 0.8.9.1
2323
bitvec 1.1.5.0
2424
blaze-builder 0.4.2.3
25-
bounded 0.0.5
25+
bounded 0.0.6
2626
byteorder 1.0.4
2727
bytestring 0.11.5.2
2828
call-stack 0.4.0

lambda-runtime/test/stack-9.4-dependencies.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ bifunctors 5.5.15
2222
binary 0.8.9.1
2323
bitvec 1.1.5.0
2424
blaze-builder 0.4.2.3
25-
bounded 0.0.5
25+
bounded 0.0.6
2626
byteorder 1.0.4
2727
bytestring 0.11.5.2
2828
call-stack 0.4.0

oauth/test/stack-9.4-dependencies.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ bifunctors 5.5.15
2222
binary 0.8.9.1
2323
bitvec 1.1.5.0
2424
blaze-builder 0.4.2.3
25-
bounded 0.0.5
25+
bounded 0.0.6
2626
byteorder 1.0.4
2727
bytestring 0.11.5.2
2828
call-stack 0.4.0

stack-deploy/test/stack-9.4-dependencies.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ bitvec 1.1.5.0
3838
blaze-builder 0.4.2.3
3939
blaze-html 0.9.1.2
4040
blaze-markup 0.8.2.8
41-
bounded 0.0.5
41+
bounded 0.0.6
4242
byteorder 1.0.4
4343
bytestring 0.11.5.2
4444
cabal-doctest 1.0.9

xray/test/stack-9.4-dependencies.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ base-orphans 0.9.0
1919
bifunctors 5.5.15
2020
binary 0.8.9.1
2121
bitvec 1.1.5.0
22-
bounded 0.0.5
22+
bounded 0.0.6
2323
bytestring 0.11.5.2
2424
call-stack 0.4.0
2525
case-insensitive 1.2.1.0

0 commit comments

Comments
 (0)