Skip to content

Commit

Permalink
feat: add EmptyPathSegment, distinct from PathSegment{}
Browse files Browse the repository at this point in the history
  • Loading branch information
rvagg committed Sep 23, 2023
1 parent 05e58cc commit 19ec3f8
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 5 deletions.
11 changes: 7 additions & 4 deletions datamodel/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ type Path struct {
segments []PathSegment
}

// EmptyPath is the Path with no segments.
var EmptyPath = Path{}

// NewPath returns a Path composed of the given segments.
//
// This constructor function does a defensive copy,
Expand Down Expand Up @@ -195,7 +198,7 @@ func (p Path) AppendSegmentInt(ps int64) Path {
// the zero path if it's already empty).
func (p Path) Parent() Path {
if len(p.segments) == 0 {
return Path{}
return EmptyPath
}
return Path{p.segments[0 : len(p.segments)-1]}
}
Expand All @@ -208,15 +211,15 @@ func (p Path) Truncate(i int) Path {
// Last returns the trailing segment of the path.
func (p Path) Last() PathSegment {
if len(p.segments) < 1 {
return PathSegment{}
return EmptyPathSegment
}
return p.segments[len(p.segments)-1]
}

// Pop returns a path with all segments except the last.
func (p Path) Pop() Path {
if len(p.segments) < 1 {
return Path{}
return EmptyPath
}
return Path{p.segments[0 : len(p.segments)-1]}
}
Expand All @@ -225,7 +228,7 @@ func (p Path) Pop() Path {
// If applied to a zero-length path, it returns an empty segment and the same zero-length path.
func (p Path) Shift() (PathSegment, Path) {
if len(p.segments) < 1 {
return PathSegment{}, Path{}
return EmptyPathSegment, EmptyPath
}
return p.segments[0], Path{p.segments[1:]}
}
6 changes: 6 additions & 0 deletions datamodel/pathSegment.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ type PathSegment struct {
i int64
}

// EmptyPathSegment is the PathSegment with no value. This is not the same as
// PathSegment{}, which will be interpreted to mean "0", as a list index.
// EmptyPathSegment is equivalent to ParsePathSegment("") or
// PathSegmentOfString("").
var EmptyPathSegment = PathSegment{i: -1}

// ParsePathSegment parses a string into a PathSegment,
// handling any escaping if present.
// (Note: there is currently no escaping specified for PathSegments,
Expand Down
4 changes: 4 additions & 0 deletions datamodel/path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,8 @@ func TestPathSegmentZeroValue(t *testing.T) {
i, err := PathSegment{}.Index()
qt.Check(t, err, qt.IsNil)
qt.Check(t, i, qt.Equals, int64(0))

qt.Check(t, EmptyPathSegment.String(), qt.Equals, "")
_, err = EmptyPathSegment.Index()
qt.Check(t, err, qt.IsNotNil)
}
2 changes: 1 addition & 1 deletion traversal/walk.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ func (prog Progress) reify(n datamodel.Node, s selector.Selector) (datamodel.Nod
}

// explore into the `InterpretAs` clause to the child selector.
s, err = s.Explore(n, datamodel.PathSegment{})
s, err = s.Explore(n, datamodel.EmptyPathSegment)
if err != nil {
return nil, nil, err
}
Expand Down

0 comments on commit 19ec3f8

Please sign in to comment.