|
1 | 1 | # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
2 | 2 |
|
| 3 | +import functools |
3 | 4 | from dataclasses import dataclass |
4 | 5 | from typing import Any, Optional, Union |
5 | 6 |
|
|
10 | 11 | from .._types import ModelParsingContextInterface, SpecificationRevision |
11 | 12 |
|
12 | 13 |
|
| 14 | +@functools.lru_cache(maxsize=None) |
| 15 | +def _expr_type(type_string: str) -> Any: |
| 16 | + """The ``openjd.expr.ExprType`` for ``type_string``. Imported here, on |
| 17 | + first use, because importing openjd.model must not load the native |
| 18 | + extension; only an EXPR template reaches the callers.""" |
| 19 | + from openjd.expr import ExprType |
| 20 | + |
| 21 | + return ExprType(type_string) |
| 22 | + |
| 23 | + |
13 | 24 | class _ReconstructionContext(ModelParsingContextInterface): |
14 | 25 | """Minimal parsing context used when a FormatString is reconstructed by |
15 | 26 | pickle or the copy module (see ``FormatString.__getnewargs_ex__``). |
@@ -224,6 +235,55 @@ def resolve_value(self, *, symtab: SymbolTable, path_format: Optional[object] = |
224 | 235 | ) |
225 | 236 | return self.resolve(symtab=symtab, path_format=path_format) |
226 | 237 |
|
| 238 | + def resolve_list_item( |
| 239 | + self, *, symtab: SymbolTable, target_type: str, path_format: Optional[object] = None |
| 240 | + ) -> list[Any]: |
| 241 | + """Resolve this format string as a list item (Expression Language §1.3.2). |
| 242 | +
|
| 243 | + When the format string is exactly one EXPR ``{{ ... }}`` expression, |
| 244 | + with no text around it, the expression is evaluated with |
| 245 | + ``target_type`` (an EXPR type string of the form ``T? | list[T]``, |
| 246 | + such as ``"int | list[int] | nulltype"``): a ``null`` result contributes no values, a list |
| 247 | + contributes one value per member, and any other result contributes |
| 248 | + itself. The values are the engine's ``ExprValue``s. A result that does |
| 249 | + not coerce to the target raises. |
| 250 | +
|
| 251 | + Any other format string (text around its expressions, several |
| 252 | + expressions, or a template without EXPR, whose expressions are bare |
| 253 | + value references that cannot produce ``null`` or a list) contributes |
| 254 | + its resolved string, as :meth:`resolve` returns it. |
| 255 | +
|
| 256 | + Raises: |
| 257 | + FormatStringError: if the expression cannot be evaluated or its |
| 258 | + result does not coerce to ``target_type``. |
| 259 | + """ |
| 260 | + expressions = self.expressions |
| 261 | + if len(expressions) == 1: |
| 262 | + info = expressions[0] |
| 263 | + expression = info.expression |
| 264 | + whole_field = info.start_pos == 0 and info.end_pos == len(self.original_value) |
| 265 | + if whole_field and expression is not None and getattr(expression, "is_expr", False): |
| 266 | + try: |
| 267 | + value = expression.evaluate_value( |
| 268 | + symtab=symtab, |
| 269 | + path_format=path_format, |
| 270 | + target_type=_expr_type(target_type), |
| 271 | + ) |
| 272 | + except ExpressionError as exc: |
| 273 | + raise FormatStringError( |
| 274 | + string=self.original_value, |
| 275 | + start=info.start_pos, |
| 276 | + end=info.end_pos, |
| 277 | + expr=expression.expr, |
| 278 | + details=str(exc), |
| 279 | + ) |
| 280 | + if getattr(value, "is_null", False): |
| 281 | + return [] |
| 282 | + if str(value.type).startswith("list["): |
| 283 | + return list(value) |
| 284 | + return [value] |
| 285 | + return [self.resolve(symtab=symtab, path_format=path_format)] |
| 286 | + |
227 | 287 | def _preprocess( |
228 | 288 | self, *, context: ModelParsingContextInterface |
229 | 289 | ) -> list[Union[str, ExpressionInfo]]: |
|
0 commit comments