Skip to content
Merged
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
8 changes: 7 additions & 1 deletion fullname.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
package purl

import "strings"

// FullName returns the package name combining namespace and name.
// If there's no namespace, returns just the name.
// Maven uses ":" as separator (groupId:artifactId), all others use "/".
// Maven uses ":" and Swift registry identities use "." as the separator.
// Swift source coordinates and other types use "/".
func (p *PURL) FullName() string {
if p.Namespace == "" {
return p.Name
}
if p.Type == "maven" {
return p.Namespace + ":" + p.Name
}
if p.Type == ecosystemSwift && !strings.ContainsAny(p.Namespace, "./") {
return p.Namespace + "." + p.Name
}
return p.Namespace + "/" + p.Name
}
20 changes: 20 additions & 0 deletions fullname_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ func TestFullName(t *testing.T) {
{"pkg:golang/github.com/gorilla/mux@v1.8.0", "github.com/gorilla/mux"},
{"pkg:golang/google.golang.org/grpc@v1.50.0", "google.golang.org/grpc"},

{"pkg:swift/Mona/Linked-List@1.0.0", "Mona.Linked-List"},
{"pkg:swift/github.com/Apple/Swift-NIO@2.0.0", "github.com/Apple/Swift-NIO"},
{"pkg:swift/git.example.com/group/subgroup/package", "git.example.com/group/subgroup/package"},
{"pkg:swift/localhost/owner/package", "localhost/owner/package"},

// Terraform modules
{"pkg:terraform/hashicorp/consul/aws@0.11.0", "hashicorp/consul/aws"},

Expand Down Expand Up @@ -55,3 +60,18 @@ func TestFullName(t *testing.T) {
})
}
}

func TestSwiftFullNameRoundTrip(t *testing.T) {
for _, name := range []string{"Mona.Linked-List", "My-Scope2.My_Package", "github.com/Apple/Swift-NIO"} {
t.Run(name, func(t *testing.T) {
input := BuildPURLString("swift", name, "1.0.0", "https://registry.example.com")
p, err := Parse(input)
if err != nil {
t.Fatal(err)
}
if got := p.FullName(); got != name {
t.Errorf("FullName() = %q, want %q", got, name)
}
})
}
}
Loading