• 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
/json/reader.go
1
// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
2

3
package json
4

5
import (
6
        "bytes"
7
        "encoding/json"
8
        "fmt"
9
        "io"
10

11
        "github.com/spdx/tools-golang/convert"
12
        "github.com/spdx/tools-golang/spdx"
13
        "github.com/spdx/tools-golang/spdx/common"
14
        "github.com/spdx/tools-golang/spdx/v2/v2_1"
15
        "github.com/spdx/tools-golang/spdx/v2/v2_2"
16
        "github.com/spdx/tools-golang/spdx/v2/v2_3"
17
)
18

19
// Read takes an io.Reader and returns a fully-parsed current model SPDX Document
20
// or an error if any error is encountered.
21
func Read(content io.Reader) (*spdx.Document, error) {
×
22
        doc := spdx.Document{}
×
23
        err := ReadInto(content, &doc)
×
24
        return &doc, err
×
25
}
×
26

27
// ReadInto takes an io.Reader, reads in the SPDX document at the version provided
28
// and converts to the doc version
29
func ReadInto(content io.Reader, doc common.AnyDocument) error {
×
30
        if !convert.IsPtr(doc) {
×
31
                return fmt.Errorf("doc to read into must be a pointer")
×
32
        }
×
33

34
        buf := new(bytes.Buffer)
×
35
        _, err := buf.ReadFrom(content)
×
36
        if err != nil {
×
37
                return err
×
38
        }
×
39

40
        var data interface{}
×
41
        err = json.Unmarshal(buf.Bytes(), &data)
×
42
        if err != nil {
×
43
                return err
×
44
        }
×
45

46
        val, ok := data.(map[string]interface{})
×
47
        if !ok {
×
48
                return fmt.Errorf("not a valid SPDX JSON document")
×
49
        }
×
50

51
        version, ok := val["spdxVersion"]
×
52
        if !ok {
×
53
                return fmt.Errorf("JSON document does not contain spdxVersion field")
×
54
        }
×
55

56
        switch version {
×
57
        case v2_1.Version:
×
58
                var doc v2_1.Document
×
59
                err = json.Unmarshal(buf.Bytes(), &doc)
×
60
                if err != nil {
×
61
                        return err
×
62
                }
×
63
                data = doc
×
64
        case v2_2.Version:
×
65
                var doc v2_2.Document
×
66
                err = json.Unmarshal(buf.Bytes(), &doc)
×
67
                if err != nil {
×
68
                        return err
×
69
                }
×
70
                data = doc
×
71
        case v2_3.Version:
×
72
                var doc v2_3.Document
×
73
                err = json.Unmarshal(buf.Bytes(), &doc)
×
74
                if err != nil {
×
75
                        return err
×
76
                }
×
77
                data = doc
×
78
        default:
×
79
                return fmt.Errorf("unsupported SDPX version: %s", version)
×
80
        }
81

82
        return convert.Document(data, doc)
×
83
}
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