github.com/Jeffail/benthos/v3@v3.65.0/website/docs/components/processors/switch.md (about)

     1  ---
     2  title: switch
     3  type: processor
     4  status: stable
     5  categories: ["Composition"]
     6  ---
     7  
     8  <!--
     9       THIS FILE IS AUTOGENERATED!
    10  
    11       To make changes please edit the contents of:
    12       lib/processor/switch.go
    13  -->
    14  
    15  import Tabs from '@theme/Tabs';
    16  import TabItem from '@theme/TabItem';
    17  
    18  
    19  Conditionally processes messages based on their contents.
    20  
    21  ```yaml
    22  # Config fields, showing default values
    23  label: ""
    24  switch: []
    25  ```
    26  
    27  For each switch case a [Bloblang query](/docs/guides/bloblang/about/) is checked and, if the result is true (or the check is empty) the child processors are executed on the message.
    28  
    29  ## Fields
    30  
    31  ### `[].check`
    32  
    33  A [Bloblang query](/docs/guides/bloblang/about/) that should return a boolean value indicating whether a message should have the processors of this case executed on it. If left empty the case always passes. If the check mapping throws an error the message will be flagged [as having failed](/docs/configuration/error_handling) and will not be tested against any other cases.
    34  
    35  
    36  Type: `string`  
    37  Default: `""`  
    38  
    39  ```yaml
    40  # Examples
    41  
    42  check: this.type == "foo"
    43  
    44  check: this.contents.urls.contains("https://benthos.dev/")
    45  ```
    46  
    47  ### `[].processors`
    48  
    49  A list of [processors](/docs/components/processors/about/) to execute on a message.
    50  
    51  
    52  Type: `array`  
    53  Default: `[]`  
    54  
    55  ### `[].fallthrough`
    56  
    57  Indicates whether, if this case passes for a message, the next case should also be executed.
    58  
    59  
    60  Type: `bool`  
    61  Default: `false`  
    62  
    63  ## Examples
    64  
    65  <Tabs defaultValue="I Hate George" values={[
    66  { label: 'I Hate George', value: 'I Hate George', },
    67  ]}>
    68  
    69  <TabItem value="I Hate George">
    70  
    71  
    72  We have a system where we're counting a metric for all messages that pass through our system. However, occasionally we get messages from George where he's rambling about dumb stuff we don't care about.
    73  
    74  For Georges messages we want to instead emit a metric that gauges how angry he is about being ignored and then we drop it.
    75  
    76  ```yaml
    77  pipeline:
    78    processors:
    79      - switch:
    80          - check: this.user.name.first != "George"
    81            processors:
    82              - metric:
    83                  type: counter
    84                  name: MessagesWeCareAbout
    85  
    86          - processors:
    87              - metric:
    88                  type: gauge
    89                  name: GeorgesAnger
    90                  value: ${! json("user.anger") }
    91              - bloblang: root = deleted()
    92  ```
    93  
    94  </TabItem>
    95  </Tabs>
    96  
    97  ## Batching
    98  
    99  When a switch processor executes on a [batch of messages](/docs/configuration/batching/) they are checked individually and can be matched independently against cases. During processing the messages matched against a case are processed as a batch, although the ordering of messages during case processing cannot be guaranteed to match the order as received.
   100  
   101  At the end of switch processing the resulting batch will follow the same ordering as the batch was received. If any child processors have split or otherwise grouped messages this grouping will be lost as the result of a switch is always a single batch. In order to perform conditional grouping and/or splitting use the [`group_by` processor](/docs/components/processors/group_by/).
   102