<commit_before>package pkg

import (
	"regexp"
	"sort"
	"strings"

	"github.com/anchore/packageurl-go"
	"github.com/anchore/syft/syft/linux"
)

const (
	PURLQualifierArch   = "arch"
	PURLQualifierDistro = "distro"
	PURLQualifierEpoch  = "epoch"
	PURLQualifierVCSURL = "vcs_url"

	// PURLQualifierUpstream this qualifier is not in the pURL spec, but is used by grype to perform indirect matching based on source information
	PURLQualifierUpstream = "upstream"

	purlCargoPkgType  = "cargo"
	purlGradlePkgType = "gradle"
)

type urlIdentifier interface {
	PackageURL(*linux.Release) string
}

func URL(p Package, release *linux.Release) string {
	if p.Metadata != nil {
		if i, ok := p.Metadata.(urlIdentifier); ok {
			return i.PackageURL(release)
		}
	}

	// the remaining cases are primarily reserved for packages without metadata struct instances

	var purlType = p.Type.PackageURLType()
	var name = p.Name
	var namespace = ""

	switch {
	case purlType == "":
		// there is no purl type, don't attempt to craft a purl
		// TODO: should this be a "generic" purl type instead?
		return ""
	case p.Type == GoModulePkg:
		re := regexp.MustCompile(`(/)[^/]*$`)
		fields := re.Split(p.Name, -1)
		if len(fields) > 1 {
			namespace = fields[0]
			name = strings.TrimPrefix(p.Name, namespace+"/")
		}
	case p.Type == NpmPkg:
		fields := strings.SplitN(p.Name, "/", 2)
		if len(fields) > 1 {
			namespace = fields[0]
			name = fields[1]
		}
	}
	// generate a purl from the package data
	return packageurl.NewPackageURL(
		purlType,
		namespace,
		name,
		p.Version,
		nil,<commit_msg>fix: use generic instead of not generating purl (#1119)<commit_after>package pkg

import (
	"regexp"
	"sort"
	"strings"

	"github.com/anchore/packageurl-go"
	"github.com/anchore/syft/syft/linux"
)

const (
	PURLQualifierArch   = "arch"
	PURLQualifierDistro = "distro"
	PURLQualifierEpoch  = "epoch"
	PURLQualifierVCSURL = "vcs_url"

	// PURLQualifierUpstream this qualifier is not in the pURL spec, but is used by grype to perform indirect matching based on source information
	PURLQualifierUpstream = "upstream"

	purlCargoPkgType  = "cargo"
	purlGradlePkgType = "gradle"
)

type urlIdentifier interface {
	PackageURL(*linux.Release) string
}

func URL(p Package, release *linux.Release) string {
	if p.Metadata != nil {
		if i, ok := p.Metadata.(urlIdentifier); ok {
			return i.PackageURL(release)
		}
	}

	// the remaining cases are primarily reserved for packages without metadata struct instances

	var purlType = p.Type.PackageURLType()
	var name = p.Name
	var namespace = ""

	switch {
	case purlType == "":
		purlType = packageurl.TypeGeneric
	case p.Type == GoModulePkg:
		re := regexp.MustCompile(`(/)[^/]*$`)
		fields := re.Split(p.Name, -1)
		if len(fields) > 1 {
			namespace = fields[0]
			name = strings.TrimPrefix(p.Name, namespace+"/")
		}
	case p.Type == NpmPkg:
		fields := strings.SplitN(p.Name, "/", 2)
		if len(fields) > 1 {
			namespace = fields[0]
			name = fields[1]
		}
	}
	// generate a purl from the package data
	return packageurl.NewPackageURL(
		purlType,
		namespace,
		name,
		p.Version,
		nil,
{'fraction_non_alphanumeric': 0.12238325281803543, 'fraction_numerical': 0.005797101449275362, 'mean_word_length': 3.756508422664625, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '714892', 'n_tokens_mistral': 1188, 'n_tokens_neox': 1108, 'n_words': 294}