Skip to content

Commit

Permalink
Add NormalizedName interface
Browse files Browse the repository at this point in the history
Add interface for for a normalized name and corresponding parser for that type.
New normalized versions of all interfaces are not added since all type information is preserved on calls to Familiar.

Signed-off-by: Derek McGowan <[email protected]> (github: dmcgowan)
  • Loading branch information
dmcgowan committed Jun 27, 2016
1 parent dfe593b commit 6741755
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 50 deletions.
69 changes: 51 additions & 18 deletions reference/normalize.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,21 @@ var (
defaultTag = "latest"
)

// NormalizedName parses a string into a named reference
// NormalizedNamed represents a name which has been
// normalized and has a familiar form. A familiar name
// is what is used in Docker UI. An example normalized
// name is "docker.io/library/ubuntu" and corresponding
// familiar name of "ubuntu".
type NormalizedNamed interface {
Named
Familiar() Named
}

// ParseNormalizedName parses a string into a named reference
// transforming a familiar name from Docker UI to a fully
// qualified reference. If the value may be an identifier
// use ParseAnyReference.
func NormalizedName(s string) (Named, error) {
func ParseNormalizedNamed(s string) (NormalizedNamed, error) {
if ok := anchoredIdentifierRegexp.MatchString(s); ok {
return nil, fmt.Errorf("invalid repository name (%s), cannot specify 64-byte hexadecimal strings", s)
}
Expand All @@ -34,7 +44,15 @@ func NormalizedName(s string) (Named, error) {
return nil, errors.New("invalid reference format: repository name must be lowercase")
}

return ParseNamed(domain + "/" + remainder)
ref, err := Parse(domain + "/" + remainder)
if err != nil {
return nil, err
}
named, isNamed := ref.(NormalizedNamed)
if !isNamed {
return nil, fmt.Errorf("reference %s has no name", ref.String())
}
return named, nil
}

// splitDockerDomain splits a repository name to domain and remotename string.
Expand All @@ -56,32 +74,47 @@ func splitDockerDomain(name string) (domain, remainder string) {
return
}

// FamiliarName returns a shortened version of the name familiar
// familiarizeName returns a shortened version of the name familiar
// to to the Docker UI. Familiar names have the default domain
// "docker.io" and "library/" repository prefix removed.
// For example, "docker.io/library/redis" will have the familiar
// name "redis" and "docker.io/dmcgowan/myapp" will be "dmcgowan/myapp".
func FamiliarName(named Named) Named {
// Returns a familiarized named only reference.
func familiarizeName(named Named) Named {
var repo repository
repo.domain, repo.path = splitDomain(named.Name())

if repo.domain == defaultDomain {
repo.domain = ""
repo.path = strings.TrimPrefix(repo.path, defaultRepoPrefix)
}
if digested, ok := named.(Digested); ok {
return canonicalReference{
repository: repo,
digest: digested.Digest(),
}
return repo
}

func (r reference) Familiar() Named {
return reference{
Named: familiarizeName(r.Named),
tag: r.tag,
digest: r.digest,
}
if tagged, ok := named.(Tagged); ok {
return taggedReference{
repository: repo,
tag: tagged.Tag(),
}
}

func (r repository) Familiar() Named {
return familiarizeName(r)
}

func (t taggedReference) Familiar() Named {
return taggedReference{
Named: familiarizeName(t.Named),
tag: t.tag,
}
}

func (c canonicalReference) Familiar() Named {
return canonicalReference{
Named: familiarizeName(c.Named),
digest: c.digest,
}
return repo
}

// EnsureTagged adds the default tag "latest" to a reference if it only has
Expand Down Expand Up @@ -111,7 +144,7 @@ func ParseAnyReference(ref string) (Reference, error) {
return digestReference(dgst), nil
}

return NormalizedName(ref)
return ParseNormalizedNamed(ref)
}

// ParseAnyReferenceWithSet parses a reference string as a possible short
Expand All @@ -128,5 +161,5 @@ func ParseAnyReferenceWithSet(ref string, ds *digest.Set) (Reference, error) {
}
}

return NormalizedName(ref)
return ParseNormalizedNamed(ref)
}
35 changes: 18 additions & 17 deletions reference/normalize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ func TestValidateReferenceName(t *testing.T) {
}

for _, name := range invalidRepoNames {
_, err := NormalizedName(name)
_, err := ParseNormalizedNamed(name)
if err == nil {
t.Fatalf("Expected invalid repo name for %q", name)
}
}

for _, name := range validRepoNames {
_, err := NormalizedName(name)
_, err := ParseNormalizedNamed(name)
if err != nil {
t.Fatalf("Error parsing repo name %s, got: %q", name, err)
}
Expand Down Expand Up @@ -79,7 +79,7 @@ func TestValidateRemoteName(t *testing.T) {
"dock__er/docker",
}
for _, repositoryName := range validRepositoryNames {
_, err := NormalizedName(repositoryName)
_, err := ParseNormalizedNamed(repositoryName)
if err != nil {
t.Errorf("Repository name should be valid: %v. Error: %v", repositoryName, err)
}
Expand Down Expand Up @@ -117,7 +117,7 @@ func TestValidateRemoteName(t *testing.T) {
"this_is_not_a_valid_namespace_because_its_lenth_is_greater_than_255_this_is_not_a_valid_namespace_because_its_lenth_is_greater_than_255_this_is_not_a_valid_namespace_because_its_lenth_is_greater_than_255_this_is_not_a_valid_namespace_because_its_lenth_is_greater_than_255/docker",
}
for _, repositoryName := range invalidRepositoryNames {
if _, err := NormalizedName(repositoryName); err == nil {
if _, err := ParseNormalizedNamed(repositoryName); err == nil {
t.Errorf("Repository name should be invalid: %v", repositoryName)
}
}
Expand Down Expand Up @@ -214,17 +214,17 @@ func TestParseRepositoryInfo(t *testing.T) {
refStrings = append(refStrings, tcase.AmbiguousName)
}

var refs []Named
var refs []NormalizedNamed
for _, r := range refStrings {
named, err := NormalizedName(r)
named, err := ParseNormalizedNamed(r)
if err != nil {
t.Fatal(err)
}
refs = append(refs, named)
}

for _, r := range refs {
if expected, actual := tcase.FamiliarName, FamiliarName(r).Name(); expected != actual {
if expected, actual := tcase.FamiliarName, r.Familiar().Name(); expected != actual {
t.Fatalf("Invalid normalized reference for %q. Expected %q, got %q", r, expected, actual)
}
if expected, actual := tcase.FullName, r.String(); expected != actual {
Expand All @@ -242,31 +242,32 @@ func TestParseRepositoryInfo(t *testing.T) {
}

func TestParseReferenceWithTagAndDigest(t *testing.T) {
ref, err := NormalizedName("busybox:latest@sha256:86e0e091d0da6bde2456dbb48306f3956bbeb2eae1b5b9a43045843f69fe4aaa")
shortRef := "busybox:latest@sha256:86e0e091d0da6bde2456dbb48306f3956bbeb2eae1b5b9a43045843f69fe4aaa"
nref, err := ParseNormalizedNamed(shortRef)
if err != nil {
t.Fatal(err)
}
if expected, actual := "docker.io/library/busybox:latest@sha256:86e0e091d0da6bde2456dbb48306f3956bbeb2eae1b5b9a43045843f69fe4aaa", ref.String(); actual != expected {
t.Fatalf("Invalid parsed reference for %q: expected %q, got %q", ref, expected, actual)
if expected, actual := "docker.io/library/"+shortRef, nref.String(); actual != expected {
t.Fatalf("Invalid parsed reference for %q: expected %q, got %q", nref, expected, actual)
}

ref = FamiliarName(ref)
if _, isTagged := ref.(NamedTagged); isTagged {
t.Fatalf("Reference from %q should not support tag", ref)
ref := nref.Familiar()
if _, isTagged := ref.(NamedTagged); !isTagged {
t.Fatalf("Reference from %q should support tag", ref)
}
if _, isCanonical := ref.(Canonical); !isCanonical {
t.Fatalf("Reference from %q should not support digest", ref)
t.Fatalf("Reference from %q should support digest", ref)
}
if expected, actual := "busybox@sha256:86e0e091d0da6bde2456dbb48306f3956bbeb2eae1b5b9a43045843f69fe4aaa", FamiliarName(ref).String(); actual != expected {
if expected, actual := shortRef, ref.String(); actual != expected {
t.Fatalf("Invalid parsed reference for %q: expected %q, got %q", ref, expected, actual)
}
}

func TestInvalidReferenceComponents(t *testing.T) {
if _, err := NormalizedName("-foo"); err == nil {
if _, err := ParseNormalizedNamed("-foo"); err == nil {
t.Fatal("Expected WithName to detect invalid name")
}
ref, err := NormalizedName("busybox")
ref, err := ParseNormalizedNamed("busybox")
if err != nil {
t.Fatal(err)
}
Expand Down
30 changes: 15 additions & 15 deletions reference/reference.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,8 @@ func Parse(s string) (Reference, error) {
}

ref := reference{
repository: repo,
tag: matches[2],
Named: repo,
tag: matches[2],
}
if matches[3] != "" {
var err error
Expand Down Expand Up @@ -272,8 +272,8 @@ func WithTag(name Named, tag string) (NamedTagged, error) {
repo.path = name.Name()
}
return taggedReference{
repository: repo,
tag: tag,
Named: repo,
tag: tag,
}, nil
}

Expand All @@ -291,13 +291,13 @@ func WithDigest(name Named, digest digest.Digest) (Canonical, error) {
repo.path = name.Name()
}
return canonicalReference{
repository: repo,
digest: digest,
Named: repo,
digest: digest,
}, nil
}

func getBestReferenceType(ref reference) Reference {
if ref.repository.path == "" {
if ref.Name() == "" {
// Allow digest only references
if ref.digest != "" {
return digestReference(ref.digest)
Expand All @@ -307,24 +307,24 @@ func getBestReferenceType(ref reference) Reference {
if ref.tag == "" {
if ref.digest != "" {
return canonicalReference{
repository: ref.repository,
digest: ref.digest,
Named: ref.Named,
digest: ref.digest,
}
}
return ref.repository
return ref.Named
}
if ref.digest == "" {
return taggedReference{
repository: ref.repository,
tag: ref.tag,
Named: ref.Named,
tag: ref.tag,
}
}

return ref
}

type reference struct {
repository
Named
tag string
digest digest.Digest
}
Expand Down Expand Up @@ -376,7 +376,7 @@ func (d digestReference) Digest() digest.Digest {
}

type taggedReference struct {
repository
Named
tag string
}

Expand All @@ -389,7 +389,7 @@ func (t taggedReference) Tag() string {
}

type canonicalReference struct {
repository
Named
digest digest.Digest
}

Expand Down

0 comments on commit 6741755

Please sign in to comment.