• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

spdx / tools-golang / 5008105195

17 May 2023 09:34PM UTC coverage: 94.782% (-0.04%) from 94.822%
5008105195

Pull #215

github

Brandon Lum
change packageVerification code for 2.1,2.2 to omitempty
Pull Request #215: Fixing some optional params: copyrightText, licenseListVersion, packageVerificationCode

7157 of 7551 relevant lines covered (94.78%)

15.09 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/spdx/v2/common/package.go
1
// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
2

3
package common
4

5
import (
6
        "encoding/json"
7
        "fmt"
8
        "strings"
9
)
10

11
type 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
20
func (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
44
func (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

54
type 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
63
func (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
87
func (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

97
type 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
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc