Return existing builder from PathBuilder.Root()
instead of creating a new one.
#420
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR fixes a bug that I came across. It is triggered by a piece of code like this:
On the current
master
this code causes a panic:The root cause is that the
PathBuilder.Root()
method instanciates a newPathBuilder
instance instead of mutating and returning the reciever. This is inconsistent with the similar methodsIndexAll
,Recursive
,Index
andChild
and with my understanding of the builder pattern.The given code sample assumes that the
.Root()
call mutates the builder, but it doesn't. Therefore the resultingpath
built bybuilder.Build()
does not have a root. This in turn causes the panic when callingpath.String()
.This is not caught by the existing tests, because the existing tests use method chaining, which executes the next call on the builder returned by
builder.Root()
, which is a builder that has a root and therefore works as expected.The fact that
path.String()
causes a panic in this case might be a separate issue that I didn't look into further.