{-# 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.DataBrew.Types.Rule
-- 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.DataBrew.Types.Rule where

import qualified Amazonka.Core as Core
import qualified Amazonka.Core.Lens.Internal as Lens
import qualified Amazonka.Data as Data
import Amazonka.DataBrew.Types.ColumnSelector
import Amazonka.DataBrew.Types.Threshold
import qualified Amazonka.Prelude as Prelude

-- | Represents a single data quality requirement that should be validated in
-- the scope of this dataset.
--
-- /See:/ 'newRule' smart constructor.
data Rule = Rule'
  { -- | List of column selectors. Selectors can be used to select columns using
    -- a name or regular expression from the dataset. Rule will be applied to
    -- selected columns.
    Rule -> Maybe (NonEmpty ColumnSelector)
columnSelectors :: Prelude.Maybe (Prelude.NonEmpty ColumnSelector),
    -- | A value that specifies whether the rule is disabled. Once a rule is
    -- disabled, a profile job will not validate it during a job run. Default
    -- value is false.
    Rule -> Maybe Bool
disabled :: Prelude.Maybe Prelude.Bool,
    -- | The map of substitution variable names to their values used in a check
    -- expression. Variable names should start with a \':\' (colon). Variable
    -- values can either be actual values or column names. To differentiate
    -- between the two, column names should be enclosed in backticks, for
    -- example, @\":col1\": \"\`Column A\`\".@
    Rule -> Maybe (HashMap Text Text)
substitutionMap :: Prelude.Maybe (Prelude.HashMap Prelude.Text Prelude.Text),
    -- | The threshold used with a non-aggregate check expression. Non-aggregate
    -- check expressions will be applied to each row in a specific column, and
    -- the threshold will be used to determine whether the validation succeeds.
    Rule -> Maybe Threshold
threshold :: Prelude.Maybe Threshold,
    -- | The name of the rule.
    Rule -> Text
name :: Prelude.Text,
    -- | The expression which includes column references, condition names
    -- followed by variable references, possibly grouped and combined with
    -- other conditions. For example,
    -- @(:col1 starts_with :prefix1 or :col1 starts_with :prefix2) and (:col1 ends_with :suffix1 or :col1 ends_with :suffix2)@.
    -- Column and value references are substitution variables that should start
    -- with the \':\' symbol. Depending on the context, substitution
    -- variables\' values can be either an actual value or a column name. These
    -- values are defined in the SubstitutionMap. If a CheckExpression starts
    -- with a column reference, then ColumnSelectors in the rule should be
    -- null. If ColumnSelectors has been defined, then there should be no
    -- column reference in the left side of a condition, for example,
    -- @is_between :val1 and :val2@.
    --
    -- For more information, see
    -- <https://docs.aws.amazon.com/databrew/latest/dg/profile.data-quality-available-checks.html Available checks>
    Rule -> Text
checkExpression :: Prelude.Text
  }
  deriving (Rule -> Rule -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Rule -> Rule -> Bool
$c/= :: Rule -> Rule -> Bool
== :: Rule -> Rule -> Bool
$c== :: Rule -> Rule -> Bool
Prelude.Eq, ReadPrec [Rule]
ReadPrec Rule
Int -> ReadS Rule
ReadS [Rule]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [Rule]
$creadListPrec :: ReadPrec [Rule]
readPrec :: ReadPrec Rule
$creadPrec :: ReadPrec Rule
readList :: ReadS [Rule]
$creadList :: ReadS [Rule]
readsPrec :: Int -> ReadS Rule
$creadsPrec :: Int -> ReadS Rule
Prelude.Read, Int -> Rule -> ShowS
[Rule] -> ShowS
Rule -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Rule] -> ShowS
$cshowList :: [Rule] -> ShowS
show :: Rule -> String
$cshow :: Rule -> String
showsPrec :: Int -> Rule -> ShowS
$cshowsPrec :: Int -> Rule -> ShowS
Prelude.Show, forall x. Rep Rule x -> Rule
forall x. Rule -> Rep Rule x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Rule x -> Rule
$cfrom :: forall x. Rule -> Rep Rule x
Prelude.Generic)

