{-# LANGUAGE InstanceSigs      #-}
{-# LANGUAGE LambdaCase        #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards   #-}
{-# LANGUAGE TemplateHaskell   #-}
module Data.Schema.OpenAPI.Types where

import           Control.Applicative ((<|>))
import           Data.Aeson          (FromJSON (..), ToJSON (toJSON),
                                      Value (Array, Object, String), object,
                                      (.:), (.:?), (.=))
import qualified Data.Aeson.Key      as Key
import           Data.Aeson.KeyMap   (KeyMap)
import qualified Data.Aeson.KeyMap   as KeyMap
import           Data.Aeson.TH       (Options (..), defaultOptions, deriveJSON)
import           Data.Aeson.Types    (Pair, Parser, Value (Bool, Null, Number),
                                      parseEither, parseFail)
import           Data.HashMap.Strict (HashMap)
import           Data.Maybe          (catMaybes)
import           Data.Scientific     (Scientific)
import           Data.Text           (Text)
import qualified Data.Text           as Text
import qualified Data.Vector         as V
import           Data.Void           (Void)
import           Text.Casing         (camel)

data Tag = Tag
    { Tag -> Text
tagName        :: Text
    , Tag -> Text
tagDescription :: Text
    }
    deriving (Int -> Tag -> ShowS
[Tag] -> ShowS
Tag -> String
(Int -> Tag -> ShowS)
-> (Tag -> String) -> ([Tag] -> ShowS) -> Show Tag
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Tag] -> ShowS
$cshowList :: [Tag] -> ShowS
show :: Tag -> String
$cshow :: Tag -> String
showsPrec :: Int -> Tag -> ShowS
$cshowsPrec :: Int -> Tag -> ShowS
Show)
$(deriveJSON defaultOptions{fieldLabelModifier = camel . drop (Text.length "Tag")} ''Tag)

data Variable = Variable
    { Variable -> Text
variableDescription :: Text
    , Variable -> Text
variableDefault     :: Text
    }
    deriving (Int -> Variable -> ShowS
[Variable] -> ShowS
Variable -> String
(Int -> Variable -> ShowS)
-> (Variable -> String) -> ([Variable] -> ShowS) -> Show Variable
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Variable] -> ShowS
$cshowList :: [Variable] -> ShowS
show :: Variable -> String
$cshow :: Variable -> String
showsPrec :: Int -> Variable -> ShowS
$cshowsPrec :: Int -> Variable -> ShowS
Show)
$(deriveJSON defaultOptions{fieldLabelModifier = camel . drop (Text.length "Variable")} ''Variable)

data Server = Server
    { Server -> Text
serverUrl       :: Text
    , Server -> Maybe (HashMap Text Variable)
serverVariables :: Maybe (HashMap Text Variable)
    }
    deriving (Int -> Server -> ShowS
[Server] -> ShowS
Server -> String
(Int -> Server -> ShowS)
-> (Server -> String) -> ([Server] -> ShowS) -> Show Server
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Server] -> ShowS
$cshowList :: [Server] -> ShowS
show :: Server -> String
$cshow :: Server -> String
showsPrec :: Int -> Server -> ShowS
$cshowsPrec :: Int -> Server -> ShowS
Show)
$(deriveJSON defaultOptions{fieldLabelModifier = camel . drop (Text.length "Server")} ''Server)

data Url = Url
    { Url -> Text
urlDescription :: Text
    , Url -> Text
urlUrl         :: Text
    }
    deriving (Int -> Url -> ShowS
[Url] -> ShowS
Url -> String
(Int -> Url -> ShowS)
-> (Url -> String) -> ([Url] -> ShowS) -> Show Url
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Url] -> ShowS
$cshowList :: [Url] -> ShowS
show :: Url -> String
$cshow :: Url -> String
showsPrec :: Int -> Url -> ShowS
$cshowsPrec :: Int -> Url -> ShowS
Show)
$(deriveJSON defaultOptions{fieldLabelModifier = camel . drop (Text.length "Url")} ''Url)

data NameUrl = NameUrl
    { NameUrl -> Text
nameUrlName :: Text
    , NameUrl -> Text
nameUrlUrl  :: Text
    }
    deriving (Int -> NameUrl -> ShowS
[NameUrl] -> ShowS
NameUrl -> String
(Int -> NameUrl -> ShowS)
-> (NameUrl -> String) -> ([NameUrl] -> ShowS) -> Show NameUrl
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [NameUrl] -> ShowS
$cshowList :: [NameUrl] -> ShowS
show :: NameUrl -> String
$cshow :: NameUrl -> String
showsPrec :: Int -> NameUrl -> ShowS
$cshowsPrec :: Int -> NameUrl -> ShowS
Show)
$(deriveJSON defaultOptions{fieldLabelModifier = camel . drop (Text.length "NameUrl")} ''NameUrl)

data Info = Info
    { Info -> Text
infoVersion        :: Text
    , Info -> Text
infoTitle          :: Text
    , Info -> Text
infoDescription    :: Text
    , Info -> NameUrl
infoLicense        :: NameUrl
    , Info -> Text
infoTermsOfService :: Text
    , Info -> NameUrl
infoContact        :: NameUrl
    }
    deriving (Int -> Info -> ShowS
[Info] -> ShowS
Info -> String
(Int -> Info -> ShowS)
-> (Info -> String) -> ([Info] -> ShowS) -> Show Info
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Info] -> ShowS
$cshowList :: [Info] -> ShowS
show :: Info -> String
$cshow :: Info -> String
showsPrec :: Int -> Info -> ShowS
$cshowsPrec :: Int -> Info -> ShowS
Show)
$(deriveJSON defaultOptions{fieldLabelModifier = camel . drop (Text.length "Info")} ''Info)

data SchemaType
    = SchemaTypeEmpty SchemaEmpty
    | SchemaTypeObject SchemaObject
    | SchemaTypeArray SchemaArray
    | SchemaTypeString SchemaString
    | SchemaTypeBoolean SchemaBoolean
    | SchemaTypeInteger (SchemaNumber Int)
    | SchemaTypeNumber (SchemaNumber Scientific)
    | SchemaTypeAllOf SchemaQuantifier
    | SchemaTypeOneOf SchemaQuantifier
    | SchemaTypeAnyOf SchemaQuantifier
    deriving (Int -> SchemaType -> ShowS
[SchemaType] -> ShowS
SchemaType -> String
(Int -> SchemaType -> ShowS)
-> (SchemaType -> String)
-> ([SchemaType] -> ShowS)
-> Show SchemaType
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [SchemaType] -> ShowS
$cshowList :: [SchemaType] -> ShowS
show :: SchemaType -> String
$cshow :: SchemaType -> String
showsPrec :: Int -> SchemaType -> ShowS
$cshowsPrec :: Int -> SchemaType -> ShowS
Show)

parseSingleSchema :: KeyMap.KeyMap Value -> Value -> Parser SchemaType
parseSingleSchema :: KeyMap Value -> Value -> Parser SchemaType
parseSingleSchema KeyMap Value
mems = \case
    String Text
"array" -> (SchemaArray -> SchemaType)
-> Parser SchemaArray -> Parser SchemaType
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap SchemaArray -> SchemaType
SchemaTypeArray (Parser SchemaArray -> Parser SchemaType)
-> Parser SchemaArray -> Parser SchemaType
forall a b. (a -> b) -> a -> b
$
        SchemaCommon [Value] Value
-> SchemaType
-> Maybe Int
-> Maybe Int
-> Maybe [Text]
-> SchemaArray
SchemaArray
            (SchemaCommon [Value] Value
 -> SchemaType
 -> Maybe Int
 -> Maybe Int
 -> Maybe [Text]
 -> SchemaArray)
-> Parser (SchemaCommon [Value] Value)
-> Parser
     (SchemaType
      -> Maybe Int -> Maybe Int -> Maybe [Text] -> SchemaArray)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> KeyMap Value -> Parser (SchemaCommon [Value] Value)
forall a example.
(FromJSON a, FromJSON example) =>
KeyMap Value -> Parser (SchemaCommon a example)
decodeCommon KeyMap Value
mems
            Parser
  (SchemaType
   -> Maybe Int -> Maybe Int -> Maybe [Text] -> SchemaArray)
-> Parser SchemaType
-> Parser (Maybe Int -> Maybe Int -> Maybe [Text] -> SchemaArray)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> KeyMap Value
mems KeyMap Value -> Key -> Parser SchemaType
forall a. FromJSON a => KeyMap Value -> Key -> Parser a
.: Key
"items"
            Parser (Maybe Int -> Maybe Int -> Maybe [Text] -> SchemaArray)
-> Parser (Maybe Int)
-> Parser (Maybe Int -> Maybe [Text] -> SchemaArray)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> KeyMap Value
mems KeyMap Value -> Key -> Parser (Maybe Int)
forall a. FromJSON a => KeyMap Value -> Key -> Parser (Maybe a)
.:? Key
"minItems"
            Parser (Maybe Int -> Maybe [Text] -> SchemaArray)
-> Parser (Maybe Int) -> Parser (Maybe [Text] -> SchemaArray)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> KeyMap Value
mems KeyMap Value -> Key -> Parser (Maybe Int)
forall a. FromJSON a => KeyMap Value -> Key -> Parser (Maybe a)
.:? Key
"maxItems"
            Parser (Maybe [Text] -> SchemaArray)
-> Parser (Maybe [Text]) -> Parser SchemaArray
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> KeyMap Value
mems KeyMap Value -> Key -> Parser (Maybe [Text])
forall a. FromJSON a => KeyMap Value -> Key -> Parser (Maybe a)
.:? Key
"required"
    String Text
"object" -> (SchemaObject -> SchemaType)
-> Parser SchemaObject -> Parser SchemaType
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap SchemaObject -> SchemaType
SchemaTypeObject (Parser SchemaObject -> Parser SchemaType)
-> Parser SchemaObject -> Parser SchemaType
forall a b. (a -> b) -> a -> b
$
        SchemaCommon Void Value
-> Maybe (HashMap Text SchemaType)
-> Maybe AdditionalProperties
-> Maybe Int
-> Maybe [Text]
-> Maybe [SchemaType]
-> Maybe [SchemaType]
-> Maybe [SchemaType]
-> SchemaObject
SchemaObject
            (SchemaCommon Void Value
 -> Maybe (HashMap Text SchemaType)
 -> Maybe AdditionalProperties
 -> Maybe Int
 -> Maybe [Text]
 -> Maybe [SchemaType]
 -> Maybe [SchemaType]
 -> Maybe [SchemaType]
 -> SchemaObject)
-> Parser (SchemaCommon Void Value)
-> Parser
     (Maybe (HashMap Text SchemaType)
      -> Maybe AdditionalProperties
      -> Maybe Int
      -> Maybe [Text]
      -> Maybe [SchemaType]
      -> Maybe [SchemaType]
      -> Maybe [SchemaType]
      -> SchemaObject)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> KeyMap Value -> Parser (SchemaCommon Void Value)
