1// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later 2 3package common 4 5import ( 6 "encoding/json" 7 "fmt" 8 "strings" 9) 10 11type Supplier struct { 12 // can be "NOASSERTION" 13 Supplier string 14 // SupplierType can be one of "Person", "Organization", or empty if Supplier is "NOASSERTION" 15 SupplierType string 16} 17 18// UnmarshalJSON takes a supplier in the typical one-line format and parses it into a Supplier struct. 19// This function is also used when unmarshalling YAML 20func (s *Supplier) UnmarshalJSON(data []byte) error { 21 // the value is just a string presented as a slice of bytes 22 supplierStr := string(data) 23 supplierStr = strings.Trim(supplierStr, "\"") 24 25 if supplierStr == "NOASSERTION" { 26 s.Supplier = supplierStr 27 return nil 28 } 29 30 supplierFields := strings.SplitN(supplierStr, ": ", 2) 31 32 if len(supplierFields) != 2 { 33 return fmt.Errorf("failed to parse Supplier '%s'", supplierStr) 34 } 35 36 s.SupplierType = supplierFields[0] 37 s.Supplier = supplierFields[1] 38 39 return nil 40} 41 42// MarshalJSON converts the receiver into a slice of bytes representing a Supplier in string form. 43// This function is also used when marshalling to YAML 44func (s Supplier) MarshalJSON() ([]byte, error) { 45 if s.Supplier == "NOASSERTION" { 46 return json.Marshal(s.Supplier) 47 } else if s.SupplierType != "" && s.Supplier != "" { 48 return json.Marshal(fmt.Sprintf("%s: %s", s.SupplierType, s.Supplier)) 49 } 50 51 return []byte{}, fmt.Errorf("failed to marshal invalid Supplier: %+v", s) 52} 53 54type Originator struct { 55 // can be "NOASSERTION" 56 Originator string 57 // OriginatorType can be one of "Person", "Organization", or empty if Originator is "NOASSERTION" 58 OriginatorType string 59} 60 61// UnmarshalJSON takes an originator in the typical one-line format and parses it into an Originator struct. 62// This function is also used when unmarshalling YAML 63func (o *Originator) UnmarshalJSON(data []byte) error { 64 // the value is just a string presented as a slice of bytes 65 originatorStr := string(data) 66 originatorStr = strings.Trim(originatorStr, "\"") 67 68 if originatorStr == "NOASSERTION" { 69 o.Originator = originatorStr 70 return nil 71 } 72 73 originatorFields := strings.SplitN(originatorStr, ": ", 2) 74 75 if len(originatorFields) != 2 { 76 return fmt.Errorf("failed to parse Originator '%s'", originatorStr) 77 } 78 79 o.OriginatorType = originatorFields[0] 80 o.Originator = originatorFields[1] 81 82 return nil 83} 84 85// MarshalJSON converts the receiver into a slice of bytes representing an Originator in string form. 86// This function is also used when marshalling to YAML 87func (o Originator) MarshalJSON() ([]byte, error) { 88 if o.Originator == "NOASSERTION" { 89 return json.Marshal(o.Originator) 90 } else if o.Originator != "" { 91 return json.Marshal(fmt.Sprintf("%s: %s", o.OriginatorType, o.Originator)) 92 } 93 94 return []byte{}, nil 95} 96 97type PackageVerificationCode struct { 98 // Cardinality: mandatory, one if filesAnalyzed is true / omitted; 99 // zero (must be omitted) if filesAnalyzed is false 100 Value string `json:"packageVerificationCodeValue"` 101 // Spec also allows specifying files to exclude from the 102 // verification code algorithm; intended to enable exclusion of 103 // the SPDX document file itself. 104 ExcludedFiles []string `json:"packageVerificationCodeExcludedFiles,omitempty"` 105} 106