{-# 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 :: Review OpenApiItems [Referenced Schema]
_OpenApiItemsArray
  = ([Referenced Schema] -> OpenApiItems)
-> Optic
     p
     f
     OpenApiItems
     OpenApiItems
     [Referenced Schema]
     [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 :: Review OpenApiItems (Referenced Schema)
_OpenApiItemsObject
  = (Referenced Schema -> OpenApiItems)
-> Optic
     p
     f
     OpenApiItems
     OpenApiItems
     (Referenced Schema)
     (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
Lens'
  Responses (InsOrdHashMap HttpStatusCode (Referenced Response))
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
Lens'
  Responses (InsOrdHashMap HttpStatusCode (Referenced Response))
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
Lens' Operation Responses
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
Lens' Operation Responses
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_ :: Lens' NamedSchema (Maybe OpenApiType)
type_ = (Schema -> f Schema) -> NamedSchema -> f NamedSchema
forall s a. HasSchema s a => Lens' s a
Lens' NamedSchema Schema
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
Lens' Schema (Maybe OpenApiType)
type_

-- OVERLAPPABLE instances

instance
  {-# OVERLAPPABLE #-}
  HasSchema s Schema
  => HasFormat s (Maybe Format) where
  format :: Lens' s (Maybe Text)
format = (Schema -> f Schema) -> s -> f s
forall s a. HasSchema s a => Lens' s a
Lens' s Schema
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
Lens' Schema (Maybe Text)
format

instance
  {-# OVERLAPPABLE #-}
  HasSchema s Schema
  => HasItems s (Maybe OpenApiItems) where
  items :: Lens' s (Maybe OpenApiItems)
items = (Schema -> f Schema) -> s -> f s
forall s a. HasSchema s a => Lens' s a
Lens' s Schema
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
Lens' Schema (Maybe OpenApiItems)
items

instance
  {-# OVERLAPPABLE #-}
  HasSchema s Schema
  => HasMaximum s (Maybe Scientific) where
  maximum_ :: Lens' s (Maybe Scientific)
maximum_ = (Schema -> f Schema) -> s -> f s
forall s a. HasSchema s a => Lens' s a
Lens' s Schema
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
Lens' Schema (Maybe Scientific)
maximum_

instance {-# OVERLAPPABLE #-} HasSchema s Schema
  => HasExclusiveMaximum s (Maybe Bool) where
  exclusiveMaximum :: Lens' s (Maybe Bool)
exclusiveMaximum = (Schema -> f Schema) -> s -> f s
forall s a. HasSchema s a => Lens' s a
Lens' s Schema
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
Lens' Schema (Maybe Bool)
exclusiveMaximum

instance {-# OVERLAPPABLE #-} HasSchema s Schema
  => HasMinimum s (Maybe Scientific) where
  minimum_ :: Lens' s (Maybe Scientific)
minimum_ = (Schema -> f Schema) -> s -> f s
forall s a. HasSchema s a => Lens' s a
Lens' s Schema
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
Lens' Schema (Maybe Scientific)
minimum_

instance {-# OVERLAPPABLE #-} HasSchema s Schema
  => HasExclusiveMinimum s (Maybe Bool) where
  exclusiveMinimum :: Lens' s (Maybe Bool)
exclusiveMinimum = (Schema -> f Schema) -> s -> f s
forall s a. HasSchema s a => Lens' s a
Lens' s Schema
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
Lens' Schema (Maybe Bool)
exclusiveMinimum

instance {-# OVERLAPPABLE #-} HasSchema s Schema
  => HasMaxLength s (Maybe Integer) where
  maxLength :: Lens' s (Maybe Integer)
maxLength = (Schema -> f Schema) -> s -> f s
forall s a. HasSchema s a => Lens' s a
Lens' s Schema
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
Lens' Schema (Maybe Integer)
maxLength

instance {-# OVERLAPPABLE #-} HasSchema s Schema
  => HasMinLength s (Maybe Integer) where
  minLength :: Lens' s (Maybe Integer)
minLength = (Schema -> f Schema) -> s -> f s
forall s a. HasSchema s a => Lens' s a
Lens' s Schema
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
Lens' Schema (Maybe Integer)
minLength

instance {-# OVERLAPPABLE #-} HasSchema s Schema
  => HasPattern s (Maybe Text) where
  pattern :: Lens' s (Maybe Text)
pattern = (Schema -> f Schema) -> s -> f s
forall s a. HasSchema s a => Lens' s a
Lens' s Schema
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
Lens' Schema (Maybe Text)
pattern

instance {-# OVERLAPPABLE #-} HasSchema s Schema
  => HasMaxItems s (Maybe Integer) where
  maxItems :: Lens' s (Maybe Integer)
maxItems = (Schema -> f Schema) -> s -> f s
forall s a. HasSchema s a => Lens' s a
Lens' s Schema
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
Lens' Schema (Maybe Integer)
maxItems

instance {-# OVERLAPPABLE #-} HasSchema s Schema
  => HasMinItems s (Maybe Integer) where
  minItems :: Lens' s (Maybe Integer)
minItems = (Schema -> f Schema) -> s -> f s
forall s a. HasSchema s a => Lens' s a
Lens' s Schema
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
Lens' Schema (Maybe Integer)
minItems

instance {-# OVERLAPPABLE #-} HasSchema s Schema
  => HasUniqueItems s (Maybe Bool) where
  uniqueItems :: Lens' s (Maybe Bool)
uniqueItems = (Schema -> f Schema) -> s -> f s
forall s a. HasSchema s a => Lens' s a
Lens' s Schema
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
Lens' Schema (Maybe Bool)
uniqueItems

instance {-# OVERLAPPABLE #-} HasSchema s Schema
  => HasEnum s (Maybe [Value]) where
  enum_ :: Lens' s (Maybe [Value])
enum_ = (Schema -> f Schema) -> s -> f s
forall s a. HasSchema s a => Lens' s a
Lens' s Schema
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
Lens' Schema (Maybe [Value])
enum_

instance {-# OVERLAPPABLE #-} HasSchema s Schema
  => HasMultipleOf s (Maybe Scientific) where
  multipleOf :: Lens' s (Maybe Scientific)
multipleOf = (Schema -> f Schema) -> s -> f s
forall s a. HasSchema s a => Lens' s a
Lens' s Schema
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
Lens' Schema (Maybe Scientific)
multipleOf