Skip to content

Commit 3cb70ef

Browse files
authored
Derive newtype ToHttpApiData in type Range (#1813)
* Derive newtype `ToHttpApiData` in type `Range` * add changelog * re-export `Servant.API.Range` * undo changes * undo re-export
1 parent d4625a3 commit 3cb70ef

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

changelog.d/pr-1813

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
synopsis: Use newtype deriving for ToHttpApiData in the type Range
2+
packages: servant
3+
prs: #1813
4+
description: {
5+
Use newtype deriving for ToHttpApiData in the type Range
6+
}

servant/src/Servant/API/Range.hs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import Servant.API
3939
-- Nothing
4040
newtype Range (min :: Nat) (max :: Nat) = MkRange {unRange :: Natural}
4141
deriving stock (Eq, Ord, Show, Generic)
42-
deriving newtype (Ix, ToJSON)
42+
deriving newtype (Ix, ToJSON, ToHttpApiData)
4343

4444
unsafeRange :: Natural -> Range min max
4545
unsafeRange = MkRange
@@ -62,9 +62,6 @@ instance (KnownNat min, KnownNat max) => FromJSON (Range min max) where
6262
n <- modifyFailure (const $ parseErrorMsg @min @max Proxy) $ parseJSON v
6363
maybe (fail $ parseErrorMsg @min @max Proxy) pure $ mkRange n
6464

65-
instance (KnownNat min, KnownNat max) => ToHttpApiData (Range min max) where
66-
toQueryParam = T.pack . show . unRange
67-
6865
instance (KnownNat min, KnownNat max) => FromHttpApiData (Range min max) where
6966
parseQueryParam v = do
7067
n <- first (const . T.pack $ parseErrorMsg @min @max Proxy) $ parseQueryParam v

0 commit comments

Comments
 (0)