forall a example.
(FromJSON a, FromJSON example) =>
KeyMap Value -> Parser (SchemaCommon a example)
decodeCommon KeyMap Value
mems
            Parser
  (Maybe (HashMap Text SchemaType)
   -> Maybe AdditionalProperties
   -> Maybe Int
   -> Maybe [Text]
   -> Maybe [SchemaType]
   -> Maybe [SchemaType]
   -> Maybe [SchemaType]
   -> SchemaObject)
-> Parser (Maybe (HashMap Text SchemaType))
-> Parser
     (Maybe AdditionalProperties
      -> Maybe Int
      -> Maybe [Text]
      -> Maybe [SchemaType]
      -> Maybe [SchemaType]
      -> Maybe [SchemaType]
      -> SchemaObject)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> KeyMap Value
mems KeyMap Value -> Key -> Parser (Maybe (HashMap Text SchemaType))
forall a. FromJSON a => KeyMap Value -> Key -> Parser (Maybe a)
.:? Key
"properties"
            Parser
  (Maybe AdditionalProperties
   -> Maybe Int
   -> Maybe [Text]
   -> Maybe [SchemaType]
   -> Maybe [SchemaType]
   -> Maybe [SchemaType]
   -> SchemaObject)
-> Parser (Maybe AdditionalProperties)
-> Parser
     (Maybe Int
      -> Maybe [Text]
      -> Maybe [SchemaType]
      -> Maybe [SchemaType]
      -> Maybe [SchemaType]
      -> SchemaObject)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> KeyMap Value
mems KeyMap Value -> Key -> Parser (Maybe AdditionalProperties)
forall a. FromJSON a => KeyMap Value -> Key -> Parser (Maybe a)
.:? Key
"additionalProperties"
            Parser
  (Maybe Int
   -> Maybe [Text]
   -> Maybe [SchemaType]
   -> Maybe [SchemaType]
   -> Maybe [SchemaType]
   -> SchemaObject)
-> Parser (Maybe Int)
-> Parser
     (Maybe [Text]
      -> Maybe [SchemaType]
      -> Maybe [SchemaType]
      -> Maybe [SchemaType]
      -> SchemaObject)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> KeyMap Value
mems KeyMap Value -> Key -> Parser (Maybe Int)
forall a. FromJSON a => KeyMap Value -> Key -> Parser (Maybe a)
.:? Key
"maxProperties"
            Parser
  (Maybe [Text]
   -> Maybe [SchemaType]
   -> Maybe [SchemaType]
   -> Maybe [SchemaType]
   -> SchemaObject)
-> Parser (Maybe [Text])
-> Parser
     (Maybe [SchemaType]
      -> Maybe [SchemaType] -> Maybe [SchemaType] -> SchemaObject)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> KeyMap Value
mems KeyMap Value -> Key -> Parser (Maybe [Text])
forall a. FromJSON a => KeyMap Value -> Key -> Parser (Maybe a)
.:? Key
"required"
            Parser
  (Maybe [SchemaType]
   -> Maybe [SchemaType] -> Maybe [SchemaType] -> SchemaObject)
-> Parser (Maybe [SchemaType])
-> Parser
     (Maybe [SchemaType] -> Maybe [SchemaType] -> SchemaObject)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> KeyMap Value
mems KeyMap Value -> Key -> Parser (Maybe [SchemaType])
forall a. FromJSON a => KeyMap Value -> Key -> Parser (Maybe a)
.:? Key
"oneOf"
            Parser (Maybe [SchemaType] -> Maybe [SchemaType] -> SchemaObject)
-> Parser (Maybe [SchemaType])
-> Parser (Maybe [SchemaType] -> SchemaObject)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> KeyMap Value
mems KeyMap Value -> Key -> Parser (Maybe [SchemaType])
forall a. FromJSON a => KeyMap Value -> Key -> Parser (Maybe a)
.:? Key
"allOf"
            Parser (Maybe [SchemaType] -> SchemaObject)
-> Parser (Maybe [SchemaType]) -> Parser SchemaObject
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> KeyMap Value
mems KeyMap Value -> Key -> Parser (Maybe [SchemaType])
forall a. FromJSON a => KeyMap Value -> Key -> Parser (Maybe a)
.:? Key
"anyOf"
    String Text
"string" -> (SchemaString -> SchemaType)
-> Parser SchemaString -> Parser SchemaType
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap SchemaString -> SchemaType
SchemaTypeString (Parser SchemaString -> Parser SchemaType)
-> Parser SchemaString -> Parser SchemaType
forall a b. (a -> b) -> a -> b
$
        SchemaCommon Text StringExample
-> Maybe [Text]
-> Maybe Text
-> Maybe Text
-> Maybe Int
-> Maybe Int
-> Maybe Bool
-> Maybe Int
-> SchemaString
SchemaString
            (SchemaCommon Text StringExample
 -> Maybe [Text]
 -> Maybe Text
 -> Maybe Text
 -> Maybe Int
 -> Maybe Int
 -> Maybe Bool
 -> Maybe Int
 -> SchemaString)
-> Parser (SchemaCommon Text StringExample)
-> Parser
     (Maybe [Text]
      -> Maybe Text
      -> Maybe Text
      -> Maybe Int
      -> Maybe Int
      -> Maybe Bool
      -> Maybe Int
      -> SchemaString)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Maybe Text
-> Maybe Text
-> Maybe Text
-> Maybe Bool
-> Maybe Bool
-> Maybe StringExample
-> Maybe Bool
-> SchemaCommon Text StringExample
forall a example.
Maybe Text
-> Maybe Text
-> Maybe a
-> Maybe Bool
-> Maybe Bool
-> Maybe example
-> Maybe Bool
-> SchemaCommon a example
SchemaCommon
                (Maybe Text
 -> Maybe Text
 -> Maybe Text
 -> Maybe Bool
 -> Maybe Bool
 -> Maybe StringExample
 -> Maybe Bool
 -> SchemaCommon Text StringExample)
-> Parser (Maybe Text)
-> Parser
     (Maybe Text
      -> Maybe Text
      -> Maybe Bool
      -> Maybe Bool
      -> Maybe StringExample
      -> Maybe Bool
      -> SchemaCommon Text StringExample)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> KeyMap Value
mems KeyMap Value -> Key -> Parser (Maybe Text)
forall a. FromJSON a => KeyMap Value -> Key -> Parser (Maybe a)
.:? Key
"description"
                Parser
  (Maybe Text
   -> Maybe Text
   -> Maybe Bool
   -> Maybe Bool
   -> Maybe StringExample
   -> Maybe Bool
   -> SchemaCommon Text StringExample)
-> Parser (Maybe Text)
-> Parser
     (Maybe Text
      -> Maybe Bool
      -> Maybe Bool
      -> Maybe StringExample
      -> Maybe Bool
      -> SchemaCommon Text StringExample)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> KeyMap Value
mems KeyMap Value -> Key -> Parser (Maybe Text)
forall a. FromJSON a => KeyMap Value -> Key -> Parser (Maybe a)
.:? Key
"title"
                Parser
  (Maybe Text
   -> Maybe Bool
   -> Maybe Bool
   -> Maybe StringExample
   -> Maybe Bool
   -> SchemaCommon Text StringExample)
-> Parser (Maybe Text)
-> Parser
     (Maybe Bool
      -> Maybe Bool
      -> Maybe StringExample
      -> Maybe Bool
      -> SchemaCommon Text StringExample)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (KeyMap Value
mems KeyMap Value -> Key -> Parser (Maybe Value)
forall a. FromJSON a => KeyMap Value -> Key -> Parser (Maybe a)
.:? Key
"default" Parser (Maybe Value)
-> (Maybe Value -> Parser (Maybe Text)) -> Parser (Maybe Text)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Value -> Parser Text) -> Maybe Value -> Parser (Maybe Text)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse Value -> Parser Text
toString)  -- sometimes a bool is used as default for string
                Parser
  (Maybe Bool
   -> Maybe Bool
   -> Maybe StringExample
   -> Maybe Bool
   -> SchemaCommon Text StringExample)
-> Parser (Maybe Bool)
-> Parser
     (Maybe Bool
      -> Maybe StringExample
      -> Maybe Bool
      -> SchemaCommon Text StringExample)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> KeyMap Value
mems KeyMap Value -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => KeyMap Value -> Key -> Parser (Maybe a)
.:? Key
"nullable"
                Parser
  (Maybe Bool
   -> Maybe StringExample
   -> Maybe Bool
   -> SchemaCommon Text StringExample)
-> Parser (Maybe Bool)
-> Parser
     (Maybe StringExample
      -> Maybe Bool -> SchemaCommon Text StringExample)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> KeyMap Value
mems KeyMap Value -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => KeyMap Value -> Key -> Parser (Maybe a)
.:? Key
"deprecated"
                Parser
  (Maybe StringExample
   -> Maybe Bool -> SchemaCommon Text StringExample)
-> Parser (Maybe StringExample)
-> Parser (Maybe Bool -> SchemaCommon Text StringExample)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> KeyMap Value
mems KeyMap Value -> Key -> Parser (Maybe StringExample)
forall a. FromJSON a => KeyMap Value -> Key -> Parser (Maybe a)
.:? Key
"example"
                Parser (Maybe Bool -> SchemaCommon Text StringExample)
-> Parser (Maybe Bool) -> Parser (SchemaCommon Text StringExample)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> KeyMap Value
mems KeyMap Value -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => KeyMap Value -> Key -> Parser (Maybe a)
.:? Key
"readOnly")
            Parser
  (Maybe [Text]
   -> Maybe Text
   -> Maybe Text
   -> Maybe Int
   -> Maybe Int
   -> Maybe Bool
   -> Maybe Int
   -> SchemaString)
-> Parser (Maybe [Text])
-> Parser
     (Maybe Text
      -> Maybe Text
      -> Maybe Int
      -> Maybe Int
      -> Maybe Bool
      -> Maybe Int
      -> SchemaString)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (([Maybe Text] -> [Text]) -> Maybe [Maybe Text] -> Maybe [Text]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [Maybe Text] -> [Text]
forall a. [Maybe a] -> [a]
catMaybes (Maybe [Maybe Text] -> Maybe [Text])
-> Parser (Maybe [Maybe Text]) -> Parser (Maybe [Text])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> KeyMap Value
mems KeyMap Value -> Key -> Parser (Maybe [Maybe Text])
forall a. FromJSON a => KeyMap Value -> Key -> Parser (Maybe a)
.:? Key
"enum")  -- we're skipping null and rely on nullable
            Parser
  (Maybe Text
   -> Maybe Text
   -> Maybe Int
   -> Maybe Int
   -> Maybe Bool
   -> Maybe Int
   -> SchemaString)
-> Parser (Maybe Text)
-> Parser
     (Maybe Text
      -> Maybe Int
      -> Maybe Int
      -> Maybe Bool
      -> Maybe Int
      -> SchemaString)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> KeyMap Value
