{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DeriveAnyClass #-}
module Kubernetes.OpenAPI.CustomTypes where
import Data.Aeson (FromJSON, ToJSON)
import qualified Data.Aeson as A
import Data.Data (Typeable)
import Data.Text (Text)
import GHC.Base (mzero)
import GHC.Generics
data IntOrString
= IntOrStringS Text
| IntOrStringI Int
deriving (Int -> IntOrString -> ShowS
[IntOrString] -> ShowS
IntOrString -> String
(Int -> IntOrString -> ShowS)
-> (IntOrString -> String)
-> ([IntOrString] -> ShowS)
-> Show IntOrString
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [IntOrString] -> ShowS
$cshowList :: [IntOrString] -> ShowS
show :: IntOrString -> String
$cshow :: IntOrString -> String
showsPrec :: Int -> IntOrString -> ShowS
$cshowsPrec :: Int -> IntOrString -> ShowS
Show, IntOrString -> IntOrString -> Bool
(IntOrString -> IntOrString -> Bool)
-> (IntOrString -> IntOrString -> Bool) -> Eq IntOrString
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: IntOrString -> IntOrString -> Bool
$c/= :: IntOrString -> IntOrString -> Bool
== :: IntOrString -> IntOrString -> Bool
$c== :: IntOrString -> IntOrString -> Bool
Eq, Typeable, (forall x. IntOrString -> Rep IntOrString x)
-> (forall x. Rep IntOrString x -> IntOrString)
-> Generic IntOrString
forall x. Rep IntOrString x -> IntOrString
forall x. IntOrString -> Rep IntOrString x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep IntOrString x -> IntOrString
$cfrom :: forall x. IntOrString -> Rep IntOrString x
Generic)
instance FromJSON IntOrString where
parseJSON :: Value -> Parser IntOrString
parseJSON (A.String Text
t) = IntOrString -> Parser IntOrString
forall (m :: * -> *) a. Monad m => a -> m a
return (IntOrString -> Parser IntOrString)
-> IntOrString -> Parser IntOrString
forall a b. (a -> b) -> a -> b
$ Text -> IntOrString
IntOrStringS Text
t
parseJSON (A.Number Scientific
n) = IntOrString -> Parser IntOrString
forall (m :: * -> *) a. Monad m => a -> m a
return (IntOrString -> Parser IntOrString)
-> IntOrString -> Parser IntOrString
forall a b. (a -> b) -> a -> b
$ Int -> IntOrString
IntOrStringI (Scientific -> Int
forall a b. (RealFrac a, Integral b) => a -> b
round Scientific
n)
parseJSON Value
_ = Parser IntOrString
forall (m :: * -> *) a. MonadPlus m => m a
mzero
instance ToJSON IntOrString where
toJSON :: IntOrString -> Value
toJSON (IntOrStringS Text
t) = Text -> Value
A.String Text
t
toJSON (IntOrStringI Int
n) = Scientific -> Value
A.Number (Int -> Scientific
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
n)
newtype Quantity = Quantity { Quantity -> Text
unQuantity :: Text }
deriving (Int -> Quantity -> ShowS
[Quantity] -> ShowS
Quantity -> String
(Int -> Quantity -> ShowS)
-> (Quantity -> String) -> ([Quantity] -> ShowS) -> Show Quantity
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Quantity] -> ShowS
$cshowList :: [Quantity] -> ShowS
show :: Quantity -> String
$cshow :: Quantity -> String
showsPrec :: Int -> Quantity -> ShowS
$cshowsPrec :: Int -> Quantity -> ShowS
Show, Quantity -> Quantity -> Bool
(Quantity -> Quantity -> Bool)
-> (Quantity -> Quantity -> Bool) -> Eq Quantity
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Quantity -> Quantity -> Bool
$c/= :: Quantity -> Quantity -> Bool
== :: Quantity -> Quantity -> Bool
$c== :: Quantity -> Quantity -> Bool
Eq, Typeable, (forall x. Quantity -> Rep Quantity x)
-> (forall x. Rep Quantity x -> Quantity) -> Generic Quantity
forall x. Rep Quantity x -> Quantity
forall x. Quantity -> Rep Quantity x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Quantity x -> Quantity
$cfrom :: forall x. Quantity -> Rep Quantity x
Generic)
instance FromJSON Quantity where
parseJSON :: Value -> Parser Quantity
parseJSON (A.String Text
t) = Quantity -> Parser Quantity
forall (m :: * -> *) a. Monad m => a -> m a
return (Quantity -> Parser Quantity) -> Quantity -> Parser Quantity
forall a b. (a -> b) -> a -> b
$ Text -> Quantity
Quantity Text
t
parseJSON Value
_ = Parser Quantity
forall (m :: * -> *) a. MonadPlus m => m a
mzero
instance ToJSON Quantity where
toJSON :: Quantity -> Value
toJSON (Quantity Text
t) = Text -> Value
A.String Text
t