• 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

64.1
/rdf/reader.go
1
// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
2

3
package rdf
4

5
import (
6
        "errors"
7
        "fmt"
8
        "io"
9

10
        "github.com/spdx/gordf/rdfloader"
11
        gordfParser "github.com/spdx/gordf/rdfloader/parser"
12
        "github.com/spdx/tools-golang/convert"
13
        "github.com/spdx/tools-golang/spdx"
14
        "github.com/spdx/tools-golang/spdx/common"
15
        "github.com/spdx/tools-golang/spdx/v2/v2_2"
16
        v2_2_reader "github.com/spdx/tools-golang/spdx/v2/v2_2/rdf/reader"
17
        "github.com/spdx/tools-golang/spdx/v2/v2_3"
18
        v2_3_reader "github.com/spdx/tools-golang/spdx/v2/v2_3/rdf/reader"
19
)
20

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

29
// ReadInto takes an io.Reader, reads in the SPDX document at the version provided
30
// and converts to the doc version
31
func ReadInto(content io.Reader, doc common.AnyDocument) error {
1✔
32
        if !convert.IsPtr(doc) {
1✔
33
                return fmt.Errorf("doc to read into must be a pointer")
×
34
        }
×
35
        var rdfParserObj, err = rdfloader.LoadFromReaderObject(content)
1✔
36
        if err != nil {
1✔
37
                return err
×
38
        }
×
39

40
        version, err := getSpdxVersion(rdfParserObj)
1✔
41
        if err != nil {
1✔
42
                return err
×
43
        }
×
44

45
        var data interface{}
1✔
46
        switch version {
1✔
47
        case v2_2.Version:
1✔
48
                data, err = v2_2_reader.LoadFromGoRDFParser(rdfParserObj)
1✔
49
        case v2_3.Version:
×
50
                data, err = v2_3_reader.LoadFromGoRDFParser(rdfParserObj)
×
51
        default:
×
52
                return fmt.Errorf("unsupported SPDX version: '%v'", version)
×
53
        }
54

55
        if err != nil {
1✔
56
                return err
×
57
        }
×
58

59
        return convert.Document(data.(common.AnyDocument), doc)
1✔
60
}
61

62
func getSpdxVersion(parser *gordfParser.Parser) (string, error) {
1✔
63
        version := ""
1✔
64
        for _, node := range parser.Triples {
41✔
65
                if node.Predicate.ID == "http://spdx.org/rdf/terms#specVersion" {
41✔
66
                        version = node.Object.ID
1✔
67
                        break
1✔
68
                }
69
        }
70
        if version == "" {
1✔
71
                return "", errors.New("unable to determine version from RDF document")
×
72
        }
×
73
        return version, nil
1✔
74
}
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