Skip to content

Add support to serialize/deserialize SARIF report #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 54 additions & 7 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,65 @@ How to use

.. code-block:: python

import cattrs
import json
import sys
from sarif_om import SarifLog, Run, Tool, ToolComponent, Result, Message


sarif_log = SarifLog(
version="2.1.0",
runs=[
Run(
tool=Tool(
driver=ToolComponent(
name="My Static Analyzer",
version="1.0.0",
)
),
results=[
Result(
rule_id="R001",
message=Message(text="Use of uninitialized variable 'x'"),
)
],
)
],
)

print(sarif_log)

To serialize it to plain dict or JSON:

from sarif_om import SarifLog
.. code-block:: python

from sarif_om import to_dict, to_json

print(to_json(sarif_log))
print(to_dict(sarif_log))

with open(sys.argv[1]) as fp:
data = json.load(fp)
To deserialize dict/JSON data to ``sarif_om.SarifLog``:

sarif_log = cattrs.structure(data, SarifLog)
.. code-block:: python

import json
from sarif_om import from_dict, from_json


sarif_log_dict = {
"runs": [
{
"tool": {"driver": {"name": "My Static Analyzer", "version": "1.0.0"}},
"results": [
{
"message": {"text": "Use of uninitialized variable 'x'"},
"ruleId": "R001",
}
],
}
],
"version": "2.1.0",
}

print(from_dict(sarif_log_dict))
print(from_json(json.dumps(sarif_log_dict)))

Generation
==========
Expand Down
59 changes: 59 additions & 0 deletions sarif_om/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# This file was generated by jschema_to_python version 1.2.3.

import json
from typing import Dict
from attrs import has, fields
from cattrs import Converter
from cattrs.gen import make_dict_unstructure_fn, make_dict_structure_fn, override

from ._sarif_log import SarifLog
from ._address import Address
from ._artifact import Artifact
Expand Down Expand Up @@ -53,3 +59,56 @@
from ._version_control_details import VersionControlDetails
from ._web_request import WebRequest
from ._web_response import WebResponse


def _generate_sarif_report_cattrs_converter() -> Converter:
converter = Converter()

def _unstructure(cls):
return make_dict_unstructure_fn(
cls,
converter,
**{
a.name: override(
rename=a.metadata["schema_property_name"],
omit_if_default=True,
)
for a in fields(cls)
},
)

def _structure(cls):
return make_dict_structure_fn(
cls,
converter,
**{
a.name: override(
rename=a.metadata["schema_property_name"],
)
for a in fields(cls)
},
)

converter.register_unstructure_hook_factory(has, _unstructure)
converter.register_structure_hook_factory(has, _structure)
return converter


_converter = _generate_sarif_report_cattrs_converter()


def to_dict(sarif_log: SarifLog) -> Dict:
return _converter.unstructure(sarif_log, SarifLog)


def to_json(sarif_log: SarifLog, indent=2) -> str:
obj = to_dict(sarif_log)
return json.dumps(obj, indent=indent)


def from_dict(data: Dict) -> SarifLog:
return _converter.structure(data, SarifLog)


def from_json(data: str) -> SarifLog:
return from_dict(json.loads(data))
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ classifier =
python_requires = >= 2.7
install_requires =
attrs
cattrs
pbr

[files]
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

from setuptools import setup

setup(setup_requires=["pbr"], pbr=True)
setup(setup_requires=["pbr"], install_requires=["attrs", "cattrs"], pbr=True)