Skip to content

Commit

Permalink
Add convenience path parse function
Browse files Browse the repository at this point in the history
Add function to parse a given path unsafe.
  • Loading branch information
HeavyWombat committed May 21, 2021
1 parent 6eb97b2 commit 60c1c14
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
11 changes: 11 additions & 0 deletions path.go
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,17 @@ func ParsePathString(pathString string, node *yamlv3.Node) (Path, error) {
return ParseDotStylePathString(pathString, node)
}

// ParsePathStringUnsafe returns a path by parsing a string representation of a
// path, which can either be GoPatch or DotStyle, but will not check the path
// elements against a given YAML document to verify the types (unsafe)
func ParsePathStringUnsafe(pathString string) (Path, error) {
if strings.HasPrefix(pathString, "/") {
return ParseGoPatchStylePathString(pathString)
}

return ParseDotStylePathStringUnsafe(pathString)
}

func (element PathElement) isMapElement() bool {
return len(element.Key) == 0 &&
len(element.Name) > 0
Expand Down
26 changes: 26 additions & 0 deletions path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,32 @@ var _ = Describe("path tests", func() {
{Idx: -1, Key: "", Name: "newkey"},
}}))
})

It("should parse an unspecified path type without checking against a YAML document (unsafe)", func() {
Expect(ParsePathStringUnsafe("list.one.newkey")).To(BeEquivalentTo(Path{DocumentIdx: 0, PathElements: []PathElement{
{Idx: -1, Key: "", Name: "list"},
{Idx: -1, Key: "", Name: "one"},
{Idx: -1, Key: "", Name: "newkey"},
}}))

Expect(ParsePathStringUnsafe("list.0.newkey")).To(BeEquivalentTo(Path{DocumentIdx: 0, PathElements: []PathElement{
{Idx: -1, Key: "", Name: "list"},
{Idx: 0, Key: "", Name: ""},
{Idx: -1, Key: "", Name: "newkey"},
}}))

Expect(ParsePathStringUnsafe("/list/one/newkey")).To(BeEquivalentTo(Path{DocumentIdx: 0, PathElements: []PathElement{
{Idx: -1, Key: "", Name: "list"},
{Idx: -1, Key: "", Name: "one"},
{Idx: -1, Key: "", Name: "newkey"},
}}))

Expect(ParsePathStringUnsafe("/list/0/newkey")).To(BeEquivalentTo(Path{DocumentIdx: 0, PathElements: []PathElement{
{Idx: -1, Key: "", Name: "list"},
{Idx: 0, Key: "", Name: ""},
{Idx: -1, Key: "", Name: "newkey"},
}}))
})
})

Context("parse go-patch style path strings into paths", func() {
Expand Down

0 comments on commit 60c1c14

Please sign in to comment.