{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE StrictData #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# OPTIONS_GHC -fno-warn-unused-imports #-}
{-# OPTIONS_GHC -fno-warn-unused-matches #-}

-- Derived from AWS service descriptions, licensed under Apache 2.0.

-- |
-- Module      : Amazonka.CostExplorer.Types.Expression
-- Copyright   : (c) 2013-2023 Brendan Hay
-- License     : Mozilla Public License, v. 2.0.
-- Maintainer  : Brendan Hay
-- Stability   : auto-generated
-- Portability : non-portable (GHC extensions)
module Amazonka.CostExplorer.Types.Expression where

import qualified Amazonka.Core as Core
import qualified Amazonka.Core.Lens.Internal as Lens
import Amazonka.CostExplorer.Types.CostCategoryValues
import Amazonka.CostExplorer.Types.DimensionValues
import Amazonka.CostExplorer.Types.TagValues
import qualified Amazonka.Data as Data
import qualified Amazonka.Prelude as Prelude

-- | Use @Expression@ to filter in various Cost Explorer APIs.
--
-- Not all @Expression@ types are supported in each API. Refer to the
-- documentation for each specific API to see what is supported.
--
-- There are two patterns:
--
-- -   Simple dimension values.
--
--     -   There are three types of simple dimension values:
--         @CostCategories@, @Tags@, and @Dimensions@.
--
--         -   Specify the @CostCategories@ field to define a filter that
--             acts on Cost Categories.
--
--         -   Specify the @Tags@ field to define a filter that acts on
--             Cost Allocation Tags.
--
--         -   Specify the @Dimensions@ field to define a filter that acts
--             on the
--             <https://docs.aws.amazon.com/aws-cost-management/latest/APIReference/API_DimensionValues.html DimensionValues>
--             .
--
--     -   For each filter type, you can set the dimension name and values
--         for the filters that you plan to use.
--
--         -   For example, you can filter for
--             @REGION==us-east-1 OR REGION==us-west-1@. For
--             @GetRightsizingRecommendation@, the Region is a full name
--             (for example, @REGION==US East (N. Virginia)@.
--
--         -   The corresponding @Expression@ for this example is as
--             follows:
--             @{ \"Dimensions\": { \"Key\": \"REGION\", \"Values\": [ \"us-east-1\", “us-west-1” ] } }@
--
--         -   As shown in the previous example, lists of dimension values
--             are combined with @OR@ when applying the filter.
--
--     -   You can also set different match options to further control how
--         the filter behaves. Not all APIs support match options. Refer to
--         the documentation for each specific API to see what is
--         supported.
--
--         -   For example, you can filter for linked account names that
--             start with “a”.
--
--         -   The corresponding @Expression@ for this example is as
--             follows:
--             @{ \"Dimensions\": { \"Key\": \"LINKED_ACCOUNT_NAME\", \"MatchOptions\": [ \"STARTS_WITH\" ], \"Values\": [ \"a\" ] } }@
--
-- -   Compound @Expression@ types with logical operations.
--
--     -   You can use multiple @Expression@ types and the logical
--         operators @AND\/OR\/NOT@ to create a list of one or more
--         @Expression@ objects. By doing this, you can filter by more
--         advanced options.
--
--     -   For example, you can filter by
--         @((REGION == us-east-1 OR REGION == us-west-1) OR (TAG.Type == Type1)) AND (USAGE_TYPE != DataTransfer)@.
--
--     -   The corresponding @Expression@ for this example is as follows:
--         @{ \"And\": [ {\"Or\": [ {\"Dimensions\": { \"Key\": \"REGION\", \"Values\": [ \"us-east-1\", \"us-west-1\" ] }}, {\"Tags\": { \"Key\": \"TagName\", \"Values\": [\"Value1\"] } } ]}, {\"Not\": {\"Dimensions\": { \"Key\": \"USAGE_TYPE\", \"Values\": [\"DataTransfer\"] }}} ] } @
--
--     Because each @Expression@ can have only one operator, the service
--     returns an error if more than one is specified. The following
--     example shows an @Expression@ object that creates an error:
--     @ { \"And\": [ ... ], \"Dimensions\": { \"Key\": \"USAGE_TYPE\", \"Values\": [ \"DataTransfer\" ] } } @
--
--     The following is an example of the corresponding error message:
--     @\"Expression has more than one roots. Only one root operator is allowed for each expression: And, Or, Not, Dimensions, Tags, CostCategories\"@
--
-- For the @GetRightsizingRecommendation@ action, a combination of OR and
-- NOT isn\'t supported. OR isn\'t supported between different dimensions,
-- or dimensions and tags. NOT operators aren\'t supported. Dimensions are
-- also limited to @LINKED_ACCOUNT@, @REGION@, or @RIGHTSIZING_TYPE@.
--
-- For the @GetReservationPurchaseRecommendation@ action, only NOT is
-- supported. AND and OR aren\'t supported. Dimensions are limited to
-- @LINKED_ACCOUNT@.
--
-- /See:/ 'newExpression' smart constructor.
data Expression = Expression'
  { -- | Return results that match both @Dimension@ objects.
    Expression -> Maybe [Expression]
and :: Prelude.Maybe [Expression],
    -- | The filter that\'s based on @CostCategory@ values.
    Expression -> Maybe CostCategoryValues
costCategories :: Prelude.Maybe CostCategoryValues,
    -- | The specific @Dimension@ to use for @Expression@.
    Expression -> Maybe DimensionValues
dimensions :: Prelude.Maybe DimensionValues,
    -- | Return results that don\'t match a @Dimension@ object.
    Expression -> Maybe Expression
not :: Prelude.Maybe Expression,
    -- | Return results that match either @Dimension@ object.
    Expression -> Maybe [Expression]
or :: Prelude.Maybe [Expression],
    -- | The specific @Tag@ to use for @Expression@.
    Expression -> Maybe TagValues
tags :: Prelude.Maybe TagValues
  }
  deriving (Expression -> Expression -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Expression -> Expression -> Bool
$c/= :: Expression -> Expression -> Bool
== :: Expression -> Expression -> Bool
$c== :: Expression -> Expression -> Bool
Prelude.Eq, ReadPrec [Expression]
ReadPrec Expression
Int -> ReadS Expression
ReadS [Expression]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [Expression]
$creadListPrec :: ReadPrec [Expression]
readPrec :: ReadPrec Expression
$creadPrec :: ReadPrec Expression
readList :: ReadS [Expression]
$creadList :: ReadS [Expression]
readsPrec :: Int -> ReadS Expression
$creadsPrec :: Int -> ReadS Expression
Prelude.Read, Int -> Expression -> ShowS
[Expression] -> ShowS
Expression -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Expression] -> ShowS
$cshowList :: [Expression] -> ShowS
show :: Expression -> String
$cshow :: Expression -> String
showsPrec :: Int -> Expression -> ShowS
$cshowsPrec :: Int -> Expression -> ShowS
Prelude.Show, forall x. Rep Expression x -> Expression
forall x. Expression -> Rep Expression x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Expression x -> Expression
$cfrom :: forall x. Expression -> Rep Expression x
Prelude.Generic)

-- |
-- Create a value of 'Expression' with all optional fields omitted.
--
-- Use <https://hackage.haskell.org/package/generic-lens generic-lens> or <https://hackage.haskell.org/package/optics optics> to modify other optional fields.
--
-- The following record fields are available, with the corresponding lenses provided
-- for backwards compatibility:
--
-- 'and', 'expression_and' - Return results that match both @Dimension@ objects.
--
-- 'costCategories', 'expression_costCategories' - The filter that\'s based on @CostCategory@ values.
--
-- 'dimensions', 'expression_dimensions' - The specific @Dimension@ to use for @Expression@.
--
-- 'not', 'expression_not' - Return results that don\'t match a @Dimension@ object.
--
-- 'or', 'expression_or' - Return results that match either @Dimension@ object.
--
-- 'tags', 'expression_tags' - The specific @Tag@ to use for @Expression@.
newExpression ::
  Expression
newExpression :: Expression
newExpression =
  Expression'
    { $sel:and:Expression' :: Maybe [Expression]
and = forall a. Maybe a
Prelude.Nothing,
      $sel:costCategories:Expression' :: Maybe CostCategoryValues
costCategories = forall a. Maybe a
Prelude.Nothing,
      $sel:dimensions:Expression' :: Maybe DimensionValues
dimensions = forall a. Maybe a
Prelude.Nothing,
      $sel:not:Expression' :: Maybe Expression
not = forall a. Maybe a
Prelude.Nothing,
      $sel:or:Expression' :: Maybe [Expression]
or = forall a. Maybe a
Prelude.Nothing,
      $sel:tags:Expression' :: Maybe TagValues
tags = forall a. Maybe a
Prelude.Nothing
    }

-- | Return results that match both @Dimension@ objects.
expression_and :: Lens.Lens' Expression (Prelude.Maybe [Expression])
expression_and :: Lens' Expression (Maybe [Expression])
expression_and = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\Expression' {Maybe [Expression]
and :: Maybe [Expression]
$sel:and:Expression' :: Expression -> Maybe [Expression]
and} -> Maybe [Expression]
and) (\s :: Expression
s@Expression' {} Maybe [Expression]
a -> Expression
s {$sel:and:Expression' :: Maybe [Expression]
and = Maybe [Expression]
a} :: Expression) forall b c a. (b -> c) -> (a -> b) -> a -> c
Prelude.. forall (f :: * -> *) (g :: * -> *) s t a b.
(Functor f, Functor g) =>
AnIso s t a b -> Iso (f s) (g t) (f a) (g b)
Lens.mapping forall s t a b. (Coercible s a, Coercible t b) => Iso s t a b
Lens.coerced

-- | The filter that\'s based on @CostCategory@ values.
expression_costCategories :: Lens.Lens' Expression (Prelude.Maybe CostCategoryValues)
expression_costCategories :: Lens' Expression (Maybe CostCategoryValues)
expression_costCategories = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\Expression' {Maybe CostCategoryValues
costCategories :: Maybe CostCategoryValues
$sel:costCategories:Expression' :: Expression -> Maybe CostCategoryValues
costCategories} -> Maybe CostCategoryValues
costCategories) (\s :: Expression
s@Expression' {} Maybe CostCategoryValues
a -> Expression
s {$sel:costCategories:Expression' :: Maybe CostCategoryValues
costCategories = Maybe CostCategoryValues
a} :: Expression)

-- | The specific @Dimension@ to use for @Expression@.
expression_dimensions :: Lens.Lens' Expression (Prelude.Maybe DimensionValues)
expression_dimensions :: Lens' Expression (Maybe DimensionValues)
expression_dimensions = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\Expression' {Maybe DimensionValues
dimensions :: Maybe DimensionValues
$sel:dimensions:Expression' :: Expression -> Maybe DimensionValues
dimensions} -> Maybe DimensionValues
dimensions) (\s :: Expression
s@Expression' {} Maybe DimensionValues
a -> Expression
s {$sel:dimensions:Expression' :: Maybe DimensionValues
dimensions = Maybe DimensionValues
a} :: Expression)

