dont export structs used for unmarshaling

Signed-off-by: Alex Goodman <alex.goodman@anchore.com>
This commit is contained in:
Alex Goodman 2020-11-16 15:15:19 -05:00
parent 4b45c42f5a
commit 62b03f3a91
No known key found for this signature in database
GPG Key ID: 5CB45AE22BAB7EA7
5 changed files with 31 additions and 20 deletions

View File

@ -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
}

View File

@ -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" {

View File

@ -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 {

View File

@ -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 {

View File

@ -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 {