Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Stringer interface on structs, add FromString method where relevant #137

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion builder/builder2v1/build_package.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func BuildPackageSection2_1(packageName string, dirRoot string, pathsIgnore []st
fileNumber++
}
// get the verification code
code, err := utils.GetVerificationCode2_1(files, "")
code, err := spdx.MakePackageVerificationCode2_1(files, "")
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion builder/builder2v2/build_package.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func BuildPackageSection2_2(packageName string, dirRoot string, pathsIgnore []st
}

// get the verification code
code, err := utils.GetVerificationCode2_2(files, "")
code, err := spdx.MakePackageVerificationCode2_2(files, "")
if err != nil {
return nil, err
}
Expand Down
32 changes: 16 additions & 16 deletions json/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func TestWrite2_2(t *testing.T) {
var want = spdx.Document2_2{
DataLicense: "CC0-1.0",
SPDXVersion: "SPDX-2.2",
SPDXIdentifier: "SPDXRef-DOCUMENT",
SPDXIdentifier: "DOCUMENT",
DocumentName: "SPDX-Tools-v2.0",
DocumentNamespace: "http://spdx.org/spdxdocs/spdx-example-444504E0-4F89-41D3-9A0C-0305E82C3301",
CreationInfo: &spdx.CreationInfo2_2{
Expand All @@ -77,7 +77,7 @@ var want = spdx.Document2_2{
DocumentComment: "This document was created using SPDX 2.0 using licenses from the web site.",
ExternalDocumentReferences: []spdx.ExternalDocumentRef2_2{
{
DocumentRefID: "DocumentRef-spdx-tool-1.2",
DocumentRefID: spdx.MakeDocElementID("spdx-tool-1.2", ""),
URI: "http://spdx.org/spdxdocs/spdx-tools-v1.2-3F2504E0-4F89-41D3-9A0C-0305E82C3301",
Checksum: spdx.Checksum{
Algorithm: spdx.SHA1,
Expand Down Expand Up @@ -148,7 +148,7 @@ var want = spdx.Document2_2{
Packages: []*spdx.Package2_2{
{
PackageName: "glibc",
PackageSPDXIdentifier: "SPDXRef-Package",
PackageSPDXIdentifier: "Package",
PackageVersion: "2.11.1",
PackageFileName: "glibc-2.11.1.tar.gz",
PackageSupplier: &spdx.Supplier{
Expand Down Expand Up @@ -223,7 +223,7 @@ var want = spdx.Document2_2{
},
},
{
PackageSPDXIdentifier: "SPDXRef-fromDoap-1",
PackageSPDXIdentifier: "fromDoap-1",
PackageCopyrightText: "NOASSERTION",
PackageDownloadLocation: "NOASSERTION",
FilesAnalyzed: false,
Expand All @@ -234,7 +234,7 @@ var want = spdx.Document2_2{
},
{
PackageName: "Jena",
PackageSPDXIdentifier: "SPDXRef-fromDoap-0",
PackageSPDXIdentifier: "fromDoap-0",
PackageCopyrightText: "NOASSERTION",
PackageDownloadLocation: "https://search.maven.org/remotecontent?filepath=org/apache/jena/apache-jena/3.12.0/apache-jena-3.12.0.tar.gz",
PackageExternalReferences: []*spdx.PackageExternalReference2_2{
Expand All @@ -251,7 +251,7 @@ var want = spdx.Document2_2{
PackageVersion: "3.12.0",
},
{
PackageSPDXIdentifier: "SPDXRef-Saxon",
PackageSPDXIdentifier: "Saxon",
PackageChecksums: []spdx.Checksum{
{
Algorithm: "SHA1",
Expand All @@ -274,7 +274,7 @@ var want = spdx.Document2_2{
Files: []*spdx.File2_2{
{
FileName: "./src/org/spdx/parser/DOAPProject.java",
FileSPDXIdentifier: "SPDXRef-DoapSource",
FileSPDXIdentifier: "DoapSource",
FileTypes: []string{
"SOURCE",
},
Expand All @@ -298,7 +298,7 @@ var want = spdx.Document2_2{
},
},
{
FileSPDXIdentifier: "SPDXRef-CommonsLangSrc",
FileSPDXIdentifier: "CommonsLangSrc",
Checksums: []spdx.Checksum{
{
Algorithm: "SHA1",
Expand All @@ -315,7 +315,7 @@ var want = spdx.Document2_2{
FileNotice: "Apache Commons Lang\nCopyright 2001-2011 The Apache Software Foundation\n\nThis product includes software developed by\nThe Apache Software Foundation (http://www.apache.org/).\n\nThis product includes software from the Spring Framework,\nunder the Apache License 2.0 (see: StringUtils.containsWhitespace())",
},
{
FileSPDXIdentifier: "SPDXRef-JenaLib",
FileSPDXIdentifier: "JenaLib",
Checksums: []spdx.Checksum{
{
Algorithm: "SHA1",
Expand All @@ -332,7 +332,7 @@ var want = spdx.Document2_2{
LicenseInfoInFiles: []string{"LicenseRef-1"},
},
{
FileSPDXIdentifier: "SPDXRef-File",
FileSPDXIdentifier: "File",
Annotations: []spdx.Annotation2_2{
{
Annotator: spdx.Annotator{
Expand Down Expand Up @@ -367,27 +367,27 @@ var want = spdx.Document2_2{
},
Snippets: []spdx.Snippet2_2{
{
SnippetSPDXIdentifier: "SPDXRef-Snippet",
SnippetFromFileSPDXIdentifier: "SPDXRef-DoapSource",
SnippetSPDXIdentifier: "Snippet",
SnippetFromFileSPDXIdentifier: "DoapSource",
Ranges: []spdx.SnippetRange{
{
StartPointer: spdx.SnippetRangePointer{
Offset: 310,
FileSPDXIdentifier: "SPDXRef-DoapSource",
FileSPDXIdentifier: "DoapSource",
},
EndPointer: spdx.SnippetRangePointer{
Offset: 420,
FileSPDXIdentifier: "SPDXRef-DoapSource",
FileSPDXIdentifier: "DoapSource",
},
},
{
StartPointer: spdx.SnippetRangePointer{
LineNumber: 5,
FileSPDXIdentifier: "SPDXRef-DoapSource",
FileSPDXIdentifier: "DoapSource",
},
EndPointer: spdx.SnippetRangePointer{
LineNumber: 23,
FileSPDXIdentifier: "SPDXRef-DoapSource",
FileSPDXIdentifier: "DoapSource",
},
},
},
Expand Down
3 changes: 2 additions & 1 deletion rdfloader/parser2v2/parse_annotation.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
gordfParser "github.com/spdx/gordf/rdfloader/parser"
"github.com/spdx/tools-golang/spdx"
"github.com/spdx/tools-golang/utils"
)

// creates a new instance of annotation and sets the annotation attributes
Expand Down Expand Up @@ -54,7 +55,7 @@ func setAnnotationToParser(parser *rdfParser2_2, annotation *spdx.Annotation2_2)

// annotator is of type [Person|Organization|Tool]:String
func setAnnotatorFromString(annotatorString string, ann *spdx.Annotation2_2) error {
subkey, subvalue, err := ExtractSubs(annotatorString, ":")
subkey, subvalue, err := utils.ExtractSubs(annotatorString)
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion rdfloader/parser2v2/parse_creation_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
gordfParser "github.com/spdx/gordf/rdfloader/parser"
"github.com/spdx/tools-golang/spdx"
"github.com/spdx/tools-golang/utils"
)

// Cardinality: Mandatory, one.
Expand Down Expand Up @@ -36,7 +37,7 @@ func (parser *rdfParser2_2) parseCreationInfoFromNode(ci *spdx.CreationInfo2_2,
}

func setCreator(creatorStr string, ci *spdx.CreationInfo2_2) error {
entityType, entity, err := ExtractSubs(creatorStr, ":")
entityType, entity, err := utils.ExtractSubs(creatorStr)
if err != nil {
return fmt.Errorf("error setting creator of a creation info: %s", err)
}
Expand Down
2 changes: 1 addition & 1 deletion rdfloader/parser2v2/parse_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func (parser *rdfParser2_2) setUnpackagedFiles() {
func setFileIdentifier(idURI string, file *spdx.File2_2) (err error) {
idURI = strings.TrimSpace(idURI)
uriFragment := getLastPartOfURI(idURI)
file.FileSPDXIdentifier, err = ExtractElementID(uriFragment)
err = file.FileSPDXIdentifier.FromString(uriFragment)
if err != nil {
return fmt.Errorf("error setting file identifier: %s", err)
}
Expand Down
9 changes: 5 additions & 4 deletions rdfloader/parser2v2/parse_license.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
gordfParser "github.com/spdx/gordf/rdfloader/parser"
"github.com/spdx/gordf/rdfwriter"
"strconv"
"strings"
)

Expand Down Expand Up @@ -37,7 +38,7 @@ func (parser *rdfParser2_2) getAnyLicenseFromNode(node *gordfParser.Node) (AnyLi
parser.cache[node.ID].Color = GREY

// setting state color to black when we're done parsing this node.
defer func(){parser.cache[node.ID].Color = BLACK}()
defer func() { parser.cache[node.ID].Color = BLACK }()

associatedTriples := rdfwriter.FilterTriples(parser.gordfParserObj.Triples, &node.ID, nil, nil)
if len(associatedTriples) == 0 {
Expand Down Expand Up @@ -256,7 +257,7 @@ func (parser *rdfParser2_2) getLicenseFromNode(node *gordfParser.Node) (lic Lice
value := triple.Object.ID
switch triple.Predicate.ID {
case SPDX_IS_OSI_APPROVED:
lic.isOsiApproved, err = boolFromString(value)
lic.isOsiApproved, err = strconv.ParseBool(value)
if err != nil {
return lic, fmt.Errorf("error parsing isOsiApproved attribute of a License: %v", err)
}
Expand All @@ -269,12 +270,12 @@ func (parser *rdfParser2_2) getLicenseFromNode(node *gordfParser.Node) (lic Lice
case SPDX_STANDARD_LICENSE_HEADER_TEMPLATE:
lic.standardLicenseHeaderTemplate = value
case SPDX_IS_DEPRECATED_LICENSE_ID:
lic.isDeprecatedLicenseID, err = boolFromString(value)
lic.isDeprecatedLicenseID, err = strconv.ParseBool(value)
if err != nil {
return lic, fmt.Errorf("error parsing isDeprecatedLicenseId attribute of a License: %v", err)
}
case SPDX_IS_FSF_LIBRE:
lic.isFsfLibre, err = boolFromString(value)
lic.isFsfLibre, err = strconv.ParseBool(value)
if err != nil {
return lic, fmt.Errorf("error parsing isFsfLibre attribute of a License: %v", err)
}
Expand Down
14 changes: 8 additions & 6 deletions rdfloader/parser2v2/parse_package.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ package parser2v2

import (
"fmt"
"github.com/spdx/tools-golang/utils"
"strconv"
"strings"

gordfParser "github.com/spdx/gordf/rdfloader/parser"
Expand Down Expand Up @@ -34,16 +36,16 @@ func (parser *rdfParser2_2) getPackageFromNode(packageNode *gordfParser.Node) (p
defer func() { parser.cache[packageNode.ID].Color = BLACK }()

// setting the SPDXIdentifier for the package.
eId, err := ExtractElementID(getLastPartOfURI(packageNode.ID))
// 3.2
err = pkg.PackageSPDXIdentifier.FromString(getLastPartOfURI(packageNode.ID))
if err != nil {
return nil, fmt.Errorf("error extracting elementID of a package identifier: %v", err)
}
pkg.PackageSPDXIdentifier = eId // 3.2

// check if we already have a package initialized for this ID
existingPackageIndex := -1
for ii, existingPkg := range parser.doc.Packages {
if existingPkg != nil && existingPkg.PackageSPDXIdentifier == eId {
if existingPkg != nil && existingPkg.PackageSPDXIdentifier == pkg.PackageSPDXIdentifier {
existingPackageIndex = ii
pkg = existingPkg
break
Expand Down Expand Up @@ -246,7 +248,7 @@ func setPackageSupplier(pkg *spdx.Package2_2, value string) error {
return nil
}

subKey, subValue, err := ExtractSubs(value, ":")
subKey, subValue, err := utils.ExtractSubs(value)
if err != nil {
return fmt.Errorf("package supplier must be of the form NOASSERTION or [Person|Organization]: string. found: %s", value)
}
Expand Down Expand Up @@ -275,7 +277,7 @@ func setPackageOriginator(pkg *spdx.Package2_2, value string) error {
return nil
}

subKey, subValue, err := ExtractSubs(value, ":")
subKey, subValue, err := utils.ExtractSubs(value)
if err != nil {
return fmt.Errorf("package Originator must be of the form NOASSERTION or [Person|Organization]: string. found: %s", value)
}
Expand Down Expand Up @@ -312,7 +314,7 @@ func setDocumentLocationFromURI(pkg *spdx.Package2_2, locationURI string) error
// boolValue is a string of type "true" or "false"
func setFilesAnalyzed(pkg *spdx.Package2_2, boolValue string) (err error) {
pkg.IsFilesAnalyzedTagPresent = true
pkg.FilesAnalyzed, err = boolFromString(boolValue)
pkg.FilesAnalyzed, err = strconv.ParseBool(boolValue)
return err
}

Expand Down
15 changes: 5 additions & 10 deletions rdfloader/parser2v2/parse_relationship.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (parser *rdfParser2_2) parseRelationship(triple *gordfParser.Triple) (err e
parser.cache[triple.Object.ID].Color = GREY

// setting state color to black to indicate when we're done parsing this node.
defer func(){parser.cache[triple.Object.ID].Color = BLACK}();
defer func() { parser.cache[triple.Object.ID].Color = BLACK }()

for _, subTriple := range parser.nodeToTriples(triple.Object) {
switch subTriple.Predicate.ID {
Expand Down Expand Up @@ -112,7 +112,7 @@ func (parser *rdfParser2_2) parseRelatedElementFromTriple(reln *spdx.Relationshi
case SPDX_SPDX_ELEMENT:
// it shouldn't be associated with any other triple.
// it must be a uri reference.
reln.RefB, err = ExtractDocElementID(getLastPartOfURI(triple.Subject.ID))
err = reln.RefB.FromString(getLastPartOfURI(triple.Subject.ID))
if err != nil {
return err
}
Expand All @@ -124,15 +124,10 @@ func (parser *rdfParser2_2) parseRelatedElementFromTriple(reln *spdx.Relationshi

// references like RefA and RefB of any relationship
func getReferenceFromURI(uri string) (spdx.DocElementID, error) {
var docElementID spdx.DocElementID
fragment := getLastPartOfURI(uri)
switch strings.ToLower(strings.TrimSpace(fragment)) {
case "noassertion", "none":
return spdx.DocElementID{
DocumentRefID: "",
ElementRefID: spdx.ElementID(strings.ToUpper(fragment)),
}, nil
}
return ExtractDocElementID(fragment)
err := docElementID.FromString(fragment)
return docElementID, err
}

// note: relationshipType is case sensitive.
Expand Down
4 changes: 2 additions & 2 deletions rdfloader/parser2v2/parse_relationship_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func Test_getReferenceFromURI(t *testing.T) {
if ref.DocumentRefID != "" {
t.Errorf("reference's documentRefID should've been empty, found %s", ref.DocumentRefID)
}
if ref.ElementRefID != "NOASSERTION" {
if ref.SpecialID != "NOASSERTION" {
t.Errorf("mismatching elementRefID. Found %s, expected %s", ref.ElementRefID, "NOASSERTION")
}

Expand All @@ -30,7 +30,7 @@ func Test_getReferenceFromURI(t *testing.T) {
if ref.DocumentRefID != "" {
t.Errorf("reference's documentRefID should've been empty, found %s", ref.DocumentRefID)
}
if ref.ElementRefID != "NONE" {
if ref.SpecialID != "NONE" {
t.Errorf("mismatching elementRefID. Found %s, expected %s", ref.ElementRefID, "NONE")
}

Expand Down
3 changes: 2 additions & 1 deletion rdfloader/parser2v2/parse_review.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
gordfParser "github.com/spdx/gordf/rdfloader/parser"
"github.com/spdx/tools-golang/spdx"
"github.com/spdx/tools-golang/utils"
)

func (parser *rdfParser2_2) setReviewFromNode(reviewedNode *gordfParser.Node) error {
Expand All @@ -24,7 +25,7 @@ func (parser *rdfParser2_2) setReviewFromNode(reviewedNode *gordfParser.Node) er
case SPDX_REVIEWER:
// cardinality: max 1
var err error
review.ReviewerType, review.Reviewer, err = ExtractSubs(triple.Object.ID, ":")
review.ReviewerType, review.Reviewer, err = utils.ExtractSubs(triple.Object.ID)
if err != nil {
return fmt.Errorf("error parsing reviewer: %v", err)
}
Expand Down
8 changes: 6 additions & 2 deletions rdfloader/parser2v2/parse_snippet_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ func (parser *rdfParser2_2) getSnippetInformationFromNode2_2(node *gordfParser.N
if err != nil {
return nil, err
}
docElemID, err := ExtractDocElementID(getLastPartOfURI(siTriple.Object.ID))
var docElemID spdx.DocElementID
err = docElemID.FromString(getLastPartOfURI(siTriple.Object.ID))
if err != nil {
return nil, err
}
si.SnippetFromFileSPDXIdentifier = docElemID.ElementRefID
case SPDX_RANGE:
// cardinality: min 1
Expand Down Expand Up @@ -189,7 +193,7 @@ func (parser *rdfParser2_2) parseRangeReference(node *gordfParser.Node, snippet

func setSnippetID(uri string, si *spdx.Snippet2_2) (err error) {
fragment := getLastPartOfURI(uri)
si.SnippetSPDXIdentifier, err = ExtractElementID(fragment)
err = si.SnippetSPDXIdentifier.FromString(fragment)
if err != nil {
return fmt.Errorf("error setting snippet identifier: %v", uri)
}
Expand Down
Loading