package model import ( "encoding/json" "fmt" "strconv" "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"` } // sourceUnpacker is used to unmarshal Source objects type sourceUnpacker struct { Type string `json:"type"` Target json.RawMessage `json:"target"` } type ImageSource struct { source.ImageMetadata Scope source.Scope `json:"scope"` } // UnmarshalJSON populates a source object from JSON bytes. func (s *Source) UnmarshalJSON(b []byte) error { var unpacker sourceUnpacker if err := json.Unmarshal(b, &unpacker); err != nil { return err } s.Type = unpacker.Type switch s.Type { case "directory": if target, err := strconv.Unquote(string(unpacker.Target)); err == nil { s.Target = target } else { s.Target = string(unpacker.Target[:]) } case "image": var payload ImageSource if err := json.Unmarshal(unpacker.Target, &payload); err != nil { return err } s.Target = payload default: return fmt.Errorf("unsupported package metadata type: %+v", s.Type) } return nil }