mems KeyMap Value -> Key -> Parser (Maybe Text)
forall a. FromJSON a => KeyMap Value -> Key -> Parser (Maybe a)
.:? Key
"format"
            Parser
  (Maybe Text
   -> Maybe Int
   -> Maybe Int
   -> Maybe Bool
   -> Maybe Int
   -> SchemaString)
-> Parser (Maybe Text)
-> Parser
     (Maybe Int -> Maybe Int -> Maybe Bool -> Maybe Int -> SchemaString)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> KeyMap Value
mems KeyMap Value -> Key -> Parser (Maybe Text)
forall a. FromJSON a => KeyMap Value -> Key -> Parser (Maybe a)
.:? Key
"pattern"
            Parser
  (Maybe Int -> Maybe Int -> Maybe Bool -> Maybe Int -> SchemaString)
-> Parser (Maybe Int)
-> Parser (Maybe Int -> Maybe Bool -> Maybe Int -> SchemaString)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> KeyMap Value
mems KeyMap Value -> Key -> Parser (Maybe Int)
forall a. FromJSON a => KeyMap Value -> Key -> Parser (Maybe a)
.:? Key
"minLength"
            Parser (Maybe Int -> Maybe Bool -> Maybe Int -> SchemaString)
-> Parser (Maybe Int)
-> Parser (Maybe Bool -> Maybe Int -> SchemaString)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> KeyMap Value
mems KeyMap Value -> Key -> Parser (Maybe Int)
forall a. FromJSON a => KeyMap Value -> Key -> Parser (Maybe a)
.:? Key
"maxLength"
            Parser (Maybe Bool -> Maybe Int -> SchemaString)
-> Parser (Maybe Bool) -> Parser (Maybe Int -> SchemaString)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> KeyMap Value
mems KeyMap Value -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => KeyMap Value -> Key -> Parser (Maybe a)
.:? Key
"additionalProperties"
            Parser (Maybe Int -> SchemaString)
-> Parser (Maybe Int) -> Parser SchemaString
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> KeyMap Value
mems KeyMap Value -> Key -> Parser (Maybe Int)
forall a. FromJSON a => KeyMap Value -> Key -> Parser (Maybe a)
.:? Key
"minItems"
    String Text
"boolean" -> (SchemaBoolean -> SchemaType)
-> Parser SchemaBoolean -> Parser SchemaType
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap SchemaBoolean -> SchemaType
SchemaTypeBoolean (Parser SchemaBoolean -> Parser SchemaType)
-> Parser SchemaBoolean -> Parser SchemaType
forall a b. (a -> b) -> a -> b
$
        SchemaCommon Bool Bool -> SchemaBoolean
SchemaBoolean
            (SchemaCommon Bool Bool -> SchemaBoolean)
-> Parser (SchemaCommon Bool Bool) -> Parser SchemaBoolean
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> KeyMap Value -> Parser (SchemaCommon Bool Bool)
forall a example.
(FromJSON a, FromJSON example) =>
KeyMap Value -> Parser (SchemaCommon a example)
decodeCommon KeyMap Value
mems
    String Text
"integer" -> (SchemaNumber Int -> SchemaType)
-> Parser (SchemaNumber Int) -> Parser SchemaType
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap SchemaNumber Int -> SchemaType
SchemaTypeInteger (Parser (SchemaNumber Int) -> Parser SchemaType)
-> Parser (SchemaNumber Int) -> Parser SchemaType
forall a b. (a -> b) -> a -> b
$
        SchemaCommon Int Int
-> Maybe Text -> Maybe Int -> Maybe Int -> SchemaNumber Int
forall int.
SchemaCommon int int
-> Maybe Text -> Maybe int -> Maybe int -> SchemaNumber int
SchemaNumber
            (SchemaCommon Int Int
 -> Maybe Text -> Maybe Int -> Maybe Int -> SchemaNumber Int)
-> Parser (SchemaCommon Int Int)
-> Parser
     (Maybe Text -> Maybe Int -> Maybe Int -> SchemaNumber Int)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> KeyMap Value -> Parser (SchemaCommon Int Int)
forall a example.
(FromJSON a, FromJSON example) =>
KeyMap Value -> Parser (SchemaCommon a example)
decodeCommon KeyMap Value
mems
            Parser (Maybe Text -> Maybe Int -> Maybe Int -> SchemaNumber Int)
-> Parser (Maybe Text)
-> Parser (Maybe Int -> Maybe Int -> SchemaNumber Int)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> KeyMap Value
mems KeyMap Value -> Key -> Parser (Maybe Text)
forall a. FromJSON a => KeyMap Value -> Key -> Parser (Maybe a)
.:? Key
"format"
            Parser (Maybe Int -> Maybe Int -> SchemaNumber Int)
-> Parser (Maybe Int) -> Parser (Maybe Int -> SchemaNumber Int)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> KeyMap Value
mems KeyMap Value -> Key -> Parser (Maybe Int)
forall a. FromJSON a => KeyMap Value -> Key -> Parser (Maybe a)
.:? Key
"minimum"
            Parser (Maybe Int -> SchemaNumber Int)
-> Parser (Maybe Int) -> Parser (SchemaNumber Int)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> KeyMap Value
mems KeyMap Value -> Key -> Parser (Maybe Int)
forall a. FromJSON a => KeyMap Value -> Key -> Parser (Maybe a)
.:? Key
"maximum"
    String Text
"number" -> (SchemaNumber Scientific -> SchemaType)
-> Parser (SchemaNumber Scientific) -> Parser SchemaType
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap SchemaNumber Scientific -> SchemaType
SchemaTypeNumber (Parser (SchemaNumber Scientific) -> Parser SchemaType)
-> Parser (SchemaNumber Scientific) -> Parser SchemaType
forall a b. (a -> b) -> a -> b
$
        SchemaCommon Scientific Scientific
-> Maybe Text
-> Maybe Scientific
-> Maybe Scientific
-> SchemaNumber Scientific
forall int.
SchemaCommon int int
-> Maybe Text -> Maybe int -> Maybe int -> SchemaNumber int
SchemaNumber
            (SchemaCommon Scientific Scientific
 -> Maybe Text
 -> Maybe Scientific
 -> Maybe Scientific
 -> SchemaNumber Scientific)
-> Parser (SchemaCommon Scientific Scientific)
-> Parser
     (Maybe Text
      -> Maybe Scientific -> Maybe Scientific -> SchemaNumber Scientific)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> KeyMap Value -> Parser (SchemaCommon Scientific Scientific)
forall a example.
(FromJSON a, FromJSON example) =>
KeyMap Value -> Parser (SchemaCommon a example)
decodeCommon KeyMap Value
mems
            Parser
  (Maybe Text
   -> Maybe Scientific -> Maybe Scientific -> SchemaNumber Scientific)
-> Parser (Maybe Text)
-> Parser
     (Maybe Scientific -> Maybe Scientific -> SchemaNumber Scientific)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> KeyMap Value
mems KeyMap Value -> Key -> Parser (Maybe Text)
forall a. FromJSON a => KeyMap Value -> Key -> Parser (Maybe a)
.:? Key
"format"
            Parser
  (Maybe Scientific -> Maybe Scientific -> SchemaNumber Scientific)
-> Parser (Maybe Scientific)
-> Parser (Maybe Scientific -> SchemaNumber Scientific)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> KeyMap Value
mems KeyMap Value -> Key -> Parser (Maybe Scientific)
forall a. FromJSON a => KeyMap Value -> Key -> Parser (Maybe a)
.:? Key
"minimum"
            Parser (Maybe Scientific -> SchemaNumber Scientific)
-> Parser (Maybe Scientific) -> Parser (SchemaNumber Scientific)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> KeyMap Value
mems KeyMap Value -> Key -> Parser (Maybe Scientific)
forall a. FromJSON a => KeyMap Value -> Key -> Parser (Maybe a)
.:? Key
"maximum"
    Value
Null -> (SchemaEmpty -> SchemaType)
-> Parser SchemaEmpty -> Parser SchemaType
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap SchemaEmpty -> SchemaType
SchemaTypeEmpty (Parser SchemaEmpty -> Parser SchemaType)
-> Parser SchemaEmpty -> Parser SchemaType
forall a b. (a -> b) -> a -> b
$
        Maybe Bool -> Maybe [Text] -> SchemaEmpty
SchemaEmpty
            (Maybe Bool -> Maybe [Text] -> SchemaEmpty)
-> Parser (Maybe Bool) -> Parser (Maybe [Text] -> SchemaEmpty)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> KeyMap Value
mems KeyMap Value -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => KeyMap Value -> Key -> Parser (Maybe a)
.:? Key
"nullable"
            Parser (Maybe [Text] -> SchemaEmpty)
-> Parser (Maybe [Text]) -> Parser SchemaEmpty
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> KeyMap Value
mems KeyMap Value -> Key -> Parser (Maybe [Text])
forall a. FromJSON a => KeyMap Value -> Key -> Parser (Maybe a)
.:? Key
"required"
    Value
ty ->
        String -> Parser SchemaType
forall a. String -> Parser a
parseFail (String -> Parser SchemaType) -> String -> Parser SchemaType
forall a b. (a -> b) -> a -> b
$ Value -> String
forall a. Show a => a -> String
show Value
ty

  where
    toString :: Value -> Parser Text
    toString :: Value -> Parser Text
toString (String Text
str) = Text -> Parser Text
forall (f :: * -> *) a. Applicative f => a -> f a
pure Text
str
    toString (Bool Bool
True)  = Text -> Parser Text
forall (f :: * -> *) a. Applicative f => a -> f a
pure Text
"true"
    toString (Bool Bool
False) = Text -> Parser Text
forall (f :: * -> *) a. Applicative f => a -> f a
pure Text
"false"
    toString Value
val          = String -> Parser Text
forall a. String -> Parser a
parseFail (String -> Parser Text) -> String -> Parser Text
forall a b. (a -> b) -> a -> b
$ String
"invalid String value: " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> Value -> String
forall a. Show a => a -> String
show Value
val

parseInferredSchema :: KeyMap Value -> Maybe (Parser SchemaType)
parseInferredSchema :: KeyMap Value -> Maybe (Parser SchemaType)
parseInferredSchema KeyMap Value
mems
    -- Try one more time. Sometimes, there's only "properties", but the type is missing.
    | Key -> KeyMap Value -> Bool
forall a. Key -> KeyMap a -> Bool
KeyMap.member Key
"properties" KeyMap Value
mems = Parser SchemaType -> Maybe (Parser SchemaType)
forall a. a -> Maybe a
Just (Parser SchemaType -> Maybe (Parser SchemaType))
-> Parser SchemaType -> Maybe (Parser SchemaType)
forall a b. (a -> b) -> a -> b
$ KeyMap Value -> Value -> Parser SchemaType
parseSingleSchema KeyMap Value
mems (Text -> Value
String Text
"object")
    -- If there's "enum", maybe it's a string.
    | Key -> KeyMap Value -> Bool
