Skip to content

Commit 7b15d40

Browse files
committed
refactor: reimplemented XML/JSON denormalization
Signed-off-by: Jan Kowalleck <[email protected]>
1 parent 96fe96c commit 7b15d40

File tree

1 file changed

+26
-28
lines changed

1 file changed

+26
-28
lines changed

cyclonedx/model/tool.py

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from sortedcontainers import SortedSet
2424

2525
from .._internal.compare import ComparableTuple as _ComparableTuple
26+
from ..exception.serialization import CycloneDxDeserializationException
2627
from ..schema import SchemaVersion
2728
from ..schema.schema import SchemaVersion1Dot4, SchemaVersion1Dot5, SchemaVersion1Dot6
2829
from . import ExternalReference, HashType, _HashTypeRepositorySerializationHelper
@@ -291,23 +292,16 @@ def json_normalize(cls, o: ToolsRepository, *,
291292
@classmethod
292293
def json_denormalize(cls, o: Union[List[Dict[str, Any]], Dict[str, Any]],
293294
**__: Any) -> ToolsRepository:
294-
components = []
295-
services = []
296-
tools = []
297-
295+
tools = None
296+
components = None
297+
services = None
298298
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', ()))
307303
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]
311305
return ToolsRepository(components=components, services=services, tools=tools)
312306

313307
@classmethod
@@ -348,19 +342,23 @@ def xml_denormalize(cls, o: Element, *,
348342
prop_info: 'ObjectMetadataLibrary.SerializableProperty',
349343
ctx: Type[Any],
350344
**kwargs: Any) -> ToolsRepository:
351-
tools: List[Tool] = []
352-
components: List[Component] = []
353-
services: List[Service] = []
354-
345+
tools = []
346+
components = None
347+
services = None
355348
for e in o:
356349
tag = e.tag if default_ns is None else e.tag.replace(f'{{{default_ns}}}', '')
357350
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

Comments
 (0)