-- | Return results that don\'t match a @Dimension@ object.
expression_not :: Lens.Lens' Expression (Prelude.Maybe Expression)
expression_not :: Lens' Expression (Maybe Expression)
expression_not = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\Expression' {Maybe Expression
not :: Maybe Expression
$sel:not:Expression' :: Expression -> Maybe Expression
not} -> Maybe Expression
not) (\s :: Expression
s@Expression' {} Maybe Expression
a -> Expression
s {$sel:not:Expression' :: Maybe Expression
not = Maybe Expression
a} :: Expression)

-- | Return results that match either @Dimension@ object.
expression_or :: Lens.Lens' Expression (Prelude.Maybe [Expression])
expression_or :: Lens' Expression (Maybe [Expression])
expression_or = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\Expression' {Maybe [Expression]
or :: Maybe [Expression]
$sel:or:Expression' :: Expression -> Maybe [Expression]
or} -> Maybe [Expression]
or) (\s :: Expression
s@Expression' {} Maybe [Expression]
a -> Expression
s {$sel:or:Expression' :: Maybe [Expression]
or = Maybe [Expression]
a} :: Expression) forall b c a. (b -> c) -> (a -> b) -> a -> c
Prelude.. forall (f :: * -> *) (g :: * -> *) s t a b.
(Functor f, Functor g) =>
AnIso s t a b -> Iso (f s) (g t) (f a) (g b)
Lens.mapping forall s t a b. (Coercible s a, Coercible t b) => Iso s t a b
Lens.coerced