forall a. Key -> KeyMap a -> Bool
KeyMap.member Key
"enum" KeyMap Value
mems = Parser SchemaType -> Maybe (Parser SchemaType)
forall a. a -> Maybe a
Just (Parser SchemaType -> Maybe (Parser SchemaType))
-> Parser SchemaType -> Maybe (Parser SchemaType)
forall a b. (a -> b) -> a -> b
$ KeyMap Value -> Value -> Parser SchemaType
parseSingleSchema KeyMap Value
mems (Text -> Value
String Text
"string")
    | Bool
otherwise = Maybe (Parser SchemaType)
forall a. Maybe a
Nothing


instance FromJSON SchemaType where
    parseJSON :: Value -> Parser SchemaType
parseJSON ob :: Value
ob@(Object KeyMap Value
mems)
        | KeyMap Value -> Bool
forall v. KeyMap v -> Bool
KeyMap.null KeyMap Value
mems = SchemaType -> Parser SchemaType
forall (f :: * -> *) a. Applicative f => a -> f a
pure (SchemaType -> Parser SchemaType)
-> (SchemaEmpty -> SchemaType) -> SchemaEmpty -> Parser SchemaType
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SchemaEmpty -> SchemaType
SchemaTypeEmpty (SchemaEmpty -> Parser SchemaType)
-> SchemaEmpty -> Parser SchemaType
forall a b. (a -> b) -> a -> b
$ Maybe Bool -> Maybe [Text] -> SchemaEmpty
SchemaEmpty Maybe Bool
forall a. Maybe a
Nothing Maybe [Text]
forall a. Maybe a
Nothing
        | Bool
otherwise =
            case (KeyMap Value -> Value -> Parser SchemaType
parseSingleSchema KeyMap Value
mems (Value -> Parser SchemaType)
-> Maybe Value -> Maybe (Parser SchemaType)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Key -> KeyMap Value -> Maybe Value
forall v. Key -> KeyMap v -> Maybe v
KeyMap.lookup Key
"type" KeyMap Value
mems)
                Maybe (Parser SchemaType)
-> Maybe (Parser SchemaType) -> Maybe (Parser SchemaType)
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> KeyMap Value -> Maybe (Parser SchemaType)
parseInferredSchema KeyMap Value
mems
                Maybe (Parser SchemaType)
-> Maybe (Parser SchemaType) -> Maybe (Parser SchemaType)
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> KeyMap Value -> Maybe (Parser SchemaType)
parseQuantifier KeyMap Value
mems of
                Just Parser SchemaType
tys -> Parser SchemaType
tys
                Maybe (Parser SchemaType)
Nothing
                    | Key -> KeyMap Value -> Bool
forall a. Key -> KeyMap a -> Bool
KeyMap.member Key
"nullable" KeyMap Value
mems Bool -> Bool -> Bool
|| Key -> KeyMap Value -> Bool
forall a. Key -> KeyMap a -> Bool
KeyMap.member Key
"required" KeyMap Value
mems ->
                        -- Type missing, nothing distinctive there, maybe empty?
                        KeyMap Value -> Value -> Parser SchemaType
parseSingleSchema KeyMap Value
mems Value
Null
                    | Bool
otherwise ->
                        String -> Parser SchemaType
forall a. String -> Parser a
parseFail (String -> Parser SchemaType) -> String -> Parser SchemaType
forall a b. (a -> b) -> a -> b
$ String
"invalid schema: " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> Value -> String
forall a. Show a => a -> String
show Value
ob
    parseJSON Value
x = String -> Parser SchemaType
forall a. String -> Parser a
parseFail (String -> Parser SchemaType) -> String -> Parser SchemaType
forall a b. (a -> b) -> a -> b
$ Value -> String
forall a. Show a => a -> String
show Value
x

instance ToJSON SchemaType where
    toJSON :: SchemaType -> Value
toJSON (SchemaTypeEmpty SchemaEmpty{Maybe Bool
Maybe [Text]
schemaEmptyRequired :: SchemaEmpty -> Maybe [Text]
schemaEmptyNullable :: SchemaEmpty -> Maybe Bool
schemaEmptyRequired :: Maybe [Text]
schemaEmptyNullable :: Maybe Bool
..}) = [Pair] -> Value
object
        [ Key
"nullable"                Key -> Maybe Bool -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Maybe Bool
schemaEmptyNullable
        , Key
"required"                Key -> Maybe [Text] -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Maybe [Text]
schemaEmptyRequired
        ]
    toJSON (SchemaTypeObject SchemaObject{Maybe Int
Maybe [Text]
Maybe [SchemaType]
Maybe (HashMap Text SchemaType)
Maybe AdditionalProperties
SchemaCommon Void Value
schemaObjectAnyOf :: SchemaObject -> Maybe [SchemaType]
schemaObjectAllOf :: SchemaObject -> Maybe [SchemaType]
schemaObjectOneOf :: SchemaObject -> Maybe [SchemaType]
schemaObjectRequired :: SchemaObject -> Maybe [Text]
schemaObjectMaxProperties :: SchemaObject -> Maybe Int
schemaObjectAdditionalProperties :: SchemaObject -> Maybe AdditionalProperties
schemaObjectProperties :: SchemaObject -> Maybe (HashMap Text SchemaType)
schemaObjectCommon :: SchemaObject -> SchemaCommon Void Value
schemaObjectAnyOf :: Maybe [SchemaType]
schemaObjectAllOf :: Maybe [SchemaType]
schemaObjectOneOf :: Maybe [SchemaType]
schemaObjectRequired :: Maybe [Text]
schemaObjectMaxProperties :: Maybe Int
schemaObjectAdditionalProperties :: Maybe AdditionalProperties
schemaObjectProperties :: Maybe (HashMap Text SchemaType)
schemaObjectCommon :: SchemaCommon Void Value
..}) =
        [Pair] -> Value
object ([Pair] -> Value) -> [Pair] -> Value
forall a b. (a -> b) -> a -> b
$ SchemaCommon Void Value -> [Pair]
forall a example.
(ToJSON a, ToJSON example) =>
SchemaCommon a example -> [Pair]
encodeCommon SchemaCommon Void Value
schemaObjectCommon [Pair] -> [Pair] -> [Pair]
forall a. [a] -> [a] -> [a]
++
        [ Key
"type"                    Key -> Value -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Text -> Value
String Text
"object"
        , Key
"properties"              Key -> Maybe (HashMap Text SchemaType) -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Maybe (HashMap Text SchemaType)
schemaObjectProperties
        , Key
"additionalProperties"    Key -> Maybe AdditionalProperties -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Maybe AdditionalProperties
schemaObjectAdditionalProperties
        , Key
"maxProperties"           Key -> Maybe Int -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Maybe Int
schemaObjectMaxProperties
        , Key
"required"                Key -> Maybe [Text] -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Maybe [Text]
schemaObjectRequired
        , Key
"oneOf"                   Key -> Maybe [SchemaType] -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Maybe [SchemaType]
schemaObjectOneOf
        , Key
"allOf"                   Key -> Maybe [SchemaType] -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Maybe [SchemaType]
schemaObjectAllOf
        , Key
"anyOf"                   Key -> Maybe [SchemaType] -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Maybe [SchemaType]
schemaObjectAnyOf
        ]
    toJSON (SchemaTypeArray SchemaArray{Maybe Int
Maybe [Text]
SchemaCommon [Value] Value
SchemaType
schemaArrayRequired :: SchemaArray -> Maybe [Text]
schemaArrayMaxItems :: SchemaArray -> Maybe Int
schemaArrayMinItems :: SchemaArray -> Maybe Int
schemaArrayItems :: SchemaArray -> SchemaType
schemaArrayCommon :: SchemaArray -> SchemaCommon [Value] Value
schemaArrayRequired :: Maybe [Text]
schemaArrayMaxItems :: Maybe Int
schemaArrayMinItems :: Maybe Int
schemaArrayItems :: SchemaType
schemaArrayCommon :: SchemaCommon [Value] Value
..}) =
        [Pair] -> Value
object ([Pair] -> Value) -> [Pair] -> Value
forall a b. (a -> b) -> a -> b
$ SchemaCommon [Value] Value -> [Pair]
forall a example.
(ToJSON a, ToJSON example) =>
SchemaCommon a example -> [Pair]
encodeCommon SchemaCommon [Value] Value
schemaArrayCommon [Pair] -> [Pair] -> [Pair]
forall a. [a] -> [a] -> [a]
++
        [ Key
"type"                    Key -> Value -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Text -> Value
String Text
"array"
        , Key
"items"                   Key -> SchemaType -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= SchemaType
schemaArrayItems
        , Key
"minItems"                Key -> Maybe Int -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Maybe Int
schemaArrayMinItems
        , Key
"maxItems"                Key -> Maybe Int -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Maybe Int
schemaArrayMaxItems
        , Key
"required"                Key -> Maybe [Text] -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Maybe [Text]
schemaArrayRequired
        ]
    toJSON (SchemaTypeString SchemaString{Maybe Bool
Maybe Int
Maybe [Text]
Maybe Text
SchemaCommon Text StringExample
schemaStringMinItems :: SchemaString -> Maybe Int
schemaStringAdditionalProperties :: SchemaString -> Maybe Bool
schemaStringMaxLength :: SchemaString -> Maybe Int
schemaStringMinLength :: SchemaString -> Maybe Int
schemaStringPattern :: SchemaString -> Maybe Text
schemaStringFormat :: SchemaString -> Maybe Text
schemaStringEnum :: SchemaString -> Maybe [Text]
schemaStringCommon :: SchemaString -> SchemaCommon Text StringExample
schemaStringMinItems :: Maybe Int
schemaStringAdditionalProperties :: Maybe Bool
schemaStringMaxLength :: Maybe Int
schemaStringMinLength :: Maybe Int
schemaStringPattern :: Maybe Text
schemaStringFormat :: Maybe Text
schemaStringEnum :: Maybe [Text]
schemaStringCommon :: SchemaCommon Text StringExample
..}) =
        [Pair] -> Value
object ([Pair] -> Value) -> [Pair] -> Value
forall a b. (a -> b) -> a -> b
$ SchemaCommon Text StringExample -> [Pair]
forall a example.
(ToJSON a, ToJSON example) =>
SchemaCommon a example -> [Pair]
encodeCommon SchemaCommon Text StringExample
schemaStringCommon [Pair] -> [Pair] -> [Pair]
forall a. [a] -> [a] -> [a]
++
        [ Key
"type"                    Key -> Value -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Text -> Value
String Text
"string"
        , Key
"enum"                    Key -> Maybe [Text] -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Maybe [Text]
schemaStringEnum
        , Key
"format"                  Key -> Maybe Text -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Maybe Text
schemaStringFormat
        , Key
"pattern"                 Key -> Maybe Text -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Maybe Text
schemaStringPattern
        , Key
"minLength"               Key -> Maybe Int -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Maybe Int
schemaStringMinLength
        , Key
"maxLength"               Key -> Maybe Int -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Maybe Int
schemaStringMaxLength
        , Key
"additionalProperties"    Key -> Maybe Bool -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Maybe Bool
schemaStringAdditionalProperties
        , Key
"minItems"                Key -> Maybe Int -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Maybe Int
schemaStringMinItems
        ]
    toJSON (SchemaTypeBoolean SchemaBoolean{SchemaCommon Bool Bool
schemaBooleanCommon :: SchemaBoolean -> SchemaCommon Bool Bool
schemaBooleanCommon :: SchemaCommon Bool Bool
..}) =
        [Pair] -> Value