-- |
-- Create a value of 'Rule' 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:
--
-- 'columnSelectors', 'rule_columnSelectors' - List of column selectors. Selectors can be used to select columns using
-- a name or regular expression from the dataset. Rule will be applied to
-- selected columns.
--
-- 'disabled', 'rule_disabled' - A value that specifies whether the rule is disabled. Once a rule is
-- disabled, a profile job will not validate it during a job run. Default
-- value is false.
--
-- 'substitutionMap', 'rule_substitutionMap' - The map of substitution variable names to their values used in a check
-- expression. Variable names should start with a \':\' (colon). Variable
-- values can either be actual values or column names. To differentiate
-- between the two, column names should be enclosed in backticks, for
-- example, @\":col1\": \"\`Column A\`\".@
--
-- 'threshold', 'rule_threshold' - The threshold used with a non-aggregate check expression. Non-aggregate
-- check expressions will be applied to each row in a specific column, and
-- the threshold will be used to determine whether the validation succeeds.
--
-- 'name', 'rule_name' - The name of the rule.
--
-- 'checkExpression', 'rule_checkExpression' - The expression which includes column references, condition names
-- followed by variable references, possibly grouped and combined with
-- other conditions. For example,
-- @(:col1 starts_with :prefix1 or :col1 starts_with :prefix2) and (:col1 ends_with :suffix1 or :col1 ends_with :suffix2)@.
-- Column and value references are substitution variables that should start
-- with the \':\' symbol. Depending on the context, substitution
-- variables\' values can be either an actual value or a column name. These
-- values are defined in the SubstitutionMap. If a CheckExpression starts
-- with a column reference, then ColumnSelectors in the rule should be
-- null. If ColumnSelectors has been defined, then there should be no
-- column reference in the left side of a condition, for example,
-- @is_between :val1 and :val2@.
--
-- For more information, see
-- <https://docs.aws.amazon.com/databrew/latest/dg/profile.data-quality-available-checks.html Available checks>
newRule ::
  -- | 'name'
  Prelude.Text ->
  -- | 'checkExpression'
  Prelude.Text ->
  Rule
newRule :: Text -> Text -> Rule
newRule Text
pName_ Text
pCheckExpression_ =
  Rule'
    { $sel:columnSelectors:Rule' :: Maybe (NonEmpty ColumnSelector)
columnSelectors = forall a. Maybe a
Prelude.Nothing,
      $sel:disabled:Rule' :: Maybe Bool
disabled = forall a. Maybe a
Prelude.Nothing,
      $sel:substitutionMap:Rule' :: Maybe (HashMap Text Text)
substitutionMap = forall a. Maybe a
Prelude.Nothing,
      $sel:threshold:Rule' :: Maybe Threshold
threshold = forall a. Maybe a
Prelude.Nothing,
      $sel:name:Rule' :: Text
name = Text
pName_,
      $sel:checkExpression:Rule' :: Text
checkExpression = Text
pCheckExpression_
    }

-- | List of column selectors. Selectors can be used to select columns using
-- a name or regular expression from the dataset. Rule will be applied to
-- selected columns.
rule_columnSelectors :: Lens.Lens' Rule (Prelude.Maybe (Prelude.NonEmpty ColumnSelector))
rule_columnSelectors :: Lens' Rule (Maybe (NonEmpty ColumnSelector))
rule_columnSelectors = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\Rule' {Maybe (NonEmpty ColumnSelector)
columnSelectors :: Maybe (NonEmpty ColumnSelector)
$sel:columnSelectors:Rule' :: Rule -> Maybe (NonEmpty ColumnSelector)
columnSelectors} -> Maybe (NonEmpty ColumnSelector)
columnSelectors) (\s :: Rule
s@Rule' {} Maybe (NonEmpty ColumnSelector)
a -> Rule
s {$sel:columnSelectors:Rule' :: Maybe (NonEmpty ColumnSelector)
columnSelectors = Maybe (NonEmpty ColumnSelector)
a} :: Rule) 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

-- | A value that specifies whether the rule is disabled. Once a rule is
-- disabled, a profile job will not validate it during a job run. Default
-- value is false.
rule_disabled :: Lens.Lens' Rule (Prelude.Maybe Prelude.Bool)
rule_disabled :: Lens' Rule (Maybe Bool)
rule_disabled = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\Rule' {Maybe Bool
disabled :: Maybe Bool
$sel:disabled:Rule' :: Rule -> Maybe Bool
disabled} -> Maybe Bool
disabled) (\s :: Rule
s@Rule' {} Maybe Bool
a -> Rule
s {$sel:disabled:Rule' :: Maybe Bool
disabled = Maybe Bool
a} :: Rule)

