{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE UndecidableInstances #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
-- |
-- Module:      Data.OpenApi.Lens
-- Maintainer:  Nickolay Kudasov <nickolay@getshoptv.com>
-- Stability:   experimental
--
-- Lenses and prisms for Swagger.
module Data.OpenApi.Lens where

import Control.Lens
import Data.Aeson (Value)
import Data.Scientific (Scientific)
import Data.OpenApi.Internal
import Data.OpenApi.Internal.Utils
import Data.Text (Text)

-- * Classy lenses

makeFields ''OpenApi
makeFields ''Components
makeFields ''Server
makeFields ''RequestBody
makeFields ''MediaTypeObject
makeFields ''Info
makeFields ''Contact
makeFields ''License
makeLensesWith swaggerFieldRules ''PathItem
makeFields ''Tag
makeFields ''Operation
makeLensesWith swaggerFieldRules ''Param
makeFields ''Header
makeLensesWith swaggerFieldRules ''Schema
makeFields ''NamedSchema
makeFields ''Xml
makeLensesWith swaggerFieldRules ''Responses
makeFields ''Response
makeLensesWith swaggerFieldRules ''SecurityScheme
makeFields ''ApiKeyParams
makeFields ''OAuth2ImplicitFlow
makeFields ''OAuth2PasswordFlow
makeFields ''OAuth2ClientCredentialsFlow
makeFields ''OAuth2AuthorizationCodeFlow
makeFields ''OAuth2Flow
makeFields ''OAuth2Flows
makeFields ''ExternalDocs
makeFields ''Encoding
makeFields ''Example
makeFields ''Discriminator
makeFields ''Link

-- * Prisms
-- ** 'SecuritySchemeType' prisms
makePrisms ''SecuritySchemeType
-- ** 'Referenced' prisms
makePrisms ''Referenced

-- ** 'OpenApiItems' prisms

_OpenApiItemsArray :: Review OpenApiItems [Referenced Schema]
_OpenApiItemsArray :: Optic' p f OpenApiItems [Referenced Schema]
_OpenApiItemsArray
  = ([Referenced Schema] -> OpenApiItems)
-> Optic' p f OpenApiItems [Referenced Schema]
forall (p :: * -> * -> *) (f :: * -> *) b t s a.
(Profunctor p, Bifunctor p, Functor f) =>
(b -> t) -> Optic p f s t a b
unto (\[Referenced Schema]
x -> [Referenced Schema] -> OpenApiItems
OpenApiItemsArray [Referenced Schema]
x)
{- \x -> case x of
      OpenApiItemsPrimitive c p -> Left (OpenApiItemsPrimitive c p)
      OpenApiItemsObject o      -> Left (OpenApiItemsObject o)
      OpenApiItemsArray a       -> Right a
-}

_OpenApiItemsObject :: Review OpenApiItems (Referenced Schema)
_OpenApiItemsObject :: Optic' p f OpenApiItems (Referenced Schema)
_OpenApiItemsObject
  = (Referenced Schema -> OpenApiItems)
-> Optic' p f OpenApiItems (Referenced Schema)
forall (p :: * -> * -> *) (f :: * -> *) b t s a.
(Profunctor p, Bifunctor p, Functor f) =>
(b -> t) -> Optic p f s t a b
unto (\Referenced Schema
x -> Referenced Schema -> OpenApiItems
OpenApiItemsObject Referenced Schema
x)
{- \x -> case x of
      OpenApiItemsPrimitive c p -> Left (OpenApiItemsPrimitive c p)
      OpenApiItemsObject o      -> Right o
      OpenApiItemsArray a       -> Left (OpenApiItemsArray a)
-}

-- =============================================================
-- More helpful instances for easier access to schema properties

type instance Index Responses = HttpStatusCode
type instance Index Operation = HttpStatusCode

type instance IxValue Responses = Referenced Response
type instance IxValue Operation = Referenced Response

instance Ixed Responses where ix :: Index Responses -> Traversal' Responses (IxValue Responses)
ix Index Responses
n = (InsOrdHashMap HttpStatusCode (Referenced Response)
 -> f (InsOrdHashMap HttpStatusCode (Referenced Response)))
-> Responses -> f Responses
forall s a. HasResponses s a => Lens' s a
responses ((InsOrdHashMap HttpStatusCode (Referenced Response)
  -> f (InsOrdHashMap HttpStatusCode (Referenced Response)))
 -> Responses -> f Responses)
-> ((Referenced Response -> f (Referenced Response))
    -> InsOrdHashMap HttpStatusCode (Referenced Response)
    -> f (InsOrdHashMap HttpStatusCode (Referenced Response)))
-> (Referenced Response -> f (Referenced Response))
-> Responses
-> f Responses
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Index (InsOrdHashMap HttpStatusCode (Referenced Response))
-> Traversal'
     (InsOrdHashMap HttpStatusCode (Referenced Response))
     (IxValue (InsOrdHashMap HttpStatusCode (Referenced Response)))
forall m. Ixed m => Index m -> Traversal' m (IxValue m)
ix Index (InsOrdHashMap HttpStatusCode (Referenced Response))
Index Responses
n
instance At   Responses where at :: Index Responses -> Lens' Responses (Maybe (IxValue Responses))
at Index Responses
n = (InsOrdHashMap HttpStatusCode (Referenced Response)
 -> f (InsOrdHashMap HttpStatusCode (Referenced Response)))
-> Responses -> f Responses
forall s a. HasResponses s a => Lens' s a
responses ((InsOrdHashMap HttpStatusCode (Referenced Response)
  -> f (InsOrdHashMap HttpStatusCode (Referenced Response)))
 -> Responses -> f Responses)
-> ((Maybe (Referenced Response)
     -> f (Maybe (Referenced Response)))
    -> InsOrdHashMap HttpStatusCode (Referenced Response)
    -> f (InsOrdHashMap HttpStatusCode (Referenced Response)))
-> (Maybe (Referenced Response) -> f (Maybe (Referenced Response)))
-> Responses
-> f Responses
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Index (InsOrdHashMap HttpStatusCode (Referenced Response))
-> Lens'
     (InsOrdHashMap HttpStatusCode (Referenced Response))
     (Maybe
        (IxValue (InsOrdHashMap HttpStatusCode (Referenced Response))))
forall m. At m => Index m -> Lens' m (Maybe (IxValue m))
at Index (InsOrdHashMap HttpStatusCode (Referenced Response))
Index Responses
n

instance Ixed Operation where ix :: Index Operation -> Traversal' Operation (IxValue Operation)
ix Index Operation
n = (Responses -> f Responses) -> Operation -> f Operation
forall s a. HasResponses s a => Lens' s a
responses ((Responses -> f Responses) -> Operation -> f Operation)
-> ((Referenced Response -> f (Referenced Response))
    -> Responses -> f Responses)
-> (Referenced Response -> f (Referenced Response))
-> Operation
-> f Operation
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Index Responses -> Traversal' Responses (IxValue Responses)
forall m. Ixed m => Index m -> Traversal' m (IxValue m)
ix Index Responses
Index Operation
n
instance At   Operation where at :: Index Operation -> Lens' Operation (Maybe (IxValue Operation))
at Index Operation
n = (Responses -> f Responses) -> Operation -> f Operation
forall s a. HasResponses s a => Lens' s a
responses ((Responses -> f Responses) -> Operation -> f Operation)
-> ((Maybe (Referenced Response)
     -> f (Maybe (Referenced Response)))
    -> Responses -> f Responses)
-> (Maybe (Referenced Response) -> f (Maybe (Referenced Response)))
-> Operation
-> f Operation
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Index Responses -> Lens' Responses (Maybe (IxValue Responses))
forall m. At m => Index m -> Lens' m (Maybe (IxValue m))
at Index Responses
Index Operation
n

instance HasType NamedSchema (Maybe OpenApiType) where type_ :: (Maybe OpenApiType -> f (Maybe OpenApiType))
-> NamedSchema -> f NamedSchema
type_ = (Schema -> f Schema) -> NamedSchema -> f NamedSchema
forall s a. HasSchema s a => Lens' s a
schema((Schema -> f Schema) -> NamedSchema -> f NamedSchema)
-> ((Maybe OpenApiType -> f (Maybe OpenApiType))
    -> Schema -> f Schema)
-> (Maybe OpenApiType -> f (Maybe OpenApiType))
-> NamedSchema
-> f NamedSchema
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Maybe OpenApiType -> f (Maybe OpenApiType)) -> Schema -> f Schema
forall s a. HasType s a => Lens' s a
type_