object ([Pair] -> Value) -> [Pair] -> Value
forall a b. (a -> b) -> a -> b
$ SchemaCommon Bool Bool -> [Pair]
forall a example.
(ToJSON a, ToJSON example) =>
SchemaCommon a example -> [Pair]
encodeCommon SchemaCommon Bool Bool
schemaBooleanCommon [Pair] -> [Pair] -> [Pair]
forall a. [a] -> [a] -> [a]
++
        [ Key
"type"                    Key -> Value -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Text -> Value
String Text
"boolean"
        ]
    toJSON (SchemaTypeInteger SchemaNumber{Maybe Int
Maybe Text
SchemaCommon Int Int
schemaNumberMaximum :: forall int. SchemaNumber int -> Maybe int
schemaNumberMinimum :: forall int. SchemaNumber int -> Maybe int
schemaNumberFormat :: forall int. SchemaNumber int -> Maybe Text
schemaNumberCommon :: forall int. SchemaNumber int -> SchemaCommon int int
schemaNumberMaximum :: Maybe Int
schemaNumberMinimum :: Maybe Int
schemaNumberFormat :: Maybe Text
schemaNumberCommon :: SchemaCommon Int Int
..}) =
        [Pair] -> Value
object ([Pair] -> Value) -> [Pair] -> Value
forall a b. (a -> b) -> a -> b
$ SchemaCommon Int Int -> [Pair]
forall a example.
(ToJSON a, ToJSON example) =>
SchemaCommon a example -> [Pair]
encodeCommon SchemaCommon Int Int
schemaNumberCommon [Pair] -> [Pair] -> [Pair]
forall a. [a] -> [a] -> [a]
++
        [ Key
"type"                    Key -> Value -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Text -> Value
String Text
"integer"
        , Key
"format"                  Key -> Maybe Text -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Maybe Text
schemaNumberFormat
        , Key
"maximum"                 Key -> Maybe Int -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Maybe Int
schemaNumberMaximum
        , Key
"minimum"                 Key -> Maybe Int -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Maybe Int
schemaNumberMinimum
        ]
    toJSON (SchemaTypeNumber SchemaNumber{Maybe Scientific
Maybe Text
SchemaCommon Scientific Scientific
schemaNumberMaximum :: Maybe Scientific
schemaNumberMinimum :: Maybe Scientific
schemaNumberFormat :: Maybe Text
schemaNumberCommon :: SchemaCommon Scientific Scientific
schemaNumberMaximum :: forall int. SchemaNumber int -> Maybe int
schemaNumberMinimum :: forall int. SchemaNumber int -> Maybe int
schemaNumberFormat :: forall int. SchemaNumber int -> Maybe Text
schemaNumberCommon :: forall int. SchemaNumber int -> SchemaCommon int int
..}) =
        [Pair] -> Value
object ([Pair] -> Value) -> [Pair] -> Value
forall a b. (a -> b) -> a -> b
$ SchemaCommon Scientific Scientific -> [Pair]
forall a example.
(ToJSON a, ToJSON example) =>
SchemaCommon a example -> [Pair]
encodeCommon SchemaCommon Scientific Scientific
schemaNumberCommon [Pair] -> [Pair] -> [Pair]
forall a. [a] -> [a] -> [a]
++
        [ Key
"type"                    Key -> Value -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Text -> Value
String Text
"number"
        , Key
"format"                  Key -> Maybe Text -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Maybe Text
schemaNumberFormat
        , Key
"maximum"                 Key -> Maybe Scientific -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Maybe Scientific
schemaNumberMaximum
        , Key
"minimum"                 Key -> Maybe Scientific -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Maybe Scientific
schemaNumberMinimum
        ]
    toJSON (SchemaTypeAllOf SchemaQuantifier{[SchemaType]
SchemaCommon Void Void
schemaQuantifierSchemas :: SchemaQuantifier -> [SchemaType]
schemaQuantifierCommon :: SchemaQuantifier -> SchemaCommon Void Void
schemaQuantifierSchemas :: [SchemaType]
schemaQuantifierCommon :: SchemaCommon Void Void
..}) =
        [Pair] -> Value
object ([Pair] -> Value) -> [Pair] -> Value
forall a b. (a -> b) -> a -> b
$ SchemaCommon Void Void -> [Pair]
forall a example.
(ToJSON a, ToJSON example) =>
SchemaCommon a example -> [Pair]
encodeCommon SchemaCommon Void Void
schemaQuantifierCommon [Pair] -> [Pair] -> [Pair]
forall a. [a] -> [a] -> [a]
++
        [ Key
"allOf"                   Key -> [SchemaType] -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= [SchemaType]
schemaQuantifierSchemas
        ]
    toJSON (SchemaTypeAnyOf SchemaQuantifier{[SchemaType]
SchemaCommon Void Void
schemaQuantifierSchemas :: [SchemaType]
schemaQuantifierCommon :: SchemaCommon Void Void
schemaQuantifierSchemas :: SchemaQuantifier -> [SchemaType]
schemaQuantifierCommon :: SchemaQuantifier -> SchemaCommon Void Void
..}) =
        [Pair] -> Value
object ([Pair] -> Value) -> [Pair] -> Value
forall a b. (a -> b) -> a -> b
$ SchemaCommon Void Void -> [Pair]
forall a example.
(ToJSON a, ToJSON example) =>
SchemaCommon a example -> [Pair]
encodeCommon SchemaCommon Void Void
schemaQuantifierCommon [Pair] -> [Pair] -> [Pair]
forall a. [a] -> [a] -> [a]
++
        [ Key
"anyOf"                   Key -> [SchemaType] -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= [SchemaType]
schemaQuantifierSchemas
        ]
    toJSON (SchemaTypeOneOf SchemaQuantifier{[SchemaType]
SchemaCommon Void Void
schemaQuantifierSchemas :: [SchemaType]
schemaQuantifierCommon :: SchemaCommon Void Void
schemaQuantifierSchemas :: SchemaQuantifier -> [SchemaType]
schemaQuantifierCommon :: SchemaQuantifier -> SchemaCommon Void Void
..}) =
        [Pair] -> Value
object ([Pair] -> Value) -> [Pair] -> Value
forall a b. (a -> b) -> a -> b
$ SchemaCommon Void Void -> [Pair]
forall a example.
(ToJSON a, ToJSON example) =>
SchemaCommon a example -> [Pair]
encodeCommon SchemaCommon Void Void
schemaQuantifierCommon [Pair] -> [Pair] -> [Pair]
forall a. [a] -> [a] -> [a]
++
        [ Key
"oneOf"                   Key -> [SchemaType] -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= [SchemaType]
schemaQuantifierSchemas
        ]

data SchemaCommon a example = SchemaCommon
    { SchemaCommon a example -> Maybe Text
schemaCommonDescription :: Maybe Text
    , SchemaCommon a example -> Maybe Text
schemaCommonTitle       :: Maybe Text
    , SchemaCommon a example -> Maybe a
schemaCommonDefault     :: Maybe a
    , SchemaCommon a example -> Maybe Bool
schemaCommonNullable    :: Maybe Bool
    , SchemaCommon a example -> Maybe Bool
schemaCommonDeprecated  :: Maybe Bool
    , SchemaCommon a example -> Maybe example
schemaCommonExample     :: Maybe example
    , SchemaCommon a example -> Maybe Bool
schemaCommonReadOnly    :: Maybe Bool
    }
    deriving (Int -> SchemaCommon a example -> ShowS
[SchemaCommon a example] -> ShowS
SchemaCommon a example -> String
(Int -> SchemaCommon a example -> ShowS)
-> (SchemaCommon a example -> String)
-> ([SchemaCommon a example] -> ShowS)
-> Show (SchemaCommon a example)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall a example.
(Show a, Show example) =>
Int -> SchemaCommon a example -> ShowS
forall a example.
(Show a, Show example) =>
[SchemaCommon a example] -> ShowS
forall a example.
(Show a, Show example) =>
SchemaCommon a example -> String
showList :: [SchemaCommon a example] -> ShowS
$cshowList :: forall a example.
(Show a, Show example) =>
[SchemaCommon a example] -> ShowS
show :: SchemaCommon a example -> String
$cshow :: forall a example.
(Show a, Show example) =>
SchemaCommon a example -> String
showsPrec :: Int -> SchemaCommon a example -> ShowS
$cshowsPrec :: forall a example.
(Show a, Show example) =>
Int -> SchemaCommon a example -> ShowS
Show)

encodeCommon :: (ToJSON a, ToJSON example) => SchemaCommon a example -> [Pair]
encodeCommon :: SchemaCommon a example -> [Pair]
encodeCommon SchemaCommon{Maybe a
Maybe example
Maybe Bool
Maybe Text
schemaCommonReadOnly :: Maybe Bool
schemaCommonExample :: Maybe example
schemaCommonDeprecated :: Maybe Bool
schemaCommonNullable :: Maybe Bool
schemaCommonDefault :: Maybe a
schemaCommonTitle :: Maybe Text
schemaCommonDescription :: Maybe Text
schemaCommonReadOnly :: forall a example. SchemaCommon a example -> Maybe Bool
schemaCommonExample :: forall a example. SchemaCommon a example -> Maybe example
schemaCommonDeprecated :: forall a example. SchemaCommon a example -> Maybe Bool
schemaCommonNullable :: forall a example. SchemaCommon a example -> Maybe Bool
schemaCommonDefault :: forall a example. SchemaCommon a example -> Maybe a
schemaCommonTitle :: forall a example. SchemaCommon a example -> Maybe Text
schemaCommonDescription :: forall a example. SchemaCommon a example -> Maybe Text
..} =
    [ Key
"description" Key -> Maybe Text -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Maybe Text
schemaCommonDescription
    , Key
"title"       Key -> Maybe Text -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Maybe Text
schemaCommonTitle
    , Key
"default"     Key -> Maybe a -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Maybe a
schemaCommonDefault
    , Key
"nullable"    Key -> Maybe Bool -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Maybe Bool
schemaCommonNullable
    , Key
"deprecated"  Key -> Maybe Bool -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Maybe Bool
schemaCommonDeprecated
    , Key
"example"     Key -> Maybe example -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Maybe example
schemaCommonExample
    , Key
"readOnly"    Key -> Maybe Bool -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Maybe Bool
schemaCommonReadOnly
    ]

