From 62b03f3a917ca7f3c681cc8904b98d1e80041ed2 Mon Sep 17 00:00:00 2001 From: Alex Goodman Date: Mon, 16 Nov 2020 15:15:19 -0500 Subject: [PATCH] dont export structs used for unmarshaling Signed-off-by: Alex Goodman --- syft/presenter/json/artifact.go | 20 ++++++++++---------- syft/presenter/json/distribution.go | 9 +++++---- syft/presenter/json/document.go | 10 ++++++---- syft/presenter/json/presenter.go | 3 +++ syft/presenter/json/source.go | 9 +++++++-- 5 files changed, 31 insertions(+), 20 deletions(-) diff --git a/syft/presenter/json/artifact.go b/syft/presenter/json/artifact.go index 3aeea21ef..e26bdc654 100644 --- a/syft/presenter/json/artifact.go +++ b/syft/presenter/json/artifact.go @@ -9,11 +9,11 @@ import ( ) type Artifact struct { - ArtifactBasicMetadata - ArtifactCustomMetadata + artifactBasicMetadata + artifactCustomMetadata } -type ArtifactBasicMetadata struct { +type artifactBasicMetadata struct { Name string `json:"name"` Version string `json:"version"` Type pkg.Type `json:"type"` @@ -23,12 +23,12 @@ type ArtifactBasicMetadata struct { Language pkg.Language `json:"language"` } -type ArtifactCustomMetadata struct { +type artifactCustomMetadata struct { MetadataType pkg.MetadataType `json:"metadataType"` Metadata interface{} `json:"metadata,omitempty"` } -type ArtifactMetadataUnpacker struct { +type artifactMetadataUnpacker struct { MetadataType string `json:"metadataType"` Metadata json.RawMessage `json:"metadata"` } @@ -36,7 +36,7 @@ type ArtifactMetadataUnpacker struct { func NewArtifact(p *pkg.Package) (Artifact, error) { return Artifact{ - ArtifactBasicMetadata: ArtifactBasicMetadata{ + artifactBasicMetadata: artifactBasicMetadata{ Name: p.Name, Version: p.Version, Type: p.Type, @@ -45,7 +45,7 @@ func NewArtifact(p *pkg.Package) (Artifact, error) { Licenses: p.Licenses, Language: p.Language, }, - ArtifactCustomMetadata: ArtifactCustomMetadata{ + artifactCustomMetadata: artifactCustomMetadata{ MetadataType: p.MetadataType, Metadata: p.Metadata, }, @@ -68,13 +68,13 @@ func (a Artifact) ToPackage() pkg.Package { } func (a *Artifact) UnmarshalJSON(b []byte) error { - var basic ArtifactBasicMetadata + var basic artifactBasicMetadata if err := json.Unmarshal(b, &basic); err != nil { return err } - a.ArtifactBasicMetadata = basic + a.artifactBasicMetadata = basic - var unpacker ArtifactMetadataUnpacker + var unpacker artifactMetadataUnpacker if err := json.Unmarshal(b, &unpacker); err != nil { return err } diff --git a/syft/presenter/json/distribution.go b/syft/presenter/json/distribution.go index b19c4ca68..150334a4c 100644 --- a/syft/presenter/json/distribution.go +++ b/syft/presenter/json/distribution.go @@ -2,13 +2,14 @@ package json import "github.com/anchore/syft/syft/distro" -// Distribution provides information about a detected Linux Distribution +// Distribution provides information about a detected Linux Distribution. type Distribution struct { - Name string `json:"name"` - Version string `json:"version"` - IDLike string `json:"idLike"` + Name string `json:"name"` // Name of the Linux distribution + Version string `json:"version"` // Version of the Linux distribution (major or major.minor version) + IDLike string `json:"idLike"` // the ID_LIKE field found within the /etc/os-release file } +// NewDistribution creates a struct with the Linux distribution to be represented in JSON. func NewDistribution(d distro.Distro) Distribution { distroName := d.Name() if distroName == "UnknownDistroType" { diff --git a/syft/presenter/json/document.go b/syft/presenter/json/document.go index 19b89c9d2..de1ce463b 100644 --- a/syft/presenter/json/document.go +++ b/syft/presenter/json/document.go @@ -8,13 +8,15 @@ import ( "github.com/anchore/syft/syft/source" ) +// Document represents the syft cataloging findings as a JSON document type Document struct { - Artifacts []Artifact `json:"artifacts"` - Source Source `json:"source"` - Distro Distribution `json:"distro"` - Descriptor Descriptor `json:"descriptor"` + Artifacts []Artifact `json:"artifacts"` // Artifacts is the list of packages discovered and placed into the catalog + Source Source `json:"source"` // Source represents the original object that was cataloged + Distro Distribution `json:"distro"` // Distro represents the Linux distribution that was detected from the source + Descriptor Descriptor `json:"descriptor"` // Descriptor is a block containing self-describing information about syft } +// NewDocument creates and populates a new JSON document struct from the given cataloging results. func NewDocument(catalog *pkg.Catalog, srcMetadata source.Metadata, d distro.Distro) (Document, error) { src, err := NewSource(srcMetadata) if err != nil { diff --git a/syft/presenter/json/presenter.go b/syft/presenter/json/presenter.go index 00af4ef4e..dd11381a0 100644 --- a/syft/presenter/json/presenter.go +++ b/syft/presenter/json/presenter.go @@ -9,12 +9,14 @@ import ( "github.com/anchore/syft/syft/source" ) +// Presenter is a JSON presentation object for the syft results type Presenter struct { catalog *pkg.Catalog srcMetadata source.Metadata distro distro.Distro } +// NewPresenter creates a new JSON presenter object for the given cataloging results. func NewPresenter(catalog *pkg.Catalog, s source.Metadata, d distro.Distro) *Presenter { return &Presenter{ catalog: catalog, @@ -23,6 +25,7 @@ func NewPresenter(catalog *pkg.Catalog, s source.Metadata, d distro.Distro) *Pre } } +// Present the catalog results to the given writer. func (pres *Presenter) Present(output io.Writer) error { doc, err := NewDocument(pres.catalog, pres.srcMetadata, pres.distro) if err != nil { diff --git a/syft/presenter/json/source.go b/syft/presenter/json/source.go index a8a1015a4..cd777b93e 100644 --- a/syft/presenter/json/source.go +++ b/syft/presenter/json/source.go @@ -7,16 +7,19 @@ import ( "github.com/anchore/syft/syft/source" ) +// Source object represents the thing that was cataloged type Source struct { Type string `json:"type"` Target interface{} `json:"target"` } -type SourceUnpacker struct { +// sourceUnpacker is used to unmarshal Source objects +type sourceUnpacker struct { Type string `json:"type"` Target json.RawMessage `json:"target"` } +// NewSource creates a new source object to be represented into JSON. func NewSource(src source.Metadata) (Source, error) { switch src.Scheme { case source.ImageScheme: @@ -34,8 +37,9 @@ func NewSource(src source.Metadata) (Source, error) { } } +// UnmarshalJSON populates a source object from JSON bytes. func (s *Source) UnmarshalJSON(b []byte) error { - var unpacker SourceUnpacker + var unpacker sourceUnpacker if err := json.Unmarshal(b, &unpacker); err != nil { return err } @@ -57,6 +61,7 @@ func (s *Source) UnmarshalJSON(b []byte) error { return nil } +// ToSourceMetadata takes a source object represented from JSON and creates a source.Metadata object. func (s *Source) ToSourceMetadata() source.Metadata { var metadata source.Metadata switch s.Type {