1{-# LANGUAGE DeriveDataTypeable #-}
2{-# LANGUAGE GeneralizedNewtypeDeriving #-}
3{-# LANGUAGE OverloadedStrings #-}
4{-# LANGUAGE ScopedTypeVariables #-}
7-- Module : Network.Google.Data.Numeric
8-- Copyright : (c) 2015-2016 Brendan Hay <brendan.g.hay@gmail.com>
9-- License : Mozilla Public License, v. 2.0.
10-- Maintainer : Brendan Hay <brendan.g.hay@gmail.com>
11-- Stability : provisional
12-- Portability : non-portable (GHC extensions)
14module Network.Google.Data.Numeric
21import Data.Monoid ((<>))
23import Data.Text (Text)
24import qualified Data.Text as Text
25import qualified Data.Text.Lazy as LText
26import Data.Text.Lazy.Builder (Builder)
27import qualified Data.Text.Lazy.Builder as Build
28import qualified Data.Text.Lazy.Builder.Int as Build
29import qualified Data.Text.Read as Read
33newtype Nat = Nat { unNat :: Natural }
34 deriving (Eq, Ord, Enum, Show, Read, Num, Real, Integral, Data, Typeable)
36instance FromJSON Nat where
37 parseJSON = parseJSON >=> go
39 go n = case floatingOrInteger n of
40 Left (_ :: Double) -> fail (floatErr n)
42 | n < 0 -> fail (negateErr n)
43 | otherwise -> return . Nat $ fromInteger i
45 floatErr = mappend "Cannot convert float to Natural: " . show
46 negateErr = mappend "Cannot convert negative number to Natural: " . show
48instance ToJSON Nat where
49 toJSON = Number . flip scientific 0 . toInteger . unNat
51instance ToHttpApiData Nat where
52 toQueryParam = shortText . Build.decimal
54instance FromHttpApiData Nat where
56 case Read.decimal t of
57 Right (x, r) | Text.null r
59 _ -> Left ("Unable to parse natural from: " <> t)
61shortText :: Builder -> Text
62shortText = LText.toStrict . Build.toLazyTextWith 32
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list