decodeCommon :: (FromJSON a, FromJSON example) => KeyMap Value -> Parser (SchemaCommon a example)
decodeCommon :: KeyMap Value -> Parser (SchemaCommon a example)
decodeCommon KeyMap Value
mems =
    Maybe Text
-> Maybe Text
-> Maybe a
-> Maybe Bool
-> Maybe Bool
-> Maybe example
-> Maybe Bool
-> SchemaCommon a example
forall a example.
Maybe Text
-> Maybe Text
-> Maybe a
-> Maybe Bool
-> Maybe Bool
-> Maybe example
-> Maybe Bool
-> SchemaCommon a example
SchemaCommon
        (Maybe Text
 -> Maybe Text
 -> Maybe a
 -> Maybe Bool
 -> Maybe Bool
 -> Maybe example
 -> Maybe Bool
 -> SchemaCommon a example)
-> Parser (Maybe Text)
-> Parser
     (Maybe Text
      -> Maybe a
      -> Maybe Bool
      -> Maybe Bool
      -> Maybe example
      -> Maybe Bool
      -> SchemaCommon a example)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> KeyMap Value
mems KeyMap Value -> Key -> Parser (Maybe Text)
forall a. FromJSON a => KeyMap Value -> Key -> Parser (Maybe a)
.:? Key
"description"
        Parser
  (Maybe Text
   -> Maybe a
   -> Maybe Bool
   -> Maybe Bool
   -> Maybe example
   -> Maybe Bool
   -> SchemaCommon a example)
-> Parser (Maybe Text)
-> Parser
     (Maybe a
      -> Maybe Bool
      -> Maybe Bool
      -> Maybe example
      -> Maybe Bool
      -> SchemaCommon a example)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> KeyMap Value
mems KeyMap Value -> Key -> Parser (Maybe Text)
forall a. FromJSON a => KeyMap Value -> Key -> Parser (Maybe a)
.:? Key
"title"
        Parser
  (Maybe a
   -> Maybe Bool
   -> Maybe Bool
   -> Maybe example
   -> Maybe Bool
   -> SchemaCommon a example)
-> Parser (Maybe a)
-> Parser
     (Maybe Bool
      -> Maybe Bool
      -> Maybe example
      -> Maybe Bool
      -> SchemaCommon a example)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> KeyMap Value
mems KeyMap Value -> Key -> Parser (Maybe a)
forall a. FromJSON a => KeyMap Value -> Key -> Parser (Maybe a)
.:? Key
"default"
        Parser
  (Maybe Bool
   -> Maybe Bool
   -> Maybe example
   -> Maybe Bool
   -> SchemaCommon a example)
-> Parser (Maybe Bool)
-> Parser
     (Maybe Bool
      -> Maybe example -> Maybe Bool -> SchemaCommon a example)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> KeyMap Value
mems KeyMap Value -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => KeyMap Value -> Key -> Parser (Maybe a)
.:? Key
"nullable"
        Parser
  (Maybe Bool
   -> Maybe example -> Maybe Bool -> SchemaCommon a example)
-> Parser (Maybe Bool)
-> Parser (Maybe example -> Maybe Bool -> SchemaCommon a example)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> KeyMap Value
mems KeyMap Value -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => KeyMap Value -> Key -> Parser (Maybe a)
.:? Key
"deprecated"
        Parser (Maybe example -> Maybe Bool -> SchemaCommon a example)
-> Parser (Maybe example)
-> Parser (Maybe Bool -> SchemaCommon a example)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> KeyMap Value
mems KeyMap Value -> Key -> Parser (Maybe example)
forall a. FromJSON a => KeyMap Value -> Key -> Parser (Maybe a)
.:? Key
"example"
        Parser (Maybe Bool -> SchemaCommon a example)
-> Parser (Maybe Bool) -> Parser (SchemaCommon a example)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> KeyMap Value
mems KeyMap Value -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => KeyMap Value -> Key -> Parser (Maybe a)
.:? Key
"readOnly"

data SchemaEmpty = SchemaEmpty
    { SchemaEmpty -> Maybe Bool
schemaEmptyNullable :: Maybe Bool
    , SchemaEmpty -> Maybe [Text]
schemaEmptyRequired :: Maybe [Text]
    }
    deriving (Int -> SchemaEmpty -> ShowS
[SchemaEmpty] -> ShowS
SchemaEmpty -> String
(Int -> SchemaEmpty -> ShowS)
-> (SchemaEmpty -> String)
-> ([SchemaEmpty] -> ShowS)
-> Show SchemaEmpty
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [SchemaEmpty] -> ShowS
$cshowList :: [SchemaEmpty] -> ShowS
show :: SchemaEmpty -> String
$cshow :: SchemaEmpty -> String
showsPrec :: Int -> SchemaEmpty -> ShowS
$cshowsPrec :: Int -> SchemaEmpty -> ShowS
Show)

data SchemaQuantifier = SchemaQuantifier
    { SchemaQuantifier -> SchemaCommon Void Void
schemaQuantifierCommon  :: SchemaCommon Void Void
    , SchemaQuantifier -> [SchemaType]
schemaQuantifierSchemas :: [SchemaType]
    }
    deriving (Int -> SchemaQuantifier -> ShowS
[SchemaQuantifier] -> ShowS
SchemaQuantifier -> String
(Int -> SchemaQuantifier -> ShowS)
-> (SchemaQuantifier -> String)
-> ([SchemaQuantifier] -> ShowS)
-> Show SchemaQuantifier
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [SchemaQuantifier] -> ShowS
$cshowList :: [SchemaQuantifier] -> ShowS
show :: SchemaQuantifier -> String
$cshow :: SchemaQuantifier -> String
showsPrec :: Int -> SchemaQuantifier -> ShowS
$cshowsPrec :: Int -> SchemaQuantifier -> ShowS
Show)

parseQuantifier :: KeyMap Value -> Maybe (Parser SchemaType)
parseQuantifier :: KeyMap Value -> Maybe (Parser SchemaType)
parseQuantifier KeyMap Value
mems =
    ((SchemaQuantifier -> SchemaType)
-> Parser SchemaQuantifier -> Parser SchemaType
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap SchemaQuantifier -> SchemaType
SchemaTypeAllOf (Parser SchemaQuantifier -> Parser SchemaType)
-> (Value -> Parser SchemaQuantifier) -> Value -> Parser SchemaType
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Parser [SchemaType] -> Parser SchemaQuantifier
parseRest (Parser [SchemaType] -> Parser SchemaQuantifier)
-> (Value -> Parser [SchemaType])
-> Value
-> Parser SchemaQuantifier
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Value -> Parser [SchemaType]
forall a. FromJSON a => Value -> Parser a
parseJSON (Value -> Parser SchemaType)
-> Maybe Value -> Maybe (Parser SchemaType)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Key -> KeyMap Value -> Maybe Value
forall v. Key -> KeyMap v -> Maybe v
KeyMap.lookup Key
"allOf" KeyMap Value
mems)
    Maybe (Parser SchemaType)
-> Maybe (Parser SchemaType) -> Maybe (Parser SchemaType)
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> ((SchemaQuantifier -> SchemaType)
-> Parser SchemaQuantifier -> Parser SchemaType
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap SchemaQuantifier -> SchemaType
SchemaTypeOneOf (Parser SchemaQuantifier -> Parser SchemaType)
-> (Value -> Parser SchemaQuantifier) -> Value -> Parser SchemaType
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Parser [SchemaType] -> Parser SchemaQuantifier
parseRest (Parser [SchemaType] -> Parser SchemaQuantifier)
-> (Value -> Parser [SchemaType])
-> Value
-> Parser SchemaQuantifier
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Value -> Parser [SchemaType]
forall a. FromJSON a => Value -> Parser a
parseJSON (Value -> Parser SchemaType)
-> Maybe Value -> Maybe (Parser SchemaType)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Key -> KeyMap Value -> Maybe Value
forall v. Key -> KeyMap v -> Maybe v
KeyMap.lookup Key
"oneOf" KeyMap Value
mems)
    Maybe (Parser SchemaType)
-> Maybe (Parser SchemaType) -> Maybe (Parser SchemaType)
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> ((SchemaQuantifier -> SchemaType)
-> Parser SchemaQuantifier -> Parser SchemaType
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap SchemaQuantifier -> SchemaType
SchemaTypeAnyOf (Parser SchemaQuantifier -> Parser SchemaType)
-> (Value -> Parser SchemaQuantifier) -> Value -> Parser SchemaType
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Parser [SchemaType] -> Parser SchemaQuantifier
parseRest (Parser [SchemaType] -> Parser SchemaQuantifier)
-> (Value -> Parser [SchemaType])
-> Value
-> Parser SchemaQuantifier
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Value -> Parser [SchemaType]
forall a. FromJSON a => Value -> Parser a
parseJSON (Value -> Parser SchemaType)
-> Maybe Value -> Maybe (Parser SchemaType)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Key -> KeyMap Value -> Maybe Value
forall v. Key -> KeyMap v -> Maybe v
KeyMap.lookup Key
"anyOf" KeyMap Value
mems)
  where
    parseRest :: Parser [SchemaType] -> Parser SchemaQuantifier
    parseRest :: Parser [SchemaType] -> Parser SchemaQuantifier
parseRest = (SchemaCommon Void Void -> [SchemaType] -> SchemaQuantifier
SchemaQuantifier (SchemaCommon Void Void -> [SchemaType] -> SchemaQuantifier)
-> Parser (SchemaCommon Void Void)
-> Parser ([SchemaType] -> SchemaQuantifier)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> KeyMap Value -> Parser (SchemaCommon Void Void)
forall a example.
(FromJSON a, FromJSON example) =>
KeyMap Value -> Parser (SchemaCommon a example)
decodeCommon KeyMap Value
mems Parser ([SchemaType] -> SchemaQuantifier)
-> Parser [SchemaType] -> Parser SchemaQuantifier
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*>)

data AdditionalProperties
    = AdditionalPropertiesBool Bool
    | AdditionalPropertiesSchema SchemaType
    deriving (Int -> AdditionalProperties -> ShowS
[AdditionalProperties] -> ShowS
AdditionalProperties -> String
(Int -> AdditionalProperties -> ShowS)
-> (AdditionalProperties -> String)
-> ([AdditionalProperties] -> ShowS)
-> Show AdditionalProperties
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [AdditionalProperties] -> ShowS
$cshowList :: [AdditionalProperties] -> ShowS
show :: AdditionalProperties -> String
$cshow :: AdditionalProperties -> String
showsPrec :: Int -> AdditionalProperties -> ShowS
$cshowsPrec :: Int -> AdditionalProperties -> ShowS
Show)

instance FromJSON AdditionalProperties where
    parseJSON :: Value -> Parser AdditionalProperties
    parseJSON :: Value -> Parser AdditionalProperties
