Skip to content

Commit

Permalink
Merge pull request #4 from willyw0nka/to-json
Browse files Browse the repository at this point in the history
feat: add to_json decorator to all dataclasses
  • Loading branch information
willyw0nka authored Apr 10, 2024
2 parents 683f68f + 56d708c commit f48bebf
Show file tree
Hide file tree
Showing 21 changed files with 52 additions and 3 deletions.
11 changes: 11 additions & 0 deletions pygrype/core/decorators/to_json.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import json
from dataclasses import asdict
from typing import Type, TypeVar

T = TypeVar('T')

def to_json(cls: Type[T]) -> Type[T]:
def to_json_func(self: T):
return json.dumps(asdict(self))
cls.to_json = to_json_func
return cls
2 changes: 2 additions & 0 deletions pygrype/core/grype_version.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from dataclasses import dataclass

from pygrype.core.decorators.to_json import to_json

@dataclass
@to_json
class GrypeVersion:
version: str
syft_version: str
Expand Down
2 changes: 2 additions & 0 deletions pygrype/core/list/db_meta_data.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from dataclasses import dataclass

from pygrype.core.decorators.to_json import to_json

@dataclass
@to_json
class DBMetaData:
built: str
version: int
Expand Down
2 changes: 2 additions & 0 deletions pygrype/core/scan/artifact.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from dataclasses import dataclass
from typing import List

from pygrype.core.decorators.to_json import to_json
from pygrype.core.scan.location import Location
from pygrype.core.scan.upstream import Upstream


@dataclass
@to_json
class Artifact:
id: str
name: str
Expand Down
2 changes: 2 additions & 0 deletions pygrype/core/scan/cvss.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from dataclasses import dataclass
from typing import Optional

from pygrype.core.decorators.to_json import to_json
from pygrype.core.scan.cvss_metrics import CVSSMetrics


@dataclass
@to_json
class CVSS:
source: Optional[str]
type: Optional[str]
Expand Down
2 changes: 2 additions & 0 deletions pygrype/core/scan/cvss_metrics.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from dataclasses import dataclass

from pygrype.core.decorators.to_json import to_json

@dataclass
@to_json
class CVSSMetrics:
baseScore: float
exploitabilityScore: float
Expand Down
2 changes: 2 additions & 0 deletions pygrype/core/scan/distro.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from dataclasses import dataclass
from typing import List, Optional

from pygrype.core.decorators.to_json import to_json

@dataclass
@to_json
class Distro:
name: str
version: str
Expand Down
2 changes: 2 additions & 0 deletions pygrype/core/scan/layer.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from dataclasses import dataclass

from pygrype.core.decorators.to_json import to_json

@dataclass
@to_json
class Layer:
mediaType: str
digest: str
Expand Down
3 changes: 3 additions & 0 deletions pygrype/core/scan/location.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
from dataclasses import dataclass
from typing import Optional

from pygrype.core.decorators.to_json import to_json

@dataclass
@to_json
class Location:
path: str
layerID: Optional[str] = None
2 changes: 2 additions & 0 deletions pygrype/core/scan/match.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
from dataclasses import dataclass
from typing import List

from pygrype.core.decorators.to_json import to_json
from pygrype.core.scan.artifact import Artifact
from pygrype.core.scan.match_details import MatchDetails
from pygrype.core.scan.vulnerability import Vulnerability


@dataclass
@to_json
class Match:
vulnerability: Vulnerability
relatedVulnerabilities: List[Vulnerability]
Expand Down
2 changes: 2 additions & 0 deletions pygrype/core/scan/match_details.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from dataclasses import dataclass

from pygrype.core.decorators.to_json import to_json
from pygrype.core.scan.match_details_found import MatchDetailsFound
from pygrype.core.scan.match_details_searched_by import MatchDetailsSearchedBy


@dataclass
@to_json
class MatchDetails:
type: str
matcher: str
Expand Down
2 changes: 2 additions & 0 deletions pygrype/core/scan/match_details_found.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from dataclasses import dataclass
from typing import List, Optional

from pygrype.core.decorators.to_json import to_json

@dataclass
@to_json
class MatchDetailsFound:
vulnerabilityID: str
versionConstraint: str
Expand Down
3 changes: 2 additions & 1 deletion pygrype/core/scan/match_details_searched_by.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from dataclasses import dataclass
from typing import List, Optional

from pygrype.core.decorators.to_json import to_json
from pygrype.core.scan.package import Package
from pygrype.core.scan.searched_by_distro import Distro


@dataclass
@to_json
class MatchDetailsSearchedBy:
namespace: str
cpes: Optional[List[str]]
Expand Down
2 changes: 2 additions & 0 deletions pygrype/core/scan/package.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from dataclasses import dataclass

from pygrype.core.decorators.to_json import to_json

@dataclass
@to_json
class Package:
name: str
version: str
3 changes: 2 additions & 1 deletion pygrype/core/scan/scan.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from dataclasses import dataclass
from typing import List

from pygrype.core.decorators.to_json import to_json
from pygrype.core.scan.distro import Distro
from pygrype.core.scan.match import Match
from pygrype.core.scan.scan_source import ScanSource


@dataclass
@to_json
class Scan:
matches: List[Match]
source: ScanSource
Expand Down
2 changes: 2 additions & 0 deletions pygrype/core/scan/scan_source.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from dataclasses import dataclass
from typing import Union

from pygrype.core.decorators.to_json import to_json
from pygrype.core.scan.target import Target


@dataclass
@to_json
class ScanSource:
type: str
target: Union[Target, str]
2 changes: 2 additions & 0 deletions pygrype/core/scan/searched_by_distro.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from dataclasses import dataclass

from pygrype.core.decorators.to_json import to_json

@dataclass
@to_json
class Distro:
type: str
version: str
3 changes: 2 additions & 1 deletion pygrype/core/scan/target.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from dataclasses import dataclass
from typing import List

from pygrype.core.decorators.to_json import to_json
from pygrype.core.scan.layer import Layer


@dataclass
@to_json
class Target:
userInput: str
imageID: str
Expand Down
2 changes: 2 additions & 0 deletions pygrype/core/scan/upstream.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from dataclasses import dataclass

from pygrype.core.decorators.to_json import to_json

@dataclass
@to_json
class Upstream:
name: str
2 changes: 2 additions & 0 deletions pygrype/core/scan/vulnerability.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from dataclasses import dataclass
from typing import List, Optional

from pygrype.core.decorators.to_json import to_json
from pygrype.core.scan.cvss import CVSS
from pygrype.core.scan.vulnerability_fix import VulnerabilityFix


@dataclass
@to_json
class Vulnerability:
id: str
description: Optional[str]
Expand Down
2 changes: 2 additions & 0 deletions pygrype/core/scan/vulnerability_fix.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from dataclasses import dataclass
from typing import List

from pygrype.core.decorators.to_json import to_json

@dataclass
@to_json
class VulnerabilityFix:
versions: List[str]
state: str

0 comments on commit f48bebf

Please sign in to comment.