-- | The specific @Tag@ to use for @Expression@.
expression_tags :: Lens.Lens' Expression (Prelude.Maybe TagValues)
expression_tags :: Lens' Expression (Maybe TagValues)
expression_tags = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\Expression' {Maybe TagValues
tags :: Maybe TagValues
$sel:tags:Expression' :: Expression -> Maybe TagValues
tags} -> Maybe TagValues
tags) (\s :: Expression
s@Expression' {} Maybe TagValues
a -> Expression
s {$sel:tags:Expression' :: Maybe TagValues
tags = Maybe TagValues
a} :: Expression)

instance Data.FromJSON Expression where
  parseJSON :: Value -> Parser Expression
parseJSON =
    forall a. String -> (Object -> Parser a) -> Value -> Parser a
Data.withObject
      String
"Expression"
      ( \Object
x ->
          Maybe [Expression]
-> Maybe CostCategoryValues
-> Maybe DimensionValues
-> Maybe Expression
-> Maybe [Expression]
-> Maybe TagValues
-> Expression
Expression'
            forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> (Object
x forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
Data..:? Key
"And" forall a. Parser (Maybe a) -> a -> Parser a
Data..!= forall a. Monoid a => a
Prelude.mempty)
            forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> (Object
x forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
Data..:? Key
"CostCategories")
            forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> (Object
x forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
Data..:? Key
"Dimensions")
            forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> (Object
x forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
Data..:? Key
"Not")
            forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> (Object
x forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
Data..:? Key
"Or" forall a. Parser (Maybe a) -> a -> Parser a
Data..!= forall a. Monoid a => a
Prelude.mempty)
            forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> (Object
x forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
Data..:? Key
"Tags")
      )

