|
23 | 23 | from sortedcontainers import SortedSet
|
24 | 24 |
|
25 | 25 | from .._internal.compare import ComparableTuple as _ComparableTuple
|
| 26 | +from ..exception.serialization import CycloneDxDeserializationException |
26 | 27 | from ..schema import SchemaVersion
|
27 | 28 | from ..schema.schema import SchemaVersion1Dot4, SchemaVersion1Dot5, SchemaVersion1Dot6
|
28 | 29 | from . import ExternalReference, HashType, _HashTypeRepositorySerializationHelper
|
@@ -291,23 +292,16 @@ def json_normalize(cls, o: ToolsRepository, *,
|
291 | 292 | @classmethod
|
292 | 293 | def json_denormalize(cls, o: Union[List[Dict[str, Any]], Dict[str, Any]],
|
293 | 294 | **__: Any) -> ToolsRepository:
|
294 |
| - components = [] |
295 |
| - services = [] |
296 |
| - tools = [] |
297 |
| - |
| 295 | + tools = None |
| 296 | + components = None |
| 297 | + services = None |
298 | 298 | if isinstance(o, Dict):
|
299 |
| - if 'components' in o: |
300 |
| - for c in o['components']: |
301 |
| - components.append(Component.from_json(c)) # type: ignore[attr-defined] |
302 |
| - |
303 |
| - if 'services' in o: |
304 |
| - for s in o['services']: |
305 |
| - services.append(Service.from_json(s)) # type: ignore[attr-defined] |
306 |
| - |
| 299 | + components = map(lambda c: Component.from_json( # type:ignore[attr-defined] |
| 300 | + c), o.get('components', ())) |
| 301 | + services = map(lambda s: Service.from_json( # type:ignore[attr-defined] |
| 302 | + s), o.get('services', ())) |
307 | 303 | elif isinstance(o, Iterable):
|
308 |
| - for t in o: |
309 |
| - tools.append(Tool.from_json(t)) # type: ignore[attr-defined] |
310 |
| - |
| 304 | + tools = map(lambda t: Tool.from_json(t), o) # type:ignore[attr-defined] |
311 | 305 | return ToolsRepository(components=components, services=services, tools=tools)
|
312 | 306 |
|
313 | 307 | @classmethod
|
@@ -348,19 +342,23 @@ def xml_denormalize(cls, o: Element, *,
|
348 | 342 | prop_info: 'ObjectMetadataLibrary.SerializableProperty',
|
349 | 343 | ctx: Type[Any],
|
350 | 344 | **kwargs: Any) -> ToolsRepository:
|
351 |
| - tools: List[Tool] = [] |
352 |
| - components: List[Component] = [] |
353 |
| - services: List[Service] = [] |
354 |
| - |
| 345 | + tools = [] |
| 346 | + components = None |
| 347 | + services = None |
355 | 348 | for e in o:
|
356 | 349 | tag = e.tag if default_ns is None else e.tag.replace(f'{{{default_ns}}}', '')
|
357 | 350 | if tag == 'tool':
|
358 |
| - tools.append(Tool.from_xml(e)) # type: ignore[attr-defined] |
359 |
| - if tag == 'components': |
360 |
| - for c in e: |
361 |
| - components.append(Component.from_xml(c)) # type: ignore[attr-defined] |
362 |
| - if tag == 'services': |
363 |
| - for s in e: |
364 |
| - services.append(Service.from_xml(s)) # type: ignore[attr-defined] |
365 |
| - |
366 |
| - return ToolsRepository(tools=tools, components=components, services=services) |
| 351 | + tools.append(Tool.from_xml( # type:ignore[attr-defined] |
| 352 | + e, default_ns)) |
| 353 | + elif tag == 'components': |
| 354 | + components = map(lambda s: Component.from_xml( # type:ignore[attr-defined] |
| 355 | + s, default_ns), e) |
| 356 | + elif tag == 'services': |
| 357 | + services = map(lambda s: Service.from_xml( # type:ignore[attr-defined] |
| 358 | + s, default_ns), e) |
| 359 | + else: |
| 360 | + raise CycloneDxDeserializationException(f'unexpected: {e!r}') |
| 361 | + return ToolsRepository( |
| 362 | + tools=tools, |
| 363 | + components=components, |
| 364 | + services=services) |
0 commit comments