experiment with encoder/decoder for data encapsulation

Signed-off-by: Alex Goodman <alex.goodman@anchore.com>
This commit is contained in:
Alex Goodman
2021-11-22 16:11:52 -05:00
parent f699a6add8
commit 5fb0235cfb
10 changed files with 67 additions and 39 deletions
+1 -1
View File
@@ -15,7 +15,7 @@ import (
// TODO: eventually this is the source of truth for all formatters
func All() []format.Format {
return []format.Format{
syftjson.Format(),
syftjson.Format( /*...now there is a requirement for app config here...*/ ),
table.Format(),
cyclonedx12xml.Format(),
spdx22json.Format(),
+4 -1
View File
@@ -10,7 +10,10 @@ import (
"github.com/anchore/syft/internal/formats/syftjson/model"
)
func decoder(reader io.Reader) (*sbom.SBOM, error) {
type decoder struct {
}
func (d decoder) Decode(reader io.Reader) (*sbom.SBOM, error) {
dec := json.NewDecoder(reader)
var doc model.Document
+6 -2
View File
@@ -7,8 +7,12 @@ import (
"github.com/anchore/syft/syft/sbom"
)
func encoder(output io.Writer, s sbom.SBOM, appConfig interface{}) error {
doc := ToFormatModel(s, appConfig)
type encoder struct {
appConfig interface{}
}
func (e encoder) Encode(output io.Writer, s sbom.SBOM) error {
doc := ToFormatModel(s, e.appConfig)
enc := json.NewEncoder(output)
// prevent > and < from being escaped in the payload
+18 -4
View File
@@ -2,11 +2,25 @@ package syftjson
import "github.com/anchore/syft/syft/format"
func Format() format.Format {
func Format(appConfig interface{}) format.Format {
return format.NewFormat(
format.JSONOption,
encoder,
decoder,
validator,
Encoder(appConfig),
Decoder(),
Validator(),
)
}
func Encoder(appConfig interface{}) format.Encoder {
return &encoder{
appConfig: appConfig,
}
}
func Decoder() format.Decoder {
return &decoder{}
}
func Validator() format.Validator {
return &validator{}
}
+4 -1
View File
@@ -9,7 +9,10 @@ import (
"github.com/anchore/syft/internal/formats/syftjson/model"
)
func validator(reader io.Reader) error {
type validator struct {
}
func (v validator) Validate(reader io.Reader) error {
type Document struct {
Schema model.Schema `json:"schema"`
}