-- OVERLAPPABLE instances

instance
  {-# OVERLAPPABLE #-}
  HasSchema s Schema
  => HasFormat s (Maybe Format) where
  format :: (Maybe Text -> f (Maybe Text)) -> s -> f s
format = (Schema -> f Schema) -> s -> f s
forall s a. HasSchema s a => Lens' s a
schema((Schema -> f Schema) -> s -> f s)
-> ((Maybe Text -> f (Maybe Text)) -> Schema -> f Schema)
-> (Maybe Text -> f (Maybe Text))
-> s
-> f s
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Maybe Text -> f (Maybe Text)) -> Schema -> f Schema
forall s a. HasFormat s a => Lens' s a
format

instance
  {-# OVERLAPPABLE #-}
  HasSchema s Schema
  => HasItems s (Maybe OpenApiItems) where
  items :: (Maybe OpenApiItems -> f (Maybe OpenApiItems)) -> s -> f s
items = (Schema -> f Schema) -> s -> f s
forall s a. HasSchema s a => Lens' s a
schema((Schema -> f Schema) -> s -> f s)
-> ((Maybe OpenApiItems -> f (Maybe OpenApiItems))
    -> Schema -> f Schema)
-> (Maybe OpenApiItems -> f (Maybe OpenApiItems))
-> s
-> f s
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Maybe OpenApiItems -> f (Maybe OpenApiItems))
-> Schema -> f Schema
forall s a. HasItems s a => Lens' s a
items

