Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

util/json: add AllPathsWithDepth #141871

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pkg/util/json/encoded.go
Original file line number Diff line number Diff line change
Expand Up @@ -797,12 +797,12 @@ func (j *jsonEncoded) numInvertedIndexEntries() (int, error) {
return decoded.numInvertedIndexEntries()
}

func (j *jsonEncoded) allPaths() ([]JSON, error) {
func (j *jsonEncoded) allPathsWithDepth(depth int) ([]JSON, error) {
decoded, err := j.decode()
if err != nil {
return nil, err
}
return decoded.allPaths()
return decoded.allPathsWithDepth(depth)
}

// HasContainerLeaf implements the JSON interface.
Expand Down
55 changes: 39 additions & 16 deletions pkg/util/json/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,12 @@ type JSON interface {
// produced if this JSON gets included in an inverted index.
numInvertedIndexEntries() (int, error)

// allPaths returns a slice of new JSON documents, each a path to a leaf
// through the receiver. Note that leaves include the empty object and array
// in addition to scalars.
allPaths() ([]JSON, error)
// allPathsWithDepth returns a slice of new JSON documents, each a path
// through the receiver. The depth parameter specifies the maximum depth of
// the paths to return. If the depth is negative, all paths of any depth are
// returned. If the depth is 0, the receiver itself is returned. Note that
// leaves include the empty object and array in addition to scalars.
allPathsWithDepth(depth int) ([]JSON, error)

// FetchValKey implements the `->` operator for strings, returning nil if the
// key is not found.
Expand Down Expand Up @@ -1717,36 +1719,51 @@ func (j jsonObject) numInvertedIndexEntries() (int, error) {
// through the input. Note that leaves include the empty object and array
// in addition to scalars.
func AllPaths(j JSON) ([]JSON, error) {
return j.allPaths()
return j.allPathsWithDepth(-1)
}

func (j jsonNull) allPaths() ([]JSON, error) {
// AllPathsWithDepth returns a slice of new JSON documents, each a path
// through the receiver. The depth parameter specifies the maximum depth of
// the paths to return. If the depth is negative, all paths of any depth are
// returned. If the depth is 0, the receiver itself is returned. Note that
// leaves include the empty object and array in addition to scalars.
func AllPathsWithDepth(j JSON, depth int) ([]JSON, error) {
return j.allPathsWithDepth(depth)
}

func (j jsonNull) allPathsWithDepth(depth int) ([]JSON, error) {
return []JSON{j}, nil
}

func (j jsonTrue) allPaths() ([]JSON, error) {
func (j jsonTrue) allPathsWithDepth(depth int) ([]JSON, error) {
return []JSON{j}, nil
}

func (j jsonFalse) allPaths() ([]JSON, error) {
func (j jsonFalse) allPathsWithDepth(depth int) ([]JSON, error) {
return []JSON{j}, nil
}

func (j jsonString) allPaths() ([]JSON, error) {
func (j jsonString) allPathsWithDepth(depth int) ([]JSON, error) {
return []JSON{j}, nil
}

func (j jsonNumber) allPaths() ([]JSON, error) {
func (j jsonNumber) allPathsWithDepth(depth int) ([]JSON, error) {
return []JSON{j}, nil
}

func (j jsonArray) allPaths() ([]JSON, error) {
if len(j) == 0 {
func (j jsonArray) allPathsWithDepth(depth int) ([]JSON, error) {
if len(j) == 0 || depth == 0 {
return []JSON{j}, nil
}
ret := make([]JSON, 0, len(j))
for i := range j {
paths, err := j[i].allPaths()
var paths []JSON
var err error
if depth > 0 {
paths, err = j[i].allPathsWithDepth(depth - 1)
} else {
paths, err = j[i].allPathsWithDepth(depth)
}
if err != nil {
return nil, err
}
Expand All @@ -1757,13 +1774,19 @@ func (j jsonArray) allPaths() ([]JSON, error) {
return ret, nil
}

func (j jsonObject) allPaths() ([]JSON, error) {
if len(j) == 0 {
func (j jsonObject) allPathsWithDepth(depth int) ([]JSON, error) {
if len(j) == 0 || depth == 0 {
return []JSON{j}, nil
}
ret := make([]JSON, 0, len(j))
for i := range j {
paths, err := j[i].v.allPaths()
var paths []JSON
var err error
if depth > 0 {
paths, err = j[i].v.allPathsWithDepth(depth - 1)
} else {
paths, err = j[i].v.allPathsWithDepth(depth)
}
if err != nil {
return nil, err
}
Expand Down