Skip to content

Commit

Permalink
Merge pull request #42 from RishabhBhatnagar/gordf
Browse files Browse the repository at this point in the history
Add tests and bug fixes ror RdfLoader files
  • Loading branch information
swinslow committed Nov 8, 2020
2 parents 35fc7e0 + 08d5f11 commit 1308779
Show file tree
Hide file tree
Showing 28 changed files with 4,944 additions and 349 deletions.
2 changes: 2 additions & 0 deletions rdfloader/parser2v2/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ var (
SPDX_SPDX_DOCUMENT = NS_SPDX + "spdxDocument"
SPDX_SPDX_DOCUMENT_CAPITALIZED = NS_SPDX + "SpdxDocument"
SPDX_CHECKSUM = NS_SPDX + "checksum"
SPDX_CHECKSUM_CAPITALIZED = NS_SPDX + "Checksum"
SPDX_ANNOTATION_TYPE = NS_SPDX + "annotationType"
SPDX_ANNOTATION_TYPE_OTHER = NS_SPDX + "annotationType_other"
SPDX_ANNOTATION_TYPE_REVIEW = NS_SPDX + "annotationType_review"
Expand Down Expand Up @@ -90,6 +91,7 @@ var (
SPDX_DISJUNCTIVE_LICENSE_SET = NS_SPDX + "DisjunctiveLicenseSet"
SPDX_CONJUNCTIVE_LICENSE_SET = NS_SPDX + "ConjunctiveLicenseSet"
SPDX_EXTRACTED_LICENSING_INFO = NS_SPDX + "ExtractedLicensingInfo"
SPDX_SIMPLE_LICENSING_INFO = NS_SPDX + "SimpleLicensingInfo"
SPDX_NONE_CAPS = NS_SPDX + "NONE"
SPDX_NOASSERTION_CAPS = NS_SPDX + "NOASSERTION"
SPDX_NONE_SMALL = NS_SPDX + "none"
Expand Down
115 changes: 115 additions & 0 deletions rdfloader/parser2v2/license_utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package parser2v2

import (
"fmt"
gordfParser "github.com/RishabhBhatnagar/gordf/rdfloader/parser"
"strings"
)

/* util methods for licenses and checksums below:*/

// Given the license URI, returns the name of the license defined
// in the last part of the uri.
// This function is susceptible to false-positives.
func getLicenseStringFromURI(uri string) string {
licenseEnd := strings.TrimSpace(getLastPartOfURI(uri))
lower := strings.ToLower(licenseEnd)
if lower == "none" || lower == "noassertion" {
return strings.ToUpper(licenseEnd)
}
return licenseEnd
}

// returns the checksum algorithm and it's value
// In the newer versions, these two strings will be bound to a single checksum struct
// whose pointer will be returned.
func (parser *rdfParser2_2) getChecksumFromNode(checksumNode *gordfParser.Node) (algorithm string, value string, err error) {
var checksumValue, checksumAlgorithm string
for _, checksumTriple := range parser.nodeToTriples(checksumNode) {
switch checksumTriple.Predicate.ID {
case RDF_TYPE:
continue
case SPDX_CHECKSUM_VALUE:
// cardinality: exactly 1
checksumValue = strings.TrimSpace(checksumTriple.Object.ID)
case SPDX_ALGORITHM:
// cardinality: exactly 1
checksumAlgorithm, err = getAlgorithmFromURI(checksumTriple.Object.ID)
if err != nil {
return
}
default:
err = fmt.Errorf("unknown predicate '%s' while parsing checksum node", checksumTriple.Predicate.ID)
return
}
}
return checksumAlgorithm, checksumValue, nil
}

func getAlgorithmFromURI(algorithmURI string) (checksumAlgorithm string, err error) {
fragment := getLastPartOfURI(algorithmURI)
if !strings.HasPrefix(fragment, "checksumAlgorithm_") {
return "", fmt.Errorf("checksum algorithm uri must begin with checksumAlgorithm_. found %s", fragment)
}
algorithm := strings.TrimPrefix(fragment, "checksumAlgorithm_")
algorithm = strings.ToLower(strings.TrimSpace(algorithm))
switch algorithm {
case "md2", "md4", "md5", "md6":
checksumAlgorithm = strings.ToUpper(algorithm)
case "sha1", "sha224", "sha256", "sha384", "sha512":
checksumAlgorithm = strings.ToUpper(algorithm)
default:
return "", fmt.Errorf("unknown checksum algorithm %s", algorithm)
}
return
}

// from a list of licenses, it returns a
// list of string representation of those licenses.
func mapLicensesToStrings(licences []AnyLicenseInfo) []string {
res := make([]string, len(licences), len(licences))
for i, lic := range licences {
res[i] = lic.ToLicenseString()
}
return res
}

/****** Type Functions ******/

// TODO: should probably add brackets while linearizing a nested license.
func (lic ConjunctiveLicenseSet) ToLicenseString() string {
return strings.Join(mapLicensesToStrings(lic.members), " AND ")
}

// TODO: should probably add brackets while linearizing a nested license.
func (lic DisjunctiveLicenseSet) ToLicenseString() string {
return strings.Join(mapLicensesToStrings(lic.members), " OR ")
}

func (lic ExtractedLicensingInfo) ToLicenseString() string {
return lic.licenseID
}

func (operator OrLaterOperator) ToLicenseString() string {
return operator.member.ToLicenseString()
}

func (lic License) ToLicenseString() string {
return lic.licenseID
}

func (lic ListedLicense) ToLicenseString() string {
return lic.licenseID
}

func (lic WithExceptionOperator) ToLicenseString() string {
return lic.member.ToLicenseString()
}

func (lic SpecialLicense) ToLicenseString() string {
return string(lic.value)
}

func (lic SimpleLicensingInfo) ToLicenseString() string {
return lic.licenseID
}

0 comments on commit 1308779

Please sign in to comment.