From 19a384604e932901092f84ca7a78235f3dde9542 Mon Sep 17 00:00:00 2001 From: tfrench-uber Date: Fri, 16 Jul 2021 19:15:26 -0400 Subject: [PATCH] update label to accept external references to the main repository add a test update remove hardcode logic clean up logic Add comments to clarify label handling add more comments --- label/label.go | 15 ++++++++++++--- label/label_test.go | 7 +++++-- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/label/label.go b/label/label.go index 1d748531e..137365ab8 100644 --- a/label/label.go +++ b/label/label.go @@ -62,7 +62,7 @@ func New(repo, pkg, name string) Label { var NoLabel = Label{} var ( - labelRepoRegexp = regexp.MustCompile(`^$|^[A-Za-z][A-Za-z0-9_]*$`) + labelRepoRegexp = regexp.MustCompile(`^@$|^[A-Za-z][A-Za-z0-9_]*$`) labelPkgRegexp = regexp.MustCompile(`^[A-Za-z0-9/._-]*$`) labelNameRegexp = regexp.MustCompile(`^[A-Za-z0-9_/.+=,@~-]*$`) ) @@ -77,9 +77,14 @@ func Parse(s string) (Label, error) { if strings.HasPrefix(s, "@") { relative = false endRepo := strings.Index(s, "//") - if endRepo > 0 { + if endRepo > len("@") { repo = s[len("@"):endRepo] s = s[endRepo:] + // If the label begins with "@//...", set repo = "@" + // to remain distinct from "//...", where repo = "" + } else if endRepo == len("@") { + repo = s[:len("@")] + s = s[len("@"):] } else { repo = s[len("@"):] s = "//:" + repo @@ -134,8 +139,12 @@ func (l Label) String() string { } var repo string - if l.Repo != "" { + if l.Repo != "" && l.Repo != "@" { repo = fmt.Sprintf("@%s", l.Repo) + } else { + // if l.Repo == "", the label string will begin with "//" + // if l.Repo == "@", the label string will begin with "@//" + repo = l.Repo } if path.Base(l.Pkg) == l.Name { diff --git a/label/label_test.go b/label/label_test.go index 1558d0284..c2eac423b 100644 --- a/label/label_test.go +++ b/label/label_test.go @@ -43,6 +43,9 @@ func TestLabelString(t *testing.T) { }, { l: Label{Relative: true, Name: "foo"}, want: ":foo", + }, { + l: Label{Repo: "@", Pkg: "foo/bar", Name: "baz"}, + want: "@//foo/bar:baz", }, } { if got, want := spec.l.String(), spec.want; got != want { @@ -61,8 +64,8 @@ func TestParse(t *testing.T) { {str: "@//:", wantErr: true}, {str: "@a:b", wantErr: true}, {str: "@a//", wantErr: true}, - {str: "@//:a", want: Label{Name: "a", Relative: false}}, - {str: "@//a:b", want: Label{Repo: "", Pkg: "a", Name: "b"}}, + {str: "@//:a", want: Label{Repo: "@", Name: "a", Relative: false}}, + {str: "@//a:b", want: Label{Repo: "@", Pkg: "a", Name: "b"}}, {str: ":a", want: Label{Name: "a", Relative: true}}, {str: "a", want: Label{Name: "a", Relative: true}}, {str: "//:a", want: Label{Name: "a", Relative: false}},