instance
  {-# OVERLAPPABLE #-}
  HasSchema s Schema
  => HasMaximum s (Maybe Scientific) where
  maximum_ :: (Maybe Scientific -> f (Maybe Scientific)) -> s -> f s
maximum_ = (Schema -> f Schema) -> s -> f s
forall s a. HasSchema s a => Lens' s a
schema((Schema -> f Schema) -> s -> f s)
-> ((Maybe Scientific -> f (Maybe Scientific))
    -> Schema -> f Schema)
-> (Maybe Scientific -> f (Maybe Scientific))
-> s
-> f s
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Maybe Scientific -> f (Maybe Scientific)) -> Schema -> f Schema
forall s a. HasMaximum s a => Lens' s a
maximum_

instance {-# OVERLAPPABLE #-} HasSchema s Schema
  => HasExclusiveMaximum s (Maybe Bool) where
  exclusiveMaximum :: (Maybe Bool -> f (Maybe Bool)) -> s -> f s
exclusiveMaximum = (Schema -> f Schema) -> s -> f s
forall s a. HasSchema s a => Lens' s a
schema((Schema -> f Schema) -> s -> f s)
-> ((Maybe Bool -> f (Maybe Bool)) -> Schema -> f Schema)
-> (Maybe Bool -> f (Maybe Bool))
-> s
-> f s
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Maybe Bool -> f (Maybe Bool)) -> Schema -> f Schema
forall s a. HasExclusiveMaximum s a => Lens' s a
exclusiveMaximum

instance {-# OVERLAPPABLE #-} HasSchema s Schema
  => HasMinimum s (Maybe Scientific) where
  minimum_ :: (Maybe Scientific -> f (Maybe Scientific)) -> s -> f s
minimum_ = (Schema -> f Schema) -> s -> f s
forall s a. HasSchema s a => Lens' s a
schema((Schema -> f Schema) -> s -> f s)
-> ((Maybe Scientific -> f (Maybe Scientific))
    -> Schema -> f Schema)
-> (Maybe Scientific -> f (Maybe Scientific))
-> s
-> f s
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Maybe Scientific -> f (Maybe Scientific)) -> Schema -> f Schema
forall s a. HasMinimum s a => Lens' s a
minimum_

instance {-# OVERLAPPABLE #-} HasSchema s Schema
  => HasExclusiveMinimum s (Maybe Bool) where
  exclusiveMinimum :: (Maybe Bool -> f (Maybe Bool)) -> s -> f s
exclusiveMinimum = (Schema -> f Schema) -> s -> f s
forall s a. HasSchema s a => Lens' s a
schema((Schema -> f Schema) -> s -> f s)
-> ((Maybe Bool -> f (Maybe Bool)) -> Schema -> f Schema)
-> (Maybe Bool -> f (Maybe Bool))
-> s
-> f s
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Maybe Bool -> f (Maybe Bool)) -> Schema -> f Schema
forall s a. HasExclusiveMinimum s a => Lens' s a
exclusiveMinimum

instance {-# OVERLAPPABLE #-} HasSchema s Schema
  => HasMaxLength s (Maybe Integer) where
  maxLength :: (Maybe Integer -> f (Maybe Integer)) -> s -> f s
maxLength = (Schema -> f Schema) -> s -> f s
forall s a. HasSchema s a => Lens' s a
schema((Schema -> f Schema) -> s -> f s)
-> ((Maybe Integer -> f (Maybe Integer)) -> Schema -> f Schema)
-> (Maybe Integer -> f (Maybe Integer))
-> s
-> f s
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Maybe Integer -> f (Maybe Integer)) -> Schema -> f Schema
forall s a. HasMaxLength s a => Lens' s a
maxLength

