{-# 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
  = 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
  = 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 = forall s a. HasResponses s a => Lens' s a
responses forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall m. Ixed m => Index m -> Traversal' m (IxValue m)
ix Index Responses
n
instance At   Responses where at :: Index Responses -> Lens' Responses (Maybe (IxValue Responses))
at Index Responses
n = forall s a. HasResponses s a => Lens' s a
responses forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall m. At m => Index m -> Lens' m (Maybe (IxValue m))
at Index Responses
n

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

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

-- OVERLAPPABLE instances

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

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

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

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

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

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

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

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

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

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

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

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

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

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