Repository navigation
Expand file tree
/
Copy pathstructure.html
More file actions
514 lines (436 loc) · 33 KB
/
Copy pathstructure.html
File metadata and controls
514 lines (436 loc) · 33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
<!DOCTYPE html>
<html lang="en" data-content_root="./">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Architecture and Design — CEL in Python documentation</title>
<link rel="stylesheet" type="text/css" href="_static/pygments.css?v=5ecbeea2" />
<link rel="stylesheet" type="text/css" href="_static/basic.css?v=b08954a9" />
<link rel="stylesheet" type="text/css" href="_static/alabaster.css?v=27fed22d" />
<script src="_static/documentation_options.js?v=5929fcd5"></script>
<script src="_static/doctools.js?v=fd6eb6e6"></script>
<script src="_static/sphinx_highlight.js?v=6ffebe34"></script>
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="next" title="API Reference" href="api.html" />
<link rel="prev" title="Application Integration" href="integration.html" />
<link rel="stylesheet" href="_static/custom.css" type="text/css" />
</head><body>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<section id="architecture-and-design">
<span id="data-structures"></span><h1>Architecture and Design<a class="headerlink" href="#architecture-and-design" title="Link to this heading">¶</a></h1>
<p>We’ll start with the C4 views:</p>
<ul>
<li><p><cite>Context</cite></p></li>
<li><p><cite>Container</cite> – this isn’t too interesting, but it can help to see this.</p></li>
<li><p><cite>Components</cite></p>
<p>This is a collection of various design notes describing some implementation details.</p>
<ul class="simple">
<li><p><a class="reference internal" href="#compile-time">Compile-Time</a></p></li>
<li><p><a class="reference internal" href="#evaluation-time">Evaluation-Time</a></p></li>
<li><p><a class="reference internal" href="#cel-types">CEL Types</a></p></li>
<li><p><a class="reference internal" href="#transpiler-missing-names">Transpiler Missing Names</a></p></li>
<li><p><a class="reference internal" href="#the-member-dot-production">The member-dot Production</a></p></li>
</ul>
</li>
</ul>
<p>The code view is in the <a class="reference internal" href="api.html#api-reference"><span class="std std-ref">API Reference</span></a> section.</p>
<section id="context">
<h2>Context<a class="headerlink" href="#context" title="Link to this heading">¶</a></h2>
<p>There are two distinct contexts for CEL Python:</p>
<ul class="simple">
<li><p>The CLI – as a stand-alone application.</p></li>
<li><p>As an importable module to provide expressions to a DSL.</p></li>
</ul>
<p class="plantuml">
<img src="_images/plantuml-c970d97dc7e0a41eb7666fff5d466440fc8d67cf.png" alt="@startuml
skinparam actorStyle awesome
left to right direction
package celpy {
package library {
usecase lib1 as "extend DSL with expressions"
usecase lib2 as "create program"
usecase lib3 as "evaluate program in context"
lib1 --> lib2
lib1 --> lib3
}
package cli {
usecase cli1 as "**expr** features
---
Use the -n option"
usecase cli2 as "**test** features
---
Use the -nb options"
usecase cli3 as "**jq** features
Newline-Delimited or single JSON doc"
usecase cli4 as "interactive computation
---
use the -i option"
}
}
actor app as "some app with a DSL"
app --> lib1
actor bash as "shell script"
bash --> cli1
bash --> cli2
bash --> cli3
actor user
user --> cli4
app <|- [c7n]
@enduml"/>
</p>
<p>From the CLI, the <code class="docutils literal notranslate"><span class="pre">celpy</span></code> application has a number of use cases:</p>
<ul class="simple">
<li><p>A shell script can use <code class="docutils literal notranslate"><span class="pre">celpy</span></code> as a command to replace other shell commands, including <strong>expr</strong>, <strong>test</strong>, and <strong>jq</strong>.</p></li>
<li><p>A person can run <code class="docutils literal notranslate"><span class="pre">celpy</span></code> interactively.
This allows experimentation.
It also supports exploring very complex JSON documents to understand their structure.</p></li>
</ul>
<p>As a library, an application (for example, C7N) can import <code class="docutils literal notranslate"><span class="pre">celpy</span></code> to provide an expression feature for the DSL.
This provides well-defined semantics, and widely-used syntax for the expression language.
There’s an explicit separation between building a program and executing the program to allow caching an expression for multiple executions without the overhead of building a Lark parser or compiling the expression.</p>
</section>
<section id="container">
<h2>Container<a class="headerlink" href="#container" title="Link to this heading">¶</a></h2>
<p>As a CLI, this is part of a shell script. It runs where the script runs.</p>
<p>As a library, this is improted into the application to extend the DSL.</p>
<p>There are no services offered or used.</p>
</section>
<section id="components">
<h2>Components<a class="headerlink" href="#components" title="Link to this heading">¶</a></h2>
<p>The Python code base has a number of modules.</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">__init__</span></code> – the <code class="docutils literal notranslate"><span class="pre">celpy</span></code> package as a whole.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">__main__</span></code> – the main applications used when running <code class="docutils literal notranslate"><span class="pre">celpy</span></code>.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">celparser</span></code> – a <strong>Facade</strong> for the Lark parser.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">evaluation</span></code> – a <strong>Facade</strong> for run-time evaluation.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">celtypes</span></code> – the underlying Python implementations of CEL data structures.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">c7nlib</span></code>– a collection of components the C7N can use to introduce CEL filters.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">adapter</span></code> – Some JSON serialization components.</p></li>
</ul>
<p>Here’s the conceptual organiation</p>
<p class="plantuml">
<img src="_images/plantuml-01f21512c6b94e19efccb4676b518bed63780ddd.png" alt="@startuml
package celpy {
component "~__init__" as init
component "~__main__" as main
component adapter
component c7nlib
component celparser
component celtypes
component evaluation
component cel.lark
}
init --> celtypes
init --> adapter
init --> celparser
init--> evaluation
main --> init
main --> celparser
main --> adapter
main --> evaluation
adapter --> celtypes
c7nlib --> evaluation
c7nlib --> adapter
c7nlib --> celtypes
c7nlib --> init
celparser --> cel.lark
celparser --> lark
evaluation --> lark
evaluation --> celtypes
package lark {
}
@enduml"/>
</p>
<p>While there is a tangle of dependencies, there are three top-level “entry points” for <code class="docutils literal notranslate"><span class="pre">celpy</span></code>.</p>
<ul class="simple">
<li><p>The <code class="docutils literal notranslate"><span class="pre">__main__</span></code> module is the CLI application.</p></li>
<li><p>The <code class="docutils literal notranslate"><span class="pre">c7nlib</span></code> module exposes CEL functionality in a form usable by Cloud Custodian filter definitions.
This library provides useful components to perform Custodian-related computations.</p></li>
<li><p>The <code class="docutils literal notranslate"><span class="pre">__init__</span></code> module is exposes the most useful parts of <code class="docutils literal notranslate"><span class="pre">celpy</span></code> for integration woth another application.</p></li>
</ul>
<section id="compile-time">
<h3>Compile-Time<a class="headerlink" href="#compile-time" title="Link to this heading">¶</a></h3>
<p>Here are the essential classes used to compile a CEL expression and prepare it for evaluation.</p>
<p class="plantuml">
<img src="_images/plantuml-60c95188891b5d1267db67bb61a17e9fd7c08060.png" alt="@startuml
hide empty members
class Environment {
package: str
annotations: dict[str, Annotation]
compile(text: str) -> lark.Tree
program(expr: lark.Tree, functions: dict) -> Runner
}
class celparser.CELParser{
parse(text: str)
}
Environment *-- CELParser
class lark.Tree {}
CELParser --> lark.Tree : "Creates"
abstract class Runner {
ast: Tree
evaluate(context: Context) -> Value
}
Environment --> Runner : "Creates"
Runner o-- lark.Tree
Runner o-- "0..m" CELFunction
class InterpretedRunner
Runner <|-- InterpretedRunner
class evaluation.Evaluator
InterpretedRunner *-- Evaluator
class CompiledRunner
Runner <|-- CompiledRunner
class evaluation.Transpiler
CompiledRunner *-- Transpiler
class evaluation.Context << (T,orchid) Type>> {
key: str
value: Result | NameContainer
}
Runner o--- "0..m" Context
class CELFunction <<Callable>>
class Annotation << (T,orchid) Type>>
Environment o-- "0..m" Annotation
class TypeType
Annotation <|-- TypeType
Annotation <|-- CELFunction
@enduml"/>
</p>
<p>The fundamental sequence of operations is</p>
<ol class="arabic simple">
<li><p>Create an <code class="xref py py-class docutils literal notranslate"><span class="pre">celpy.Environment</span></code> with any needed <code class="xref py py-class docutils literal notranslate"><span class="pre">celpy.Annotation</span></code> instances.
For the most part, these are based on the overall application domain.
Any type definitions should be subclasses of <code class="xref py py-class docutils literal notranslate"><span class="pre">celpy.TypeType</span></code> or a callable function defined by the <code class="xref py py-class docutils literal notranslate"><span class="pre">celpy.CELFunction</span></code> type.</p></li>
<li><p>Use the <code class="xref py py-class docutils literal notranslate"><span class="pre">celpy.Environment</span></code> to compile the CEL text to create a parse tree.</p></li>
<li><p>Use the <code class="xref py py-class docutils literal notranslate"><span class="pre">celpy.Environment</span></code> to create a <code class="xref py py-class docutils literal notranslate"><span class="pre">celpy.Runner</span></code> instance from the parse tree and any function definitions that override or extend the predefined CEL environment.</p></li>
<li><p>Evaluate the <code class="xref py py-class docutils literal notranslate"><span class="pre">celpy.Runner</span></code> with a <code class="xref py py-class docutils literal notranslate"><span class="pre">celpy.Context</span></code>.
The <code class="xref py py-class docutils literal notranslate"><span class="pre">celpy.Context</span></code> provides specific values for variables required for evaluation.
Generally, each variable should have an <code class="xref py py-class docutils literal notranslate"><span class="pre">celpy.Annotation</span></code> defined in the <code class="xref py py-class docutils literal notranslate"><span class="pre">celpy.Environment</span></code>.</p></li>
</ol>
<p>The <code class="xref py py-class docutils literal notranslate"><span class="pre">celpy.Runner</span></code> can be evaluated with any number of distinct <code class="xref py py-class docutils literal notranslate"><span class="pre">celpy.Context</span></code> values.
This amortizes the cost of compilation over multiple executions.</p>
</section>
<section id="evaluation-time">
<h3>Evaluation-Time<a class="headerlink" href="#evaluation-time" title="Link to this heading">¶</a></h3>
<p>Here’s the classes to evaluate a CEL expression.</p>
<p class="plantuml">
<img src="_images/plantuml-165a23eb11f806bdd308ffac47a78125152ade58.png" alt="@startuml
hide empty members
abstract class Runner {
ast: Tree
evaluate(context: Context) -> Value
}
Environment --- Runner : "Created By <"
Runner o-- "0..m" CELFunction
Runner o-- Context
class lark.Tree
Tree --* Runner
class InterpretedRunner <<Adapter>>
Runner <|-- InterpretedRunner
abstract class lark.Interpreter
class evaluation.Evaluator {
activation: Activation
functions: dict[str, CELFunction]
evaluate() -> Value
}
lark.Interpreter <|--- Evaluator
InterpretedRunner *-- Evaluator
class CompiledRunner <<Adapter>>
Runner <|-- CompiledRunner
InterpretedRunner -[hidden]> CompiledRunner
class evaluation.Transpiler {
functions: dict[str, CELFunction]
transpile()
evaluate() -> Value
}
CompiledRunner *-- Transpiler
lark.Interpreter <|--- Transpiler
class evaluation.Activation {
annotations: Annotation
identifiers: dict[str, Result | CELFunction]
}
Runner *-- Activation : "Uses"
Runner --> Activation : "Creates"
Activation --> Activation : "based on"
class Annotation << (T,orchid) Type>>
Runner *-- "0..m" Annotation
Annotation --o Activation : Initializes
CELFunction --o Activation : Initializes
Context --o Activation : Initializes
@enduml"/>
</p>
<p>The evalation of the CEL expression is done via a <code class="xref py py-class docutils literal notranslate"><span class="pre">celpy.Runner</span></code> object.
There are two <code class="xref py py-class docutils literal notranslate"><span class="pre">celpy.Runner</span></code> implementations.</p>
<ul class="simple">
<li><p>The <code class="xref py py-class docutils literal notranslate"><span class="pre">celpy.InterpretedRunner</span></code> walks the AST, creating the final result <code class="xref py py-class docutils literal notranslate"><span class="pre">celpy.Value</span></code> or <code class="xref py py-class docutils literal notranslate"><span class="pre">celpy.CELEvalError</span></code> exception.
This uses a <a class="reference internal" href="api.html#celpy.evaluation.Activation" title="celpy.evaluation.Activation"><code class="xref py py-class docutils literal notranslate"><span class="pre">celpy.evaluation.Activation</span></code></a> to perform the evaluation.</p></li>
<li><p>The <code class="xref py py-class docutils literal notranslate"><span class="pre">celpy.CompiledRunner</span></code> transpiles the AST into a Python sequence of statements.
The internal <code class="xref py py-func docutils literal notranslate"><span class="pre">compile()</span></code> creates a code object that can then be evaluated with a given <a class="reference internal" href="api.html#celpy.evaluation.Activation" title="celpy.evaluation.Activation"><code class="xref py py-class docutils literal notranslate"><span class="pre">celpy.evaluation.Activation</span></code></a>
The internal <code class="xref py py-func docutils literal notranslate"><span class="pre">exec()</span></code> functions performs the evaluation.</p></li>
</ul>
<p>The subclasses of <code class="xref py py-class docutils literal notranslate"><span class="pre">celpy.Runner</span></code> are <strong>Adapter</strong> classes to provide a tidy interface to the somewhat more complex <code class="xref py py-class docutils literal notranslate"><span class="pre">celpy.Evaluator</span></code> or <code class="xref py py-class docutils literal notranslate"><span class="pre">celpy.Transpiler</span></code> objects.
In the case of the <code class="xref py py-class docutils literal notranslate"><span class="pre">celpy.InterpretedRunner</span></code>, evaluation involves creating an <a class="reference internal" href="api.html#celpy.evaluation.Activation" title="celpy.evaluation.Activation"><code class="xref py py-class docutils literal notranslate"><span class="pre">celpy.evaluation.Activation</span></code></a> and visiting the AST.
Whereas, the <code class="xref py py-class docutils literal notranslate"><span class="pre">celpy.CompiledRunner</span></code> must first visit the AST to create code. At evaluation time, it create an <a class="reference internal" href="api.html#celpy.evaluation.Activation" title="celpy.evaluation.Activation"><code class="xref py py-class docutils literal notranslate"><span class="pre">celpy.evaluation.Activation</span></code></a> and uses <code class="xref py py-func docutils literal notranslate"><span class="pre">exec()</span></code> to compute the final value.</p>
<p>The <a class="reference internal" href="api.html#celpy.evaluation.Activation" title="celpy.evaluation.Activation"><code class="xref py py-class docutils literal notranslate"><span class="pre">celpy.evaluation.Activation</span></code></a> contains several things:</p>
<ul class="simple">
<li><p>The <code class="xref py py-class docutils literal notranslate"><span class="pre">Annotation</span></code> definitions to provide type information for identifiers.</p></li>
<li><p>The <code class="xref py py-class docutils literal notranslate"><span class="pre">CELFunction</span></code> functions that extend or override the built-in functions.</p></li>
<li><p>The values for identifiers.</p></li>
</ul>
<p>The <a class="reference internal" href="api.html#celpy.evaluation.Activation" title="celpy.evaluation.Activation"><code class="xref py py-class docutils literal notranslate"><span class="pre">celpy.evaluation.Activation</span></code></a> is a kind of chainmap for name resolution.
The chain has the following structure:</p>
<ul class="simple">
<li><p>The end of the chain has the built-in defaults.
(This is the bottom-most base definition.)</p></li>
<li><p>A layer on top of this can offer types and functions which are provided to integrate into the containing app or framework.</p></li>
<li><p>The next layer is the “current” activation when evaluating a given expression.
For the CLI, this has the command-line variables.
For other integrations, these are the input values.</p></li>
<li><p>A transient layer on top of this is used to create a local variable binding for the macro evaluations.
These can be nested, and introduce the macro variable as a temporary annotation and value binding.</p></li>
</ul>
</section>
<section id="cel-types">
<h3>CEL Types<a class="headerlink" href="#cel-types" title="Link to this heading">¶</a></h3>
<p>There are ten extension types that wrap Python built-in types to provide the unique CEL semantics.</p>
<ul class="simple">
<li><p><code class="xref py py-class docutils literal notranslate"><span class="pre">celtypes.TypeType</span></code> is a supertype for CEL types.</p></li>
<li><p><code class="xref py py-class docutils literal notranslate"><span class="pre">celtypes.BoolType</span></code> wraps <code class="docutils literal notranslate"><span class="pre">int</span></code> and creates additional type overload exceptions.</p></li>
<li><p><code class="xref py py-class docutils literal notranslate"><span class="pre">celtypes.BytesType</span></code> wraps <code class="docutils literal notranslate"><span class="pre">bytes</span></code> it handles conversion from <code class="xref py py-class docutils literal notranslate"><span class="pre">celtypes.StringType</span></code>.</p></li>
<li><p><code class="xref py py-class docutils literal notranslate"><span class="pre">celtypes.DoubleType</span></code> wraps <code class="docutils literal notranslate"><span class="pre">float</span></code> and creates additional type overload exceptions.</p></li>
<li><p><code class="xref py py-class docutils literal notranslate"><span class="pre">celtypes.IntType</span></code> wraps <code class="docutils literal notranslate"><span class="pre">int</span></code> and adds a 64-bit signed range constraint.</p></li>
<li><p><code class="xref py py-class docutils literal notranslate"><span class="pre">celtypes.UintType</span></code> wraps <code class="docutils literal notranslate"><span class="pre">int</span></code> and adds a 64-bit unsigned range constraint.</p></li>
<li><p><code class="xref py py-class docutils literal notranslate"><span class="pre">celtypes.ListType</span></code> wraps <code class="docutils literal notranslate"><span class="pre">list</span></code> and includes some type overload exceptions.</p></li>
<li><p><code class="xref py py-class docutils literal notranslate"><span class="pre">celtypes.MapType</span></code> wraps <code class="docutils literal notranslate"><span class="pre">dict</span></code> and includes some type overload exceptions.
Additionally, the <code class="docutils literal notranslate"><span class="pre">MapKeyTypes</span></code> type hint is the subset of types permitted as keys.</p></li>
<li><p><code class="xref py py-class docutils literal notranslate"><span class="pre">celtypes.StringType</span></code> wraps <code class="docutils literal notranslate"><span class="pre">str</span></code> and includes some type overload exceptions.</p></li>
<li><p><code class="xref py py-class docutils literal notranslate"><span class="pre">celtypes.TimestampType</span></code> wraps <code class="docutils literal notranslate"><span class="pre">datetime.datetime</span></code> and includes a number of conversions
from <code class="docutils literal notranslate"><span class="pre">datetime.datetime</span></code>, <code class="docutils literal notranslate"><span class="pre">int</span></code>, and <code class="docutils literal notranslate"><span class="pre">str</span></code> values.</p></li>
<li><p><code class="xref py py-class docutils literal notranslate"><span class="pre">celtypes.DurationType</span></code> wraps <code class="docutils literal notranslate"><span class="pre">datetime.timedelta</span></code> and includes a number of conversions
from <code class="docutils literal notranslate"><span class="pre">datetime.timedelta</span></code>, <code class="docutils literal notranslate"><span class="pre">int</span></code>, and <code class="docutils literal notranslate"><span class="pre">str</span></code> values.</p></li>
</ul>
<p>Additionally, a <code class="xref py py-class docutils literal notranslate"><span class="pre">celtypes.NullType</span></code> is defined, but does not seem to be needed. It hasn’t been deleted, yet.
It should be considered deprecated.</p>
</section>
</section>
<section id="transpiler-missing-names">
<h2>Transpiler Missing Names<a class="headerlink" href="#transpiler-missing-names" title="Link to this heading">¶</a></h2>
<p>The <code class="docutils literal notranslate"><span class="pre">member_dot</span></code> transpilation with a missing name will be found at evaluation time via <code class="docutils literal notranslate"><span class="pre">member.get('IDENT')</span></code>. This raises No Such Member in Mapping error.</p>
<p>The <code class="docutils literal notranslate"><span class="pre">primary</span> <span class="pre">::</span> <span class="pre">ident</span></code> evaluation can result in one of the following conditions:</p>
<blockquote>
<div><ul>
<li><p><code class="docutils literal notranslate"><span class="pre">ident</span></code> denotes a type definition. The value’s type is <code class="docutils literal notranslate"><span class="pre">TypeType</span></code>.
The value is a type reference <code class="docutils literal notranslate"><span class="pre">bool</span></code> becomes <code class="docutils literal notranslate"><span class="pre">celpy.celtypes.BoolType</span></code>.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">ident</span></code> denotes a built-in function. The value’s type is <code class="docutils literal notranslate"><span class="pre">CELFunction</span></code>.
The value is the Python function reference.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">ident</span></code> denotes an annotation, but the value’s type is neither <code class="docutils literal notranslate"><span class="pre">TypeType</span></code> nor <code class="docutils literal notranslate"><span class="pre">CELFunction</span></code>.</p>
<p>The transpiled value is <code class="docutils literal notranslate"><span class="pre">f"activation.{ident}"</span></code>, assuming it will be a defined variable.</p>
<p>If, at <code class="docutils literal notranslate"><span class="pre">exec()</span></code> time the name is not in the Activation with a value, a <code class="docutils literal notranslate"><span class="pre">NameError</span></code> exception will be raised that becomes a <code class="docutils literal notranslate"><span class="pre">CELEvalError</span></code> exception.</p>
</li>
</ul>
</div></blockquote>
</section>
<section id="the-member-dot-production">
<h2>The Member-Dot Production<a class="headerlink" href="#the-member-dot-production" title="Link to this heading">¶</a></h2>
<p>Consider <code class="docutils literal notranslate"><span class="pre">protobuf_message{field:</span> <span class="pre">42}.field</span></code>.
This is parsed using the following productions.</p>
<div class="highlight-bnf notranslate"><div class="highlight"><pre><span></span>member : member_dot | member_dot_arg | member_item | member_object | primary
member_dot : member "." IDENT
member_object : member "{" [fieldinits] "}"
</pre></div>
</div>
<p>The <code class="docutils literal notranslate"><span class="pre">member_object</span></code> will be a <code class="docutils literal notranslate"><span class="pre">primary</span></code> which can be an <code class="docutils literal notranslate"><span class="pre">ident</span></code>.
It MUST refer to the Annotation (not the value) because it has <code class="docutils literal notranslate"><span class="pre">fieldinits</span></code>.
All other choices are (generally) values.
They can be annotations, which means <code class="docutils literal notranslate"><span class="pre">bool.type()</span></code> works the same as <code class="docutils literal notranslate"><span class="pre">type(bool)</span></code>.</p>
<p>Here’s <code class="docutils literal notranslate"><span class="pre">primary</span></code> production, which defines the <code class="docutils literal notranslate"><span class="pre">ident</span></code> in the <code class="docutils literal notranslate"><span class="pre">member</span></code> production.</p>
<div class="highlight-bnf notranslate"><div class="highlight"><pre><span></span>primary : dot_ident_arg | dot_ident | ident_arg | ident
| paren_expr | list_lit | map_lit | literal
</pre></div>
</div>
<p>The <code class="docutils literal notranslate"><span class="pre">ident</span></code> is not <strong>always</strong> transpiled as <code class="docutils literal notranslate"><span class="pre">activation.{name}</span></code>.
Inside <code class="docutils literal notranslate"><span class="pre">member_object</span></code>, it’s <code class="docutils literal notranslate"><span class="pre">activation.resolve_name({name})</span></code>.
Outside <code class="docutils literal notranslate"><span class="pre">member_object</span></code>, it can be <code class="docutils literal notranslate"><span class="pre">activation.{name}</span></code> because it’s a simple variable.</p>
<p>It may make sense to rename the <code class="xref py py-meth docutils literal notranslate"><span class="pre">Activation.resolve_name()</span></code> method to <code class="xref py py-meth docutils literal notranslate"><span class="pre">Activation.get()</span></code>.</p>
<p>This, however, overloads the <code class="docutils literal notranslate"><span class="pre">get()</span></code> method.
This has type hint consequences.</p>
<div class="admonition important">
<p class="admonition-title">Important</p>
<p>The <code class="docutils literal notranslate"><span class="pre">member</span></code> can be any of a variety of objects:</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">NameContainer(Dict[str,</span> <span class="pre">Referent])</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">Activation</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">MapType(Dict[Value,</span> <span class="pre">Value])</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">MessageType(MapType)</span></code></p></li>
</ul>
<p>All of these classes must define a <code class="docutils literal notranslate"><span class="pre">get()</span></code> method.
The nuance is the <code class="docutils literal notranslate"><span class="pre">NameContainer</span></code> is also a Python <code class="docutils literal notranslate"><span class="pre">dict</span></code> and there’s an
overload issue between that <code class="docutils literal notranslate"><span class="pre">get()</span></code> and other <code class="docutils literal notranslate"><span class="pre">get()</span></code> definitions.</p>
</div>
<p>The Transpilation <strong>currently</strong> leverages a common method named <code class="docutils literal notranslate"><span class="pre">get()</span></code> for all of these types.
This is a Pythonic approach, but, the overload for the <code class="docutils literal notranslate"><span class="pre">NameContainer</span></code> (a <code class="docutils literal notranslate"><span class="pre">Dict</span></code> subclass) isn’t quite right:
it doesn’t return a <code class="docutils literal notranslate"><span class="pre">Referent</span></code>, but the value from a <code class="docutils literal notranslate"><span class="pre">Referent</span></code>.</p>
<p>A slightly smarter approach is to define a <code class="docutils literal notranslate"><span class="pre">get_value(member,</span> <span class="pre">'name')</span></code> function that uses a match/case structure to do the right thing for each type. The problem is, the result is a union of type, value, function, and any of these four containers!</p>
<p>Another possibility is to leverage the Annotations.
They <strong>can</strong> provide needed type information to discern which method with specific result type.</p>
</section>
</section>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="Main">
<div class="sphinxsidebarwrapper">
<h1 class="logo"><a href="index.html">CEL in Python</a></h1>
<search id="searchbox" style="display: none" role="search">
<div class="searchformwrapper">
<form class="search" action="search.html" method="get">
<input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" placeholder="Search"/>
<input type="submit" value="Go" />
</form>
</div>
</search>
<script>document.getElementById('searchbox').style.display = "block"</script><h3>Navigation</h3>
<p class="caption" role="heading"><span class="caption-text">Documentation Content:</span></p>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="installation.html">Installation</a></li>
<li class="toctree-l1"><a class="reference internal" href="cli.html">CLI Use of CEL-Python</a></li>
<li class="toctree-l1"><a class="reference internal" href="configuration.html">Configuration</a></li>
<li class="toctree-l1"><a class="reference internal" href="integration.html">Application Integration</a></li>
<li class="toctree-l1 current"><a class="current reference internal" href="#">Architecture and Design</a><ul>
<li class="toctree-l2"><a class="reference internal" href="#context">Context</a></li>
<li class="toctree-l2"><a class="reference internal" href="#container">Container</a></li>
<li class="toctree-l2"><a class="reference internal" href="#components">Components</a></li>
<li class="toctree-l2"><a class="reference internal" href="#transpiler-missing-names">Transpiler Missing Names</a></li>
<li class="toctree-l2"><a class="reference internal" href="#the-member-dot-production">The Member-Dot Production</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="api.html">API Reference</a></li>
<li class="toctree-l1"><a class="reference internal" href="development.html">Development Tools</a></li>
<li class="toctree-l1"><a class="reference internal" href="c7n_functions.html">C7N Functions Required</a></li>
</ul>
<div class="relations">
<h3>Related Topics</h3>
<ul>
<li><a href="index.html">Documentation overview</a><ul>
<li>Previous: <a href="integration.html" title="previous chapter">Application Integration</a></li>
<li>Next: <a href="api.html" title="next chapter">API Reference</a></li>
</ul></li>
</ul>
</div>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="footer">
©2020, CapitalOne.
|
Powered by <a href="https://www.sphinx-doc.org/">Sphinx 9.1.0</a>
& <a href="https://alabaster.readthedocs.io">Alabaster 1.0.0</a>
|
<a href="_sources/structure.rst.txt"
rel="nofollow">Page source</a>
</div>
</body>
</html>