-- | The map of substitution variable names to their values used in a check
-- expression. Variable names should start with a \':\' (colon). Variable
-- values can either be actual values or column names. To differentiate
-- between the two, column names should be enclosed in backticks, for
-- example, @\":col1\": \"\`Column A\`\".@
rule_substitutionMap :: Lens.Lens' Rule (Prelude.Maybe (Prelude.HashMap Prelude.Text Prelude.Text))
rule_substitutionMap :: Lens' Rule (Maybe (HashMap Text Text))
rule_substitutionMap = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\Rule' {Maybe (HashMap Text Text)
substitutionMap :: Maybe (HashMap Text Text)
$sel:substitutionMap:Rule' :: Rule -> Maybe (HashMap Text Text)
substitutionMap} -> Maybe (HashMap Text Text)
substitutionMap) (\s :: Rule
s@Rule' {} Maybe (HashMap Text Text)
a -> Rule
s {$sel:substitutionMap:Rule' :: Maybe (HashMap Text Text)
substitutionMap = Maybe (HashMap Text Text)
a} :: Rule) 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 threshold used with a non-aggregate check expression. Non-aggregate
-- check expressions will be applied to each row in a specific column, and
-- the threshold will be used to determine whether the validation succeeds.
rule_threshold :: Lens.Lens' Rule (Prelude.Maybe Threshold)
rule_threshold :: Lens' Rule (Maybe Threshold)
rule_threshold = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\Rule' {Maybe Threshold
threshold :: Maybe Threshold
$sel:threshold:Rule' :: Rule -> Maybe Threshold
threshold} -> Maybe Threshold
threshold) (\s :: Rule
s@Rule' {} Maybe Threshold
a -> Rule
s {$sel:threshold:Rule' :: Maybe Threshold
threshold = Maybe Threshold
a} :: Rule)

-- | The name of the rule.
rule_name :: Lens.Lens' Rule Prelude.Text
rule_name :: Lens' Rule Text
rule_name = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\Rule' {Text
name :: Text
$sel:name:Rule' :: Rule -> Text
name} -> Text
name) (\s :: Rule
s@Rule' {} Text
a -> Rule
s {$sel:name:Rule' :: Text
name = Text
a} :: Rule)

-- | The expression which includes column references, condition names
-- followed by variable references, possibly grouped and combined with
-- other conditions. For example,
-- @(:col1 starts_with :prefix1 or :col1 starts_with :prefix2) and (:col1 ends_with :suffix1 or :col1 ends_with :suffix2)@.
-- Column and value references are substitution variables that should start
-- with the \':\' symbol. Depending on the context, substitution
-- variables\' values can be either an actual value or a column name. These
-- values are defined in the SubstitutionMap. If a CheckExpression starts
-- with a column reference, then ColumnSelectors in the rule should be
-- null. If ColumnSelectors has been defined, then there should be no
-- column reference in the left side of a condition, for example,
-- @is_between :val1 and :val2@.
--
-- For more information, see
-- <https://docs.aws.amazon.com/databrew/latest/dg/profile.data-quality-available-checks.html Available checks>
rule_checkExpression :: Lens.Lens' Rule Prelude.Text
rule_checkExpression :: Lens' Rule Text
rule_checkExpression = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\Rule' {Text
checkExpression :: Text
$sel:checkExpression:Rule' :: Rule -> Text
checkExpression} -> Text
checkExpression) (\s :: Rule
s@Rule' {} Text
a -> Rule
s {$sel:checkExpression:Rule' :: Text
checkExpression = Text
a} :: Rule)

instance Data.FromJSON Rule where
  parseJSON :: Value -> Parser Rule
parseJSON =
    forall a. String -> (Object -> Parser a) -> Value -> Parser a
Data.withObject
      String
"Rule"
      ( \Object
x ->
          Maybe (NonEmpty ColumnSelector)
-> Maybe Bool
-> Maybe (HashMap Text Text)
-> Maybe Threshold
-> Text
-> Text
-> Rule
Rule'
            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
"ColumnSelectors")
            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
"Disabled")
            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
"SubstitutionMap"
                            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
"Threshold")
            forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> (Object
x forall a. FromJSON a => Object -> Key -> Parser a
Data..: Key
"Name")
            forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> (Object
x forall a. FromJSON a => Object -> Key -> Parser a
Data..: Key
"CheckExpression")
      )

instance Prelude.Hashable Rule where
  hashWithSalt :: Int -> Rule -> Int
