diff --git a/ecosystem.go b/ecosystem.go index acdc0f7..331b6b7 100644 --- a/ecosystem.go +++ b/ecosystem.go @@ -4,6 +4,8 @@ import ( "strings" ) +const ecosystemMaven = "maven" + // purlTypeForEcosystem maps ecosystem names to PURL types. // Most ecosystems use their name as the PURL type, but some differ. var purlTypeForEcosystem = map[string]string{ @@ -28,7 +30,7 @@ var osvEcosystemNames = map[string]string{ "pypi": "PyPI", "cargo": "crates.io", "golang": "Go", - "maven": "Maven", + ecosystemMaven: "Maven", "nuget": "NuGet", "composer": "Packagist", "hex": "Hex", @@ -39,13 +41,13 @@ var osvEcosystemNames = map[string]string{ // depsdevSystemNames maps PURL types to deps.dev system names. var depsdevSystemNames = map[string]string{ - "npm": "NPM", - "gem": "RUBYGEMS", - "pypi": "PYPI", - "cargo": "CARGO", - "golang": "GO", - "maven": "MAVEN", - "nuget": "NUGET", + "npm": "NPM", + "gem": "RUBYGEMS", + "pypi": "PYPI", + "cargo": "CARGO", + "golang": "GO", + ecosystemMaven: "MAVEN", + "nuget": "NUGET", } // defaultNamespaces defines default namespaces for certain ecosystems. @@ -128,8 +130,8 @@ func MakePURL(ecosystem, name, version string) *PURL { switch NormalizeEcosystem(ecosystem) { case "npm": if strings.HasPrefix(name, "@") { - parts := strings.SplitN(name, "/", 2) - if len(parts) == 2 { + parts := strings.SplitN(name, "/", 2) //nolint:mnd + if len(parts) == 2 { //nolint:mnd namespace = parts[0] // Keep the @ for packageurl-go pkgName = parts[1] } @@ -139,22 +141,22 @@ func MakePURL(ecosystem, name, version string) *PURL { namespace = name[:idx] pkgName = name[idx+1:] } - case "maven": + case ecosystemMaven: if strings.Contains(name, ":") { - parts := strings.SplitN(name, ":", 2) + parts := strings.SplitN(name, ":", 2) //nolint:mnd namespace = parts[0] pkgName = parts[1] } case "packagist", "composer": if strings.Contains(name, "/") { - parts := strings.SplitN(name, "/", 2) + parts := strings.SplitN(name, "/", 2) //nolint:mnd namespace = parts[0] pkgName = parts[1] } case "github-actions": // GitHub Actions: owner/repo or owner/repo/path -> namespace=owner, name=repo (path ignored) if strings.Contains(name, "/") { - parts := strings.SplitN(name, "/", 3) + parts := strings.SplitN(name, "/", 3) //nolint:mnd namespace = parts[0] pkgName = parts[1] } diff --git a/makepurl.go b/makepurl.go index 31dce6e..bf1e7a4 100644 --- a/makepurl.go +++ b/makepurl.go @@ -46,7 +46,7 @@ func BuildPURLString(ecosystem, name, version, registryURL string) string { n += 1 + len(cleanVersion) // "@" + version } if needsQualifier { - n += 16 + len(registryURL) // "?repository_url=" + value + n += len("?repository_url=") + len(registryURL) } var b strings.Builder @@ -138,8 +138,8 @@ func writeComponentEscaped(b *strings.Builder, s string) { b.WriteByte(c) } else { b.WriteByte('%') - b.WriteByte(hexDigit(c >> 4)) - b.WriteByte(hexDigit(c & 0x0f)) + b.WriteByte(hexDigit(c >> 4)) //nolint:mnd + b.WriteByte(hexDigit(c & 0x0f)) //nolint:mnd } } } @@ -163,8 +163,8 @@ func writeQualifierEscaped(b *strings.Builder, s string) { b.WriteByte(c) } else { b.WriteByte('%') - b.WriteByte(hexDigit(c >> 4)) - b.WriteByte(hexDigit(c & 0x0f)) + b.WriteByte(hexDigit(c >> 4)) //nolint:mnd + b.WriteByte(hexDigit(c & 0x0f)) //nolint:mnd } } } @@ -175,8 +175,8 @@ func isQualifierValueSafe(c byte) bool { } func hexDigit(b byte) byte { - if b < 10 { + if b < 10 { //nolint:mnd return '0' + b } - return 'A' + b - 10 + return 'A' + b - 10 //nolint:mnd } diff --git a/registry.go b/registry.go index f890100..dd62d91 100644 --- a/registry.go +++ b/registry.go @@ -50,21 +50,23 @@ func expandTemplate(rc *RegistryConfig, namespace, name, version string) (string // Select the appropriate template if version != "" && rc.Components.VersionInURL { - if hasNamespace && rc.URITemplateWithVersion != "" { + switch { + case hasNamespace && rc.URITemplateWithVersion != "": template = rc.URITemplateWithVersion - } else if !hasNamespace && rc.URITemplateWithVersionNoNS != "" { + case !hasNamespace && rc.URITemplateWithVersionNoNS != "": template = rc.URITemplateWithVersionNoNS - } else if rc.URITemplateWithVersion != "" { + case rc.URITemplateWithVersion != "": template = rc.URITemplateWithVersion } } if template == "" { - if hasNamespace { + switch { + case hasNamespace: template = rc.URITemplate - } else if rc.URITemplateNoNamespace != "" { + case rc.URITemplateNoNamespace != "": template = rc.URITemplateNoNamespace - } else { + default: template = rc.URITemplate } } @@ -129,19 +131,19 @@ func ParseRegistryURLWithType(url, purlType string) (*PURL, error) { if len(matches) > 1 { namespace = matches[1] } - if len(matches) > 2 { + if len(matches) > 2 { //nolint:mnd name = matches[2] } - if len(matches) > 3 { + if len(matches) > 3 { //nolint:mnd version = matches[3] } } else { // Namespace is optional: matches[1]=namespace (maybe empty), matches[2]=name - if len(matches) > 2 { + if len(matches) > 2 { //nolint:mnd namespace = matches[1] name = matches[2] } - if len(matches) > 3 { + if len(matches) > 3 { //nolint:mnd version = matches[3] } } @@ -150,7 +152,7 @@ func ParseRegistryURLWithType(url, purlType string) (*PURL, error) { if len(matches) > 1 { name = matches[1] } - if len(matches) > 2 { + if len(matches) > 2 { //nolint:mnd version = matches[2] } } diff --git a/types_test.go b/types_test.go index b74ed5c..d3d8b6c 100644 --- a/types_test.go +++ b/types_test.go @@ -20,13 +20,14 @@ func TestTypeInfo(t *testing.T) { for _, tt := range tests { t.Run(tt.purlType, func(t *testing.T) { cfg := TypeInfo(tt.purlType) - if tt.wantNil { + switch { + case tt.wantNil: if cfg != nil { t.Errorf("TypeInfo(%q) = %v, want nil", tt.purlType, cfg) } - } else if cfg == nil { + case cfg == nil: t.Errorf("TypeInfo(%q) = nil, want non-nil", tt.purlType) - } else { + default: if cfg.Description != tt.wantDescription { t.Errorf("Description = %q, want %q", cfg.Description, tt.wantDescription) }