Debian Codegraph @

doc/antora/modules/unlang/pages/switch.adoc

raw
1 = The switch Statement
2
3 .Syntax
4 [source,unlang]
5 ----
6 switch <expansion> {
7 case <match-1> {
8 [ statements-1 ]
9 }
10 case <match-2> {
11 [ statements-2 ]
12 }
13 case {
14 [ statements-3 ]
15 }
16 }
17 ----
18
19 A `switch` statement causes the server to evaluate _expansion_, which
20 can be an xref:attr.adoc[&Attribute-Name] or
21 xref:condition/operands.adoc[data]. The result is compared against _match-1_
22 and _match-2_ to find a match. If no string matches, then the server
23 looks for the default xref:case.adoc[case] statement, which has no
24 associated match.
25
26 The matching is done via equality. The `switch` statement is mostly
27 syntactic sugar and is used to simplify the visual form of the
28 configuration. It is mostly equivalent to the following use of
29 xref:if.adoc[if] statements:
30
31 .Nearly equivalent syntax
32 [source,unlang]
33 ----
34 if (<expansion> == <match-1>) {
35 [ statements-1 ]
36 }
37 elsif (<expansion> == <match-2>) {
38 [ statements-2 ]
39 }
40 else {
41 [ statements-3 ]
42 }
43 ----
44
45 The only difference between the two forms is that for a `switch`
46 statement, the _expansion_ is evaluated only once. For the equivalent
47 xref:if.adoc[if] statement, the _expansion_ is evaluated again for every
48 xref:if.adoc[if].
49
50 If a matching xref:case.adoc[case] is found, the statements within
51 that xref:case.adoc[case] are evaluated. If no matching
52 xref:case.adoc[case] is found, the `case` section with no "match" is
53 evaluated. If there is no such `case { ...}` subsection, then the
54 `switch` statement behaves as if the `case {...}` section was empty.
55
56 ////
57 For compatibility with version 3, and empty `case` statement can also
58 be used instead of `default`.
59 ////
60
61 The _match_ text for the xref:case.adoc[case] statement can be an
62 xref:attr.adoc[&Attribute-Name] or xref:type/index.adoc[data].
63
64 No statement other than xref:case.adoc[case] can appear in a `switch`
65 statement, and the xref:case.adoc[case] statement cannot appear outside of a
66 `switch` statement.
67
68 .Example
69 [source,unlang]
70 ----
71 switch &User-Name {
72 case "bob" {
73 reject
74 }
75
76 case {
77 ok
78 }
79 }
80 ----
81
82 // Copyright (C) 2020 Network RADIUS SAS. Licenced under CC-by-NC 4.0.
83 // Development of this documentation was sponsored by Network RADIUS SAS.