instance {-# OVERLAPPABLE #-} HasSchema s Schema
  => HasMinLength s (Maybe Integer) where
  minLength :: (Maybe Integer -> f (Maybe Integer)) -> s -> f s
minLength = (Schema -> f Schema) -> s -> f s
forall s a. HasSchema s a => Lens' s a
schema((Schema -> f Schema) -> s -> f s)
-> ((Maybe Integer -> f (Maybe Integer)) -> Schema -> f Schema)
-> (Maybe Integer -> f (Maybe Integer))
-> s
-> f s
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Maybe Integer -> f (Maybe Integer)) -> Schema -> f Schema
forall s a. HasMinLength s a => Lens' s a
minLength

instance {-# OVERLAPPABLE #-} HasSchema s Schema
  => HasPattern s (Maybe Text) where
  pattern :: (Maybe Text -> f (Maybe Text)) -> s -> f s
pattern = (Schema -> f Schema) -> s -> f s
forall s a. HasSchema s a => Lens' s a
schema((Schema -> f Schema) -> s -> f s)
-> ((Maybe Text -> f (Maybe Text)) -> Schema -> f Schema)
-> (Maybe Text -> f (Maybe Text))
-> s
-> f s
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Maybe Text -> f (Maybe Text)) -> Schema -> f Schema
forall s a. HasPattern s a => Lens' s a
pattern

instance {-# OVERLAPPABLE #-} HasSchema s Schema
  => HasMaxItems s (Maybe Integer) where
  maxItems :: (Maybe Integer -> f (Maybe Integer)) -> s -> f s
maxItems = (Schema -> f Schema) -> s -> f s
forall s a. HasSchema s a => Lens' s a
schema((Schema -> f Schema) -> s -> f s)
-> ((Maybe Integer -> f (Maybe Integer)) -> Schema -> f Schema)
-> (Maybe Integer -> f (Maybe Integer))
-> s
-> f s
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Maybe Integer -> f (Maybe Integer)) -> Schema -> f Schema
forall s a. HasMaxItems s a => Lens' s a
maxItems

instance {-# OVERLAPPABLE #-} HasSchema s Schema
  => HasMinItems s (Maybe Integer) where
  minItems :: (Maybe Integer -> f (Maybe Integer)) -> s -> f s
minItems = (Schema -> f Schema) -> s -> f s
forall s a. HasSchema s a => Lens' s a
schema((Schema -> f Schema) -> s -> f s)
-> ((Maybe Integer -> f (Maybe Integer)) -> Schema -> f Schema)
-> (Maybe Integer -> f (Maybe Integer))
-> s
-> f s
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Maybe Integer -> f (Maybe Integer)) -> Schema -> f Schema
forall s a. HasMinItems s a => Lens' s a
minItems

instance {-# OVERLAPPABLE #-} HasSchema s Schema
  => HasUniqueItems s (Maybe Bool) where
  uniqueItems :: (Maybe Bool -> f (Maybe Bool)) -> s -> f s
uniqueItems = (Schema -> f Schema) -> s -> f s
forall s a. HasSchema s a => Lens' s a
schema((Schema -> f Schema) -> s -> f s)
-> ((Maybe Bool -> f (Maybe Bool)) -> Schema -> f Schema)
-> (Maybe Bool -> f (Maybe Bool))
-> s
-> f s
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Maybe Bool -> f (Maybe Bool)) -> Schema -> f Schema
forall s a. HasUniqueItems s a => Lens' s a
uniqueItems

instance {-# OVERLAPPABLE #-} HasSchema s Schema
  => HasEnum s (Maybe [Value]) where
  enum_ :: (Maybe [Value] -> f (Maybe [Value])) -> s -> f s
enum_ = (Schema -> f Schema) -> s -> f s
forall s a. HasSchema s a => Lens' s a
schema((Schema -> f Schema) -> s -> f s)
-> ((Maybe [Value] -> f (Maybe [Value])) -> Schema -> f Schema)
-> (Maybe [Value] -> f (Maybe [Value]))
-> s
-> f s
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Maybe [Value] -> f (Maybe [Value])) -> Schema -> f Schema
forall s a. HasEnum s a => Lens' s a
enum_

instance {-# OVERLAPPABLE #-} HasSchema s Schema
  => HasMultipleOf s (Maybe Scientific) where
  multipleOf :: (Maybe Scientific -> f (Maybe Scientific)) -> s -> f s
multipleOf = (Schema -> f Schema) -> s -> f s
forall s a. HasSchema s a => Lens' s a
schema((Schema -> f Schema) -> s -> f s)
-> ((Maybe Scientific -> f (Maybe Scientific))
    -> Schema -> f Schema)
-> (Maybe Scientific -> f (Maybe Scientific))
-> s
-> f s
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Maybe Scientific -> f (Maybe Scientific)) -> Schema -> f Schema
forall s a. HasMultipleOf s a => Lens' s a
multipleOf