Skip to content

Commit

Permalink
if namespace is empty string, change it to a hypen for the coordinates
Browse files Browse the repository at this point in the history
Signed-off-by: pxp928 <[email protected]>
  • Loading branch information
pxp928 committed Nov 5, 2024
1 parent 127e3e3 commit aead181
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 14 deletions.
34 changes: 21 additions & 13 deletions pkg/misc/coordinates/coordinates.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,23 +127,23 @@ func ConvertPurlToCoordinate(purlUri string) (*Coordinate, error) {
return &Coordinate{
CoordinateType: "pod",
Provider: "cocoapods",
Namespace: "-",
Namespace: emptyToHyphen(pkg.Namespace),
Name: pkg.Name,
Revision: pkg.Version,
}, nil
case "cargo":
return &Coordinate{
CoordinateType: "crate",
Provider: "cratesio",
Namespace: "-",
Namespace: emptyToHyphen(pkg.Namespace),
Name: pkg.Name,
Revision: pkg.Version,
}, nil
case "composer":
return &Coordinate{
CoordinateType: pkg.Type,
Provider: "packagist",
Namespace: pkg.Namespace,
Namespace: emptyToHyphen(pkg.Namespace),
Name: pkg.Name,
Revision: pkg.Version,
}, nil
Expand Down Expand Up @@ -175,17 +175,17 @@ func ConvertPurlToCoordinate(purlUri string) (*Coordinate, error) {
}

// subdir is the associated platform
var Namespace string
var namespace string
if subdir, ok := qualifiers["subdir"]; ok {
Namespace = subdir
namespace = subdir
} else {
return nil, fmt.Errorf("failed to find subdir for conda")
}

return &Coordinate{
CoordinateType: pkg.Type,
Provider: Provider,
Namespace: Namespace,
Namespace: emptyToHyphen(namespace),
Name: pkg.Name,
Revision: Revision,
}, nil
Expand Down Expand Up @@ -221,23 +221,23 @@ func ConvertPurlToCoordinate(purlUri string) (*Coordinate, error) {
return &Coordinate{
CoordinateType: pkg.Type,
Provider: "rubygems",
Namespace: "-",
Namespace: emptyToHyphen(pkg.Namespace),
Name: pkg.Name,
Revision: pkg.Version,
}, nil
case "github":
return &Coordinate{
CoordinateType: "git",
Provider: pkg.Type,
Namespace: pkg.Namespace,
Namespace: emptyToHyphen(pkg.Namespace),
Name: pkg.Name,
Revision: pkg.Version,
}, nil
case "golang":
return &Coordinate{
CoordinateType: "go",
Provider: pkg.Type,
Namespace: url.QueryEscape(pkg.Namespace),
Namespace: url.QueryEscape(emptyToHyphen(pkg.Namespace)),
Name: pkg.Name,
Revision: pkg.Version,
}, nil
Expand All @@ -257,23 +257,23 @@ func ConvertPurlToCoordinate(purlUri string) (*Coordinate, error) {
return &Coordinate{
CoordinateType: "maven",
Provider: Provider,
Namespace: pkg.Namespace,
Namespace: emptyToHyphen(pkg.Namespace),
Name: pkg.Name,
Revision: pkg.Version,
}, nil
case "npm":
return &Coordinate{
CoordinateType: pkg.Type,
Provider: "npmjs",
Namespace: pkg.Namespace,
Namespace: emptyToHyphen(pkg.Namespace),
Name: pkg.Name,
Revision: pkg.Version,
}, nil
case "nuget":
return &Coordinate{
CoordinateType: pkg.Type,
Provider: "nuget",
Namespace: "-",
Namespace: emptyToHyphen(pkg.Namespace),
Name: pkg.Name,
Revision: pkg.Version,
}, nil
Expand All @@ -285,7 +285,7 @@ func ConvertPurlToCoordinate(purlUri string) (*Coordinate, error) {
return &Coordinate{
CoordinateType: pkg.Type,
Provider: "pypi",
Namespace: "-",
Namespace: emptyToHyphen(pkg.Namespace),
Name: pkg.Name,
Revision: pkg.Version,
}, nil
Expand All @@ -300,3 +300,11 @@ func (c *Coordinate) ToString() string {
return fmt.Sprintf("%s/%s/%s/%s/%s", c.CoordinateType, c.Provider, c.Namespace, c.Name, c.Revision)
}
}

func emptyToHyphen(namespace string) string {
if namespace == "" {
return "-"
} else {
return namespace
}
}
26 changes: 25 additions & 1 deletion pkg/misc/coordinates/coordinates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,18 @@ func TestConvertPurlToCoordinate(t *testing.T) {
Revision: "244fd47e07d1004",
},
wantErr: false,
},
{
Name: "github",
purlUri: "pkg:github/purl-spec@244fd47e07d1004#everybody/loves/dogs",
want: &Coordinate{
CoordinateType: "git",
Provider: "github",
Namespace: "-",
Name: "purl-spec",
Revision: "244fd47e07d1004",
},
wantErr: false,
}, {
Name: "golang",
purlUri: "pkg:golang/github.com/gorilla/context@234fd47e07d1004f0aed9c#api",
Expand All @@ -160,6 +172,18 @@ func TestConvertPurlToCoordinate(t *testing.T) {
Revision: "234fd47e07d1004f0aed9c",
},
wantErr: false,
},
{
Name: "golang",
purlUri: "pkg:golang/context@234fd47e07d1004f0aed9c#api",
want: &Coordinate{
CoordinateType: "go",
Provider: "golang",
Namespace: "-",
Name: "context",
Revision: "234fd47e07d1004f0aed9c",
},
wantErr: false,
}, {
Name: "maven - mavencentral",
purlUri: "pkg:maven/org.apache.xmlgraphics/[email protected]?type=zip&classifier=dist",
Expand Down Expand Up @@ -235,7 +259,7 @@ func TestConvertPurlToCoordinate(t *testing.T) {
t.Errorf("ConvertPurlToCoordinate() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(*got, *tt.want) {
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("ConvertPurlToCoordinate() = %v, want %v", got, tt.want)
}
})
Expand Down

0 comments on commit aead181

Please sign in to comment.