parseJSON Value
val =
        (Bool -> AdditionalProperties
AdditionalPropertiesBool (Bool -> AdditionalProperties)
-> Parser Bool -> Parser AdditionalProperties
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Value -> Parser Bool
forall a. FromJSON a => Value -> Parser a
parseJSON Value
val) Parser AdditionalProperties
-> Parser AdditionalProperties -> Parser AdditionalProperties
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
        (SchemaType -> AdditionalProperties
AdditionalPropertiesSchema (SchemaType -> AdditionalProperties)
-> Parser SchemaType -> Parser AdditionalProperties
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Value -> Parser SchemaType
forall a. FromJSON a => Value -> Parser a
parseJSON Value
val)

instance ToJSON AdditionalProperties where
    toJSON :: AdditionalProperties -> Value
toJSON (AdditionalPropertiesBool Bool
str)   = Bool -> Value
forall a. ToJSON a => a -> Value
toJSON Bool
str
    toJSON (AdditionalPropertiesSchema SchemaType
arr) = SchemaType -> Value
forall a. ToJSON a => a -> Value
toJSON SchemaType
arr

data SchemaObject = SchemaObject
    { SchemaObject -> SchemaCommon Void Value
schemaObjectCommon               :: SchemaCommon Void Value
    , SchemaObject -> Maybe (HashMap Text SchemaType)
schemaObjectProperties           :: Maybe (HashMap Text SchemaType)
    , SchemaObject -> Maybe AdditionalProperties
schemaObjectAdditionalProperties :: Maybe AdditionalProperties
    , SchemaObject -> Maybe Int
schemaObjectMaxProperties        :: Maybe Int
    , SchemaObject -> Maybe [Text]
schemaObjectRequired             :: Maybe [Text]
    , SchemaObject -> Maybe [SchemaType]
schemaObjectOneOf                :: Maybe [SchemaType]
    , SchemaObject -> Maybe [SchemaType]
schemaObjectAllOf                :: Maybe [SchemaType]
    , SchemaObject -> Maybe [SchemaType]
schemaObjectAnyOf                :: Maybe [SchemaType]
    }
    deriving (Int -> SchemaObject -> ShowS
[SchemaObject] -> ShowS
SchemaObject -> String
(Int -> SchemaObject -> ShowS)
-> (SchemaObject -> String)
-> ([SchemaObject] -> ShowS)
-> Show SchemaObject
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [SchemaObject] -> ShowS
$cshowList :: [SchemaObject] -> ShowS
show :: SchemaObject -> String
$cshow :: SchemaObject -> String
showsPrec :: Int -> SchemaObject -> ShowS
$cshowsPrec :: Int -> SchemaObject -> ShowS
Show)

data SchemaArray = SchemaArray
    { SchemaArray -> SchemaCommon [Value] Value
schemaArrayCommon   :: SchemaCommon [Value] Value -- should be [Value], but ghes has many bugs
    , SchemaArray -> SchemaType
schemaArrayItems    :: SchemaType
    , SchemaArray -> Maybe Int
schemaArrayMinItems :: Maybe Int
    , SchemaArray -> Maybe Int
schemaArrayMaxItems :: Maybe Int
    , SchemaArray -> Maybe [Text]
schemaArrayRequired :: Maybe [Text]
    }
    deriving (Int -> SchemaArray -> ShowS
[SchemaArray] -> ShowS
SchemaArray -> String
(Int -> SchemaArray -> ShowS)
-> (SchemaArray -> String)
-> ([SchemaArray] -> ShowS)
-> Show SchemaArray
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [SchemaArray] -> ShowS
$cshowList :: [SchemaArray] -> ShowS
show :: SchemaArray -> String
$cshow :: SchemaArray -> String
showsPrec :: Int -> SchemaArray -> ShowS
$cshowsPrec :: Int -> SchemaArray -> ShowS
Show)

data StringExample
    = StringExampleString Text
    | StringExampleArray [Text]
    deriving (Int -> StringExample -> ShowS
[StringExample] -> ShowS
StringExample -> String
(Int -> StringExample -> ShowS)
-> (StringExample -> String)
-> ([StringExample] -> ShowS)
-> Show StringExample
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [StringExample] -> ShowS
$cshowList :: [StringExample] -> ShowS
show :: StringExample -> String
$cshow :: StringExample -> String
showsPrec :: Int -> StringExample -> ShowS
$cshowsPrec :: Int -> StringExample -> ShowS
Show)

instance FromJSON StringExample where
    parseJSON :: Value -> Parser StringExample
parseJSON Value
val =
        (Text -> StringExample
StringExampleString (Text -> StringExample) -> Parser Text -> Parser StringExample
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Value -> Parser Text
forall a. FromJSON a => Value -> Parser a
parseJSON Value
val) Parser StringExample
-> Parser StringExample -> Parser StringExample
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
        ([Text] -> StringExample
StringExampleArray ([Text] -> StringExample) -> Parser [Text] -> Parser StringExample
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Value -> Parser [Text]
forall a. FromJSON a => Value -> Parser a
parseJSON Value
val)

instance ToJSON StringExample where
    toJSON :: StringExample -> Value
toJSON (StringExampleString Text
str) = Text -> Value
forall a. ToJSON a => a -> Value
toJSON Text
str
    toJSON (StringExampleArray [Text]
arr)  = [Text] -> Value
forall a. ToJSON a => a -> Value
toJSON [Text]
arr

data SchemaString = SchemaString
    { SchemaString -> SchemaCommon Text StringExample
schemaStringCommon               :: SchemaCommon Text StringExample
    , SchemaString -> Maybe [Text]
schemaStringEnum                 :: Maybe [Text]
    , SchemaString -> Maybe Text
schemaStringFormat               :: Maybe Text
    , SchemaString -> Maybe Text
schemaStringPattern              :: Maybe Text
    , SchemaString -> Maybe Int
schemaStringMinLength            :: Maybe Int
    , SchemaString -> Maybe Int
schemaStringMaxLength            :: Maybe Int
    , SchemaString -> Maybe Bool
schemaStringAdditionalProperties :: Maybe Bool
    , SchemaString -> Maybe Int
schemaStringMinItems             :: Maybe Int
    }
    deriving (Int -> SchemaString -> ShowS
[SchemaString] -> ShowS
SchemaString -> String
(Int -> SchemaString -> ShowS)
-> (SchemaString -> String)
-> ([SchemaString] -> ShowS)
-> Show SchemaString
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [SchemaString] -> ShowS
$cshowList :: [SchemaString] -> ShowS
show :: SchemaString -> String
$cshow :: SchemaString -> String
showsPrec :: Int -> SchemaString -> ShowS
$cshowsPrec :: Int -> SchemaString -> ShowS
Show)

newtype SchemaBoolean = SchemaBoolean
    { SchemaBoolean -> SchemaCommon Bool Bool
schemaBooleanCommon :: SchemaCommon Bool Bool
    }
    deriving (Int -> SchemaBoolean -> ShowS
[SchemaBoolean] -> ShowS
SchemaBoolean -> String
(Int -> SchemaBoolean -> ShowS)
-> (SchemaBoolean -> String)
-> ([SchemaBoolean] -> ShowS)
-> Show SchemaBoolean
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [SchemaBoolean] -> ShowS
$cshowList :: [SchemaBoolean] -> ShowS
show :: SchemaBoolean -> String
$cshow :: SchemaBoolean -> String
showsPrec :: Int -> SchemaBoolean -> ShowS
$cshowsPrec :: Int -> SchemaBoolean -> ShowS
Show)

data SchemaNumber int = SchemaNumber
    { SchemaNumber int -> SchemaCommon int int
schemaNumberCommon  :: SchemaCommon int int
    , SchemaNumber int -> Maybe Text
schemaNumberFormat  :: Maybe Text
    , SchemaNumber int -> Maybe int
schemaNumberMinimum :: Maybe int
    , SchemaNumber int -> Maybe int
schemaNumberMaximum :: Maybe int
    }
    deriving (Int -> SchemaNumber int -> ShowS
[SchemaNumber int] -> ShowS
SchemaNumber int -> String
(Int -> SchemaNumber int -> ShowS)
-> (SchemaNumber int -> String)
-> ([SchemaNumber int] -> ShowS)
-> Show (SchemaNumber int)
forall int. Show int => Int -> SchemaNumber int -> ShowS
forall int. Show int => [SchemaNumber int] -> ShowS
forall int. Show int => SchemaNumber int -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [SchemaNumber int] -> ShowS
$cshowList :: forall int. Show int => [SchemaNumber int] -> ShowS
show :: SchemaNumber int -> String
$cshow :: forall int. Show int => SchemaNumber int -> String
showsPrec :: Int -> SchemaNumber int -> ShowS
$cshowsPrec :: forall int. Show int => Int -> SchemaNumber int -> ShowS
Show)

data Content = Content
    { Content -> SchemaType
contentSchema   :: SchemaType
    , Content -> Maybe (HashMap Text Value)
contentExamples :: Maybe (HashMap Text Value)
    }
    deriving (Int -> Content -> ShowS
[Content] -> ShowS
Content -> String
(Int -> Content -> ShowS)
-> (Content -> String) -> ([Content] -> ShowS) -> Show Content
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Content] -> ShowS
$cshowList :: [Content] -> ShowS
show :: Content -> String
$cshow :: Content -> String
showsPrec :: Int -> Content -> ShowS
$cshowsPrec :: Int -> Content -> ShowS
Show)
$(deriveJSON defaultOptions{fieldLabelModifier = camel . drop (Text.length "Content")} ''Content)

data Header = Header
    { Header -> Maybe Value
headerExample :: Maybe Value
    , Header -> Maybe SchemaType
headerSchema  :: Maybe SchemaType
    }
    deriving (Int -> Header -> ShowS
[Header] -> ShowS
Header -> String
(Int -> Header -> ShowS)
-> (Header -> String) -> ([Header] -> ShowS) -> Show Header
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Header] -> ShowS
$cshowList :: [Header] -> ShowS
show :: Header -> String
$cshow :: Header -> String
showsPrec :: Int -> Header -> ShowS
$cshowsPrec :: Int -> Header -> ShowS
Show)
$(deriveJSON defaultOptions{fieldLabelModifier = camel . drop (Text.length "Header")} ''Header)

data Response = Response
    { Response -> Maybe Text
responseDescription :: Maybe Text
    , Response -> Maybe (HashMap Text Content)
responseContent     :: Maybe (HashMap Text Content)
    , Response -> Maybe (HashMap Text Header)
responseHeaders     :: Maybe (HashMap Text Header)
    }
    deriving (Int -> Response -> ShowS
[Response] -> ShowS
Response -> String
(Int -> Response -> ShowS)
-> (Response -> String) -> ([Response] -> ShowS) -> Show Response
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Response] -> ShowS
$cshowList :: [Response] -> ShowS
show :: Response -> String
$cshow :: Response -> String
showsPrec :: Int -> Response -> ShowS
$cshowsPrec :: Int -> Response -> ShowS
Show)
$(deriveJSON defaultOptions{fieldLabelModifier = camel . drop (Text.length "Response")} ''Response)