instance Prelude.Hashable Expression where
  hashWithSalt :: Int -> Expression -> Int
hashWithSalt Int
_salt Expression' {Maybe [Expression]
Maybe DimensionValues
Maybe CostCategoryValues
Maybe TagValues
Maybe Expression
tags :: Maybe TagValues
or :: Maybe [Expression]
not :: Maybe Expression
dimensions :: Maybe DimensionValues
costCategories :: Maybe CostCategoryValues
and :: Maybe [Expression]
$sel:tags:Expression' :: Expression -> Maybe TagValues
$sel:or:Expression' :: Expression -> Maybe [Expression]
$sel:not:Expression' :: Expression -> Maybe Expression
$sel:dimensions:Expression' :: Expression -> Maybe DimensionValues
$sel:costCategories:Expression' :: Expression -> Maybe CostCategoryValues
$sel:and:Expression' :: Expression -> Maybe [Expression]
..} =
    Int
_salt
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` Maybe [Expression]
and
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` Maybe CostCategoryValues
costCategories
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` Maybe DimensionValues
dimensions
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` Maybe Expression
not
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` Maybe [Expression]
or
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` Maybe TagValues
tags

instance Prelude.NFData Expression where
  rnf :: Expression -> ()
rnf Expression' {Maybe [Expression]
Maybe DimensionValues
Maybe CostCategoryValues
Maybe TagValues
Maybe Expression
tags :: Maybe TagValues
or :: Maybe [Expression]
not :: Maybe Expression
dimensions :: Maybe DimensionValues
costCategories :: Maybe CostCategoryValues
and :: Maybe [Expression]
$sel:tags:Expression' :: Expression -> Maybe TagValues
$sel:or:Expression' :: Expression -> Maybe [Expression]
$sel:not:Expression' :: Expression -> Maybe Expression
$sel:dimensions:Expression' :: Expression -> Maybe DimensionValues
$sel:costCategories:Expression' :: Expression -> Maybe CostCategoryValues
$sel:and:Expression' :: Expression -> Maybe [Expression]
..} =
    forall a. NFData a => a -> ()
Prelude.rnf Maybe [Expression]
and
      seq :: forall a b. a -> b -> b
`Prelude.seq` forall a. NFData a => a -> ()
Prelude.rnf Maybe CostCategoryValues
costCategories
      seq :: forall a b. a -> b -> b
`Prelude.seq` forall a. NFData a => a -> ()
Prelude.rnf Maybe DimensionValues
dimensions
      seq :: forall a b. a -> b -> b
`Prelude.seq` forall a. NFData a => a -> ()
Prelude.rnf Maybe Expression
not
      seq :: forall a b. a -> b -> b
`Prelude.seq` forall a. NFData a => a -> ()
Prelude.rnf Maybe [Expression]
or
      seq :: forall a b. a -> b -> b
`Prelude.seq` forall a. NFData a => a -> ()
Prelude.rnf Maybe TagValues
tags

instance Data.ToJSON Expression where
  toJSON :: Expression -> Value
toJSON Expression' {Maybe [Expression]
Maybe DimensionValues
Maybe CostCategoryValues
Maybe TagValues
Maybe Expression
tags :: Maybe TagValues
or :: Maybe [Expression]
not :: Maybe Expression
dimensions :: Maybe DimensionValues
costCategories :: Maybe CostCategoryValues
and :: Maybe [Expression]
$sel:tags:Expression' :: Expression -> Maybe TagValues
$sel:or:Expression' :: Expression -> Maybe [Expression]
$sel:not:Expression' :: Expression -> Maybe Expression
$sel:dimensions:Expression' :: Expression -> Maybe DimensionValues
$sel:costCategories:Expression' :: Expression -> Maybe CostCategoryValues
$sel:and:Expression' :: Expression -> Maybe [Expression]
..} =
    [Pair] -> Value
Data.object
      ( forall a. [Maybe a] -> [a]
Prelude.catMaybes
          [ (Key
"And" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
Data..=) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> Maybe [Expression]
and,
            (Key
"CostCategories" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
Data..=)
              forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> Maybe CostCategoryValues
costCategories,
            (Key
"Dimensions" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
Data..=) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> Maybe DimensionValues
dimensions,
            (Key
"Not" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
Data..=) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> Maybe Expression
not,
            (Key
"Or" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
Data..=) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> Maybe [Expression]
or,
            (Key
"Tags" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
Data..=) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> Maybe TagValues
tags
          ]
      )