Skip to content

Commit

Permalink
fix: implement missing Types: cran, conan, swift
Browse files Browse the repository at this point in the history
This commit implements the missing purl Types cran,
conan and swift. Furthermore, it adds a function
for validating custom Rules for these types.
  • Loading branch information
shibumi authored and ashcrow committed Feb 3, 2022
1 parent 36e2941 commit 2101d64
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions packageurl.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ var (
var (
// TypeBitbucket is a pkg:bitbucket purl.
TypeBitbucket = "bitbucket"
// TypeCran is a pkg:cran purl.
TypeCran = "cran"
// TypeConan is pkg:conan purl.
TypeConan = "conan"
// TypeComposer is a pkg:composer purl.
TypeComposer = "composer"
// TypeDebian is a pkg:deb purl.
Expand All @@ -71,6 +75,8 @@ var (
TypePyPi = "pypi"
// TypeRPM is a pkg:rpm purl.
TypeRPM = "rpm"
// TypeSwift is pkg:swift purl
TypeSwift = "swift"
)

// Qualifier represents a single key=value qualifier in the package url
Expand Down Expand Up @@ -299,6 +305,11 @@ func FromString(purl string) (PackageURL, error) {
return PackageURL{}, errors.New("name is required")
}

err := validateCustomRules(purlType, name, namespace, version, qualifiers)
if err != nil {
return PackageURL{}, err
}

return PackageURL{
Type: purlType,
Namespace: namespace,
Expand Down Expand Up @@ -334,3 +345,38 @@ func typeAdjustName(purlType, name string) string {
func validQualifierKey(key string) bool {
return QualifierKeyPattern.MatchString(key)
}

func validateCustomRules(purlType, name, ns, version string, qualifiers Qualifiers) error {
q := qualifiers.Map()
switch purlType {
case TypeConan:
if ns != "" {
if val, ok := q["channel"]; ok {
if val == "" {
return errors.New("the qualifier channel must be not empty if namespace is present")
}
} else {
return errors.New("channel qualifier does not exist")
}
}
if ns == "" {
if val, ok := q["channel"]; ok {
if val != "" {
return errors.New("namespace is required if channel is non empty")
}
}
}
case TypeSwift:
if ns == "" {
return errors.New("namespace is required")
}
if version == "" {
return errors.New("version is required")
}
case TypeCran:
if version == "" {
return errors.New("version is required")
}
}
return nil
}

0 comments on commit 2101d64

Please sign in to comment.