Skip to content

Commit

Permalink
feat(resourcename): improve revision support
Browse files Browse the repository at this point in the history
* HasParent supports revisions
* Add HasRevision
* Add TrimRevision
  • Loading branch information
odsod committed Jun 29, 2021
1 parent 0c56609 commit 92fd32c
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
12 changes: 11 additions & 1 deletion resourcename/hasparent.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package resourcename

// HasParent tests whether name has the specified parent. Wildcard segments (-) are considered.
// Resource names without revisions are considered parents of the same resource name with a revision.
func HasParent(name, parent string) bool {
if name == "" || parent == "" || name == parent {
return false
Expand All @@ -12,7 +13,16 @@ func HasParent(name, parent string) bool {
if !nameScanner.Scan() {
return false
}
if parentScanner.Segment() != "-" && parentScanner.Segment() != nameScanner.Segment() {
if parentScanner.Segment().IsWildcard() {
continue
}
// Special-case: Identical resource IDs without revision are parents of revisioned resource IDs.
if nameScanner.Segment().Literal().HasRevision() &&
!parentScanner.Segment().Literal().HasRevision() &&
(nameScanner.Segment().Literal().ResourceID() == parentScanner.Segment().Literal().ResourceID()) {
continue
}
if parentScanner.Segment() != nameScanner.Segment() {
return false
}
}
Expand Down
49 changes: 49 additions & 0 deletions resourcename/hasparent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,55 @@ func TestHasParent(t *testing.T) {
parent: "//freight-example.einride.tech/shippers/-",
expected: false,
},

{
test: "revisioned child",
name: "shippers/1/sites/1@beef",
parent: "shippers/1/sites/1",
expected: true,
},

{
test: "revisioned child with other revision",
name: "shippers/1/sites/1@beef",
parent: "shippers/1/sites/1@dead",
expected: false,
},

{
test: "identical revisioned child",
name: "shippers/1/sites/1@beef",
parent: "shippers/1/sites/1@beef",
expected: false,
},

{
test: "revisioned parent",
parent: "datasets/1@beef",
name: "datasets/1@beef/tables/1",
expected: true,
},

{
test: "revisioned parent with non-revisoned child",
parent: "datasets/1@beef",
name: "datasets/1/tables/1",
expected: false,
},

{
test: "revisioned parent with non-matching revision child",
parent: "datasets/1@beef",
name: "datasets/1@dead/tables/1",
expected: false,
},

{
test: "non-revisioned parent with revisioned child",
parent: "datasets/1",
name: "datasets/1@beef/tables/1",
expected: true,
},
} {
tt := tt
t.Run(tt.test, func(t *testing.T) {
Expand Down

0 comments on commit 92fd32c

Please sign in to comment.