Skip to content

Commit

Permalink
make PackageURL.String() use a value receiver rather than pointer rec…
Browse files Browse the repository at this point in the history
…eiver

This makes

   fmt.Sprintf("%s", packageurl.FromString("pkg:maven/org.yaml/snakeyaml@1.24"))

output the expected

   "pkg:maven/org.yaml/snakeyaml@1.24"

rather than

   "{maven org.yaml snakeyaml 1.24  }"

As it stands with String() as a pointer receiver one needs to:

   purl := packageurl.FromString("pkg:maven/org.yaml/snakeyaml@1.24")
   fmt.Sprintf("%s", &purl)

to get the intended output.
  • Loading branch information
petergardfjall authored and ashcrow committed Nov 18, 2019
1 parent 04e124e commit 0789fa5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packageurl.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ func (p *PackageURL) ToString() string {
return purl
}

func (p *PackageURL) String() string {
func (p PackageURL) String() string {
return p.ToString()
}

Expand Down
14 changes: 11 additions & 3 deletions packageurl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,14 +237,22 @@ func TestStringer(t *testing.T) {
if tc.IsInvalid == true {
continue
}
instance := packageurl.NewPackageURL(
purlPtr := packageurl.NewPackageURL(
tc.PackageType, tc.Namespace, tc.Name,
tc.Version, tc.Qualifiers(), tc.Subpath)
purlValue := *purlPtr

// Verify that the Stringer implementation returns a result
// equivalent to ToString().
if instance.ToString() != instance.String() {
t.Logf("%s failed: Stringer implementation differs from ToString: %s != %s", tc.Description, instance.String(), instance.ToString())
if purlPtr.ToString() != purlPtr.String() {
t.Logf("%s failed: Stringer implementation differs from ToString: %s != %s", tc.Description, purlPtr.String(), purlPtr.ToString())
t.Fail()
}

// Verify that the %s format modifier works for values.
fmtStr := fmt.Sprintf("%s", purlValue)
if fmtStr != purlPtr.String() {
t.Logf("%s failed: %%s format modifier does not work on values: %s != %s", tc.Description, fmtStr, purlPtr.ToString())
t.Fail()
}
}
Expand Down

0 comments on commit 0789fa5

Please sign in to comment.