data Request = Request
    { Request -> HashMap Text Content
requestContent     :: HashMap Text Content
    , Request -> Maybe Text
requestDescription :: Maybe Text
    , Request -> Maybe Bool
requestRequired    :: Maybe Bool
    }
    deriving (Int -> Request -> ShowS
[Request] -> ShowS
Request -> String
(Int -> Request -> ShowS)
-> (Request -> String) -> ([Request] -> ShowS) -> Show Request
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Request] -> ShowS
$cshowList :: [Request] -> ShowS
show :: Request -> String
$cshow :: Request -> String
showsPrec :: Int -> Request -> ShowS
$cshowsPrec :: Int -> Request -> ShowS
Show)
$(deriveJSON defaultOptions{fieldLabelModifier = camel . drop (Text.length "Request")} ''Request)

data Parameter = Parameter
    { Parameter -> Maybe Text
parameterDescription :: Maybe Text
    , Parameter -> Text
parameterIn          :: Text
    , Parameter -> Text
parameterName        :: Text
    , Parameter -> Maybe Value
parameterExample     :: Maybe Value
    , Parameter -> Maybe (HashMap Text Value)
parameterExamples    :: Maybe (HashMap Text Value)
    , Parameter -> SchemaType
parameterSchema      :: SchemaType
    , Parameter -> Maybe Bool
parameterRequired    :: Maybe Bool
    , Parameter -> Maybe Bool
parameterDeprecated  :: Maybe Bool
    }
    deriving (Int -> Parameter -> ShowS
[Parameter] -> ShowS
Parameter -> String
(Int -> Parameter -> ShowS)
-> (Parameter -> String)
-> ([Parameter] -> ShowS)
-> Show Parameter
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Parameter] -> ShowS
$cshowList :: [Parameter] -> ShowS
show :: Parameter -> String
$cshow :: Parameter -> String
showsPrec :: Int -> Parameter -> ShowS
$cshowsPrec :: Int -> Parameter -> ShowS
Show)
$(deriveJSON defaultOptions{fieldLabelModifier = camel . drop (Text.length "Parameter")} ''Parameter)

data Method = Method
    { Method -> Text
methodSummary      :: Text
    , Method -> Maybe [Server]
methodServers      :: Maybe [Server]
    , Method -> Maybe Text
methodDescription  :: Maybe Text
    , Method -> Maybe Bool
methodDeprecated   :: Maybe Bool
    , Method -> Maybe Url
methodExternalDocs :: Maybe Url
    , Method -> [Text]
methodTags         :: [Text]
    , Method -> Text
methodOperationId  :: Text
    , Method -> Maybe [Parameter]
methodParameters   :: Maybe [Parameter]
    , Method -> HashMap Text Response
methodResponses    :: HashMap Text Response
    , Method -> Maybe Request
methodRequestBody  :: Maybe Request
    }
    deriving (Int -> Method -> ShowS
[Method] -> ShowS
Method -> String
(Int -> Method -> ShowS)
-> (Method -> String) -> ([Method] -> ShowS) -> Show Method
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Method] -> ShowS
$cshowList :: [Method] -> ShowS
show :: Method -> String
$cshow :: Method -> String
showsPrec :: Int -> Method -> ShowS
$cshowsPrec :: Int -> Method -> ShowS
Show)
$(deriveJSON defaultOptions{fieldLabelModifier = camel . drop (Text.length "Method")} ''Method)

data Path = Path
    { Path -> Maybe Method
pathGet    :: Maybe Method
    , Path -> Maybe Method
pathPost   :: Maybe Method
    , Path -> Maybe Method
pathDelete :: Maybe Method
    , Path -> Maybe Method
pathPatch  :: Maybe Method
    , Path -> Maybe Method
pathPut    :: Maybe Method
    }
    deriving (Int -> Path -> ShowS
[Path] -> ShowS
Path -> String
(Int -> Path -> ShowS)
-> (Path -> String) -> ([Path] -> ShowS) -> Show Path
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Path] -> ShowS
$cshowList :: [Path] -> ShowS
show :: Path -> String
$cshow :: Path -> String
showsPrec :: Int -> Path -> ShowS
$cshowsPrec :: Int -> Path -> ShowS
Show)
$(deriveJSON defaultOptions{fieldLabelModifier = camel . drop (Text.length "Path")} ''Path)

data Spec = Spec
    { Spec -> Text
specOpenapi      :: Text
    , Spec -> Info
specInfo         :: Info
    , Spec -> [Tag]
specTags         :: [Tag]
    , Spec -> [Server]
specServers      :: [Server]
    , Spec -> Url
specExternalDocs :: Url
    , Spec -> HashMap Text Path
specPaths        :: HashMap Text Path
    }
    deriving (Int -> Spec -> ShowS
[Spec] -> ShowS
Spec -> String
(Int -> Spec -> ShowS)
-> (Spec -> String) -> ([Spec] -> ShowS) -> Show Spec
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Spec] -> ShowS
$cshowList :: [Spec] -> ShowS
show :: Spec -> String
$cshow :: Spec -> String
showsPrec :: Int -> Spec -> ShowS
$cshowsPrec :: Int -> Spec -> ShowS
Show)
$(deriveJSON defaultOptions{fieldLabelModifier = camel . drop (Text.length "Spec")} ''Spec)

resolveRef :: Text -> Value -> Value
resolveRef :: Text -> Value -> Value
resolveRef Text
ref Value
root = [Text] -> Value -> Value
go (Text -> Text -> [Text]
Text.splitOn Text
"/" Text
ref) Value
root
  where
    go :: [Text] -> Value -> Value
    go :: [Text] -> Value -> Value
go [] Value
val = Value -> Value -> Value
resolveRefsWith Value
root Value
val
    go (Text
"#":[Text]
path) Value
val = [Text] -> Value -> Value
go [Text]
path Value
val
    go (Text
key:[Text]
path) (Object KeyMap Value
mems) =
        case Key -> KeyMap Value -> Maybe Value
forall v. Key -> KeyMap v -> Maybe v
KeyMap.lookup (Text -> Key
Key.fromText Text
key) KeyMap Value
mems of
            Maybe Value
Nothing -> String -> Value
forall a. HasCallStack => String -> a
error (String -> Value) -> String -> Value
forall a b. (a -> b) -> a -> b
$ Text -> String
forall a. Show a => a -> String
show Text
key
            Just Value
ob -> [Text] -> Value -> Value
go [Text]
path Value
ob
    go [Text]
_ Value
val = String -> Value
forall a. HasCallStack => String -> a
error (String -> Value) -> String -> Value
forall a b. (a -> b) -> a -> b
$ (Text, Value) -> String
forall a. Show a => a -> String
show (Text
ref, Value
val)

resolveRefsWith :: Value -> Value -> Value
resolveRefsWith :: Value -> Value -> Value
resolveRefsWith Value
root = Value -> Value
go
  where
    go :: Value -> Value
go (Object KeyMap Value
mems) =
        case Key -> KeyMap Value -> Maybe Value
forall v. Key -> KeyMap v -> Maybe v
KeyMap.lookup Key
"$ref" KeyMap Value
mems of
            Just (String Text
ref) -> Text -> Value -> Value
resolveRef Text
ref Value
root
            Maybe Value
_                 -> KeyMap Value -> Value
Object (KeyMap Value -> Value) -> KeyMap Value -> Value
forall a b. (a -> b) -> a -> b
$ (Value -> Value) -> KeyMap Value -> KeyMap Value
forall a b. (a -> b) -> KeyMap a -> KeyMap b
KeyMap.map Value -> Value
go KeyMap Value
mems
    go (Array Array
items) = Array -> Value
Array (Array -> Value) -> Array -> Value
forall a b. (a -> b) -> a -> b
$ (Value -> Value) -> Array -> Array
forall a b. (a -> b) -> Vector a -> Vector b
V.map Value -> Value
go Array
items

    go x :: Value
x@String{} = Value
x
    go x :: Value
x@Number{} = Value
x
    go x :: Value
x@Bool{} = Value
x
    go x :: Value
x@Null{} = Value
x

resolveRefs :: Value -> Value
resolveRefs :: Value -> Value
resolveRefs Value
root =
    case Value -> Value -> Value
resolveRefsWith Value
root Value
root of
        Object KeyMap Value
mems -> KeyMap Value -> Value
Object ((Key -> Value -> Bool) -> KeyMap Value -> KeyMap Value
forall v. (Key -> v -> Bool) -> KeyMap v -> KeyMap v
KeyMap.filterWithKey (\Key
k Value
_ -> Key
k Key -> [Key] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` [Key
"components", Key
""]) KeyMap Value
mems)
        Value
resolved -> Value
resolved

parseSpec :: Value -> Either String Spec
parseSpec :: Value -> Either String Spec
parseSpec = (Value -> Parser Spec) -> Value -> Either String Spec
forall a b. (a -> Parser b) -> a -> Either String b
parseEither Value -> Parser Spec
forall a. FromJSON a => Value -> Parser a
parseJSON

removeNulls :: ToJSON a => a -> Value
removeNulls :: a -> Value
removeNulls = Value -> Value
go (Value -> Value) -> (a -> Value) -> a -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Value
forall a. ToJSON a => a -> Value
toJSON
  where
    go :: Value -> Value
go (Array  Array
x) = Array -> Value
Array (Array -> Value) -> (Array -> Array) -> Array -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Value -> Value) -> Array -> Array
forall a b. (a -> b) -> Vector a -> Vector b
V.map Value -> Value
go (Array -> Value) -> Array -> Value
forall a b. (a -> b) -> a -> b
$ Array
x
    go (Object KeyMap Value
x) = KeyMap Value -> Value
Object (KeyMap Value -> Value)
-> (KeyMap Value -> KeyMap Value) -> KeyMap Value -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Value -> Value) -> KeyMap Value -> KeyMap Value
forall a b. (a -> b) -> KeyMap a -> KeyMap b
KeyMap.map Value -> Value
go (KeyMap Value -> KeyMap Value)
-> (KeyMap Value -> KeyMap Value) -> KeyMap Value -> KeyMap Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Key -> Value -> Bool) -> KeyMap Value -> KeyMap Value
forall v. (Key -> v -> Bool) -> KeyMap v -> KeyMap v
KeyMap.filterWithKey Key -> Value -> Bool
validPair (KeyMap Value -> Value) -> KeyMap Value -> Value
forall a b. (a -> b) -> a -> b
$ KeyMap Value
x
    go         Value
x  = Value
x

    isEmpty :: Value -> Bool
isEmpty Value
Null      = Bool
True
    isEmpty (Array Array
x) = Array -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null Array
x
    isEmpty Value
_         = Bool
False

    validPair :: Key -> Value -> Bool
validPair Key
k Value
v = Bool -> Bool
not (Value -> Bool
isEmpty Value
v Bool -> Bool -> Bool
|| Text
"x-" Text -> Text -> Bool
`Text.isPrefixOf` Key -> Text
Key.toText Key
k)