hashWithSalt Int
_salt Rule' {Maybe Bool
Maybe (NonEmpty ColumnSelector)
Maybe (HashMap Text Text)
Maybe Threshold
Text
checkExpression :: Text
name :: Text
threshold :: Maybe Threshold
substitutionMap :: Maybe (HashMap Text Text)
disabled :: Maybe Bool
columnSelectors :: Maybe (NonEmpty ColumnSelector)
$sel:checkExpression:Rule' :: Rule -> Text
$sel:name:Rule' :: Rule -> Text
$sel:threshold:Rule' :: Rule -> Maybe Threshold
$sel:substitutionMap:Rule' :: Rule -> Maybe (HashMap Text Text)
$sel:disabled:Rule' :: Rule -> Maybe Bool
$sel:columnSelectors:Rule' :: Rule -> Maybe (NonEmpty ColumnSelector)
..} =
    Int
_salt
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` Maybe (NonEmpty ColumnSelector)
columnSelectors
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` Maybe Bool
disabled
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` Maybe (HashMap Text Text)
substitutionMap
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` Maybe Threshold
threshold
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` Text
name
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` Text
checkExpression

instance Prelude.NFData Rule where
  rnf :: Rule -> ()
rnf Rule' {Maybe Bool
Maybe (NonEmpty ColumnSelector)
Maybe (HashMap Text Text)
Maybe Threshold
Text
checkExpression :: Text
name :: Text
threshold :: Maybe Threshold
substitutionMap :: Maybe (HashMap Text Text)
disabled :: Maybe Bool
columnSelectors :: Maybe (NonEmpty ColumnSelector)
$sel:checkExpression:Rule' :: Rule -> Text
$sel:name:Rule' :: Rule -> Text
$sel:threshold:Rule' :: Rule -> Maybe Threshold
$sel:substitutionMap:Rule' :: Rule -> Maybe (HashMap Text Text)
$sel:disabled:Rule' :: Rule -> Maybe Bool
$sel:columnSelectors:Rule' :: Rule -> Maybe (NonEmpty ColumnSelector)
..} =
    forall a. NFData a => a -> ()
Prelude.rnf Maybe (NonEmpty ColumnSelector)
columnSelectors
      seq :: forall a b. a -> b -> b
`Prelude.seq` forall a. NFData a => a -> ()
Prelude.rnf Maybe Bool
disabled
      seq :: forall a b. a -> b -> b
`Prelude.seq` forall a. NFData a => a -> ()
Prelude.rnf Maybe (HashMap Text Text)
substitutionMap
      seq :: forall a b. a -> b -> b
`Prelude.seq` forall a. NFData a => a -> ()
Prelude.rnf Maybe Threshold
threshold
      seq :: forall a b. a -> b -> b
`Prelude.seq` forall a. NFData a => a -> ()
Prelude.rnf Text
name
      seq :: forall a b. a -> b -> b
`Prelude.seq` forall a. NFData a => a -> ()
Prelude.rnf Text
checkExpression

instance Data.ToJSON Rule where
  toJSON :: Rule -> Value
toJSON Rule' {Maybe Bool
Maybe (NonEmpty ColumnSelector)
Maybe (HashMap Text Text)
Maybe Threshold
Text
checkExpression :: Text
name :: Text
threshold :: Maybe Threshold
substitutionMap :: Maybe (HashMap Text Text)
disabled :: Maybe Bool
columnSelectors :: Maybe (NonEmpty ColumnSelector)
$sel:checkExpression:Rule' :: Rule -> Text
$sel:name:Rule' :: Rule -> Text
$sel:threshold:Rule' :: Rule -> Maybe Threshold
$sel:substitutionMap:Rule' :: Rule -> Maybe (HashMap Text Text)
$sel:disabled:Rule' :: Rule -> Maybe Bool
$sel:columnSelectors:Rule' :: Rule -> Maybe (NonEmpty ColumnSelector)
..} =
    [Pair] -> Value
Data.object
      ( forall a. [Maybe a] -> [a]
Prelude.catMaybes
          [ (Key
"ColumnSelectors" 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 (NonEmpty ColumnSelector)
columnSelectors,
            (Key
"Disabled" 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 Bool
disabled,
            (Key
"SubstitutionMap" 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 (HashMap Text Text)
substitutionMap,
            (Key
"Threshold" 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 Threshold
threshold,
            forall a. a -> Maybe a
Prelude.Just (Key
"Name" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
Data..= Text
name),
            forall a. a -> Maybe a
Prelude.Just
              (Key
"CheckExpression" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
Data..= Text
checkExpression)
          ]
      )