package model import ( "encoding/json" "fmt" "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"` } // 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": s.Target = string(unpacker.Target[:]) case "image": var payload source.ImageMetadata 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 }