Skip to content

Commit

Permalink
Add IsPathInTree function
Browse files Browse the repository at this point in the history
Add check function `IsPathInTree` to verify whether the provided path is
in the given YAML structure.
  • Loading branch information
HeavyWombat committed Sep 4, 2020
1 parent d9cb0ce commit 3f2d616
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 4 deletions.
15 changes: 15 additions & 0 deletions assets/examples/types.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,36 @@ yaml:

named-entry-list-using-name:
- name: A
foo: bar
- name: B
foo: bar
- name: C
foo: bar
- name: X
foo: bar
- name: Z
foo: bar

named-entry-list-using-key:
- key: A
foo: bar
- key: B
foo: bar
- key: C
foo: bar
- key: X
foo: bar
- key: Z
foo: bar

named-entry-list-using-id:
- id: A
foo: bar
- id: B
foo: bar
- id: C
foo: bar
- id: X
foo: bar
- id: Z
foo: bar
8 changes: 4 additions & 4 deletions getting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ var _ = Describe("getting stuff test cases", func() {
Expect(grab(example, "/yaml/map/intA")).To(BeEquivalentTo(42))
Expect(grab(example, "/yaml/map/mapA")).To(BeAsNode(yml(`{ key0: A, key1: A }`)))
Expect(grab(example, "/yaml/map/listA")).To(BeAsNode(list(`[ A, A, A ]`)))
Expect(grab(example, "/yaml/named-entry-list-using-name/name=B")).To(BeAsNode(yml(`{ name: B }`)))
Expect(grab(example, "/yaml/named-entry-list-using-key/key=B")).To(BeAsNode(yml(`{ key: B }`)))
Expect(grab(example, "/yaml/named-entry-list-using-id/id=B")).To(BeAsNode(yml(`{ id: B }`)))
Expect(grab(example, "/yaml/named-entry-list-using-name/name=B")).To(BeAsNode(yml(`{ name: B, foo: bar }`)))
Expect(grab(example, "/yaml/named-entry-list-using-key/key=B")).To(BeAsNode(yml(`{ key: B, foo: bar }`)))
Expect(grab(example, "/yaml/named-entry-list-using-id/id=B")).To(BeAsNode(yml(`{ id: B, foo: bar }`)))
Expect(grab(example, "/yaml/simple-list/1")).To(BeEquivalentTo("B"))
Expect(grab(example, "/yaml/named-entry-list-using-key/3")).To(BeAsNode(yml(`{ key: X }`)))
Expect(grab(example, "/yaml/named-entry-list-using-key/3")).To(BeAsNode(yml(`{ key: X, foo: bar }`)))

example = yml(assets("bosh-yaml", "manifest.yml"))
Expect(grab(example, "/instance_groups/name=web/networks/name=concourse/static_ips/0")).To(BeEquivalentTo("XX.XX.XX.XX"))
Expand Down
24 changes: 24 additions & 0 deletions path.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,30 @@ func ListPaths(location string, style PathStyle) ([]Path, error) {
return paths, nil
}

// IsPathInTree returns whether the provided path is in the given YAML structure
func IsPathInTree(tree *yamlv3.Node, pathString string) (bool, error) {
searchPath, err := ParsePathString(pathString, tree)
if err != nil {
return false, err
}

resultChan := make(chan bool)

go func() {
for _, node := range tree.Content {
traverseTree(Path{}, node, func(path Path, _ *yamlv3.Node) {
if path.ToGoPatchStyle() == searchPath.ToGoPatchStyle() {
resultChan <- true
}
})

resultChan <- false
}
}()

return <-resultChan, nil
}

func traverseTree(path Path, node *yamlv3.Node, leafFunc func(p Path, n *yamlv3.Node)) {
switch node.Kind {
case yamlv3.DocumentNode:
Expand Down
15 changes: 15 additions & 0 deletions path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,19 @@ var _ = Describe("path tests", func() {
Expect(list).To(BeEquivalentTo(listOfPathsWithSameValue))
})
})

Context("checking for path in YAML", func() {
It("should check whether the provided path is in the YAML", func() {
example := yml(assets("examples", "types.yml"))

Expect(IsPathInTree(example, "/yaml/map/before")).To(BeTrue())
Expect(IsPathInTree(example, "/yaml/map/nope")).To(BeFalse())

Expect(IsPathInTree(example, "/yaml/simple-list/0")).To(BeTrue())
Expect(IsPathInTree(example, "/yaml/simple-list/5")).To(BeFalse())

Expect(IsPathInTree(example, "/yaml/named-entry-list-using-name/name=A/foo")).To(BeTrue())
Expect(IsPathInTree(example, "/yaml/named-entry-list-using-name/name=nope/foo")).To(BeFalse())
})
})
})

0 comments on commit 3f2d616

Please sign in to comment.