Skip to content

Commit 0e28b1e

Browse files
committed
util/json: remove AllPathsWithDepth
This commit reverts a65115d. `AllPathsWithDepth` was initially created to iterate through the children of a json array or object, but due to performance reasons (jsonEncoded would decode the entire object on each call), another approach was taken to use `json.ObjectIter` and `json.FetchValIdx`, thus eliminating the need for `AllPathsWithDepth`. Release note: None
1 parent 664648a commit 0e28b1e

File tree

3 files changed

+18
-142
lines changed

3 files changed

+18
-142
lines changed

pkg/util/json/encoded.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -797,12 +797,12 @@ func (j *jsonEncoded) numInvertedIndexEntries() (int, error) {
797797
return decoded.numInvertedIndexEntries()
798798
}
799799

800-
func (j *jsonEncoded) allPathsWithDepth(depth int) ([]JSON, error) {
800+
func (j *jsonEncoded) allPaths() ([]JSON, error) {
801801
decoded, err := j.decode()
802802
if err != nil {
803803
return nil, err
804804
}
805-
return decoded.allPathsWithDepth(depth)
805+
return decoded.allPaths()
806806
}
807807

808808
// HasContainerLeaf implements the JSON interface.

pkg/util/json/json.go

Lines changed: 16 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -138,12 +138,10 @@ type JSON interface {
138138
// produced if this JSON gets included in an inverted index.
139139
numInvertedIndexEntries() (int, error)
140140

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

148146
// FetchValKey implements the `->` operator for strings, returning nil if the
149147
// key is not found.
@@ -1795,51 +1793,36 @@ func (j jsonObject) numInvertedIndexEntries() (int, error) {
17951793
// through the input. Note that leaves include the empty object and array
17961794
// in addition to scalars.
17971795
func AllPaths(j JSON) ([]JSON, error) {
1798-
return j.allPathsWithDepth(-1)
1796+
return j.allPaths()
17991797
}
18001798

1801-
// AllPathsWithDepth returns a slice of new JSON documents, each a path
1802-
// through the receiver. The depth parameter specifies the maximum depth of
1803-
// the paths to return. If the depth is negative, all paths of any depth are
1804-
// returned. If the depth is 0, the receiver itself is returned. Note that
1805-
// leaves include the empty object and array in addition to scalars.
1806-
func AllPathsWithDepth(j JSON, depth int) ([]JSON, error) {
1807-
return j.allPathsWithDepth(depth)
1808-
}
1809-
1810-
func (j jsonNull) allPathsWithDepth(depth int) ([]JSON, error) {
1799+
func (j jsonNull) allPaths() ([]JSON, error) {
18111800
return []JSON{j}, nil
18121801
}
18131802

1814-
func (j jsonTrue) allPathsWithDepth(depth int) ([]JSON, error) {
1803+
func (j jsonTrue) allPaths() ([]JSON, error) {
18151804
return []JSON{j}, nil
18161805
}
18171806

1818-
func (j jsonFalse) allPathsWithDepth(depth int) ([]JSON, error) {
1807+
func (j jsonFalse) allPaths() ([]JSON, error) {
18191808
return []JSON{j}, nil
18201809
}
18211810

1822-
func (j jsonString) allPathsWithDepth(depth int) ([]JSON, error) {
1811+
func (j jsonString) allPaths() ([]JSON, error) {
18231812
return []JSON{j}, nil
18241813
}
18251814

1826-
func (j jsonNumber) allPathsWithDepth(depth int) ([]JSON, error) {
1815+
func (j jsonNumber) allPaths() ([]JSON, error) {
18271816
return []JSON{j}, nil
18281817
}
18291818

1830-
func (j jsonArray) allPathsWithDepth(depth int) ([]JSON, error) {
1831-
if len(j) == 0 || depth == 0 {
1819+
func (j jsonArray) allPaths() ([]JSON, error) {
1820+
if len(j) == 0 {
18321821
return []JSON{j}, nil
18331822
}
18341823
ret := make([]JSON, 0, len(j))
18351824
for i := range j {
1836-
var paths []JSON
1837-
var err error
1838-
if depth > 0 {
1839-
paths, err = j[i].allPathsWithDepth(depth - 1)
1840-
} else {
1841-
paths, err = j[i].allPathsWithDepth(depth)
1842-
}
1825+
paths, err := j[i].allPaths()
18431826
if err != nil {
18441827
return nil, err
18451828
}
@@ -1850,19 +1833,13 @@ func (j jsonArray) allPathsWithDepth(depth int) ([]JSON, error) {
18501833
return ret, nil
18511834
}
18521835

1853-
func (j jsonObject) allPathsWithDepth(depth int) ([]JSON, error) {
1854-
if len(j) == 0 || depth == 0 {
1836+
func (j jsonObject) allPaths() ([]JSON, error) {
1837+
if len(j) == 0 {
18551838
return []JSON{j}, nil
18561839
}
18571840
ret := make([]JSON, 0, len(j))
18581841
for i := range j {
1859-
var paths []JSON
1860-
var err error
1861-
if depth > 0 {
1862-
paths, err = j[i].v.allPathsWithDepth(depth - 1)
1863-
} else {
1864-
paths, err = j[i].v.allPathsWithDepth(depth)
1865-
}
1842+
paths, err := j[i].v.allPaths()
18661843
if err != nil {
18671844
return nil, err
18681845
}

pkg/util/json/json_test.go

Lines changed: 0 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -2898,104 +2898,3 @@ func TestToDecimal(t *testing.T) {
28982898
})
28992899
}
29002900
}
2901-
2902-
func TestAllPaths(t *testing.T) {
2903-
cases := []struct {
2904-
json string
2905-
expected []string
2906-
}{
2907-
{`{}`, []string{`{}`}},
2908-
{`[]`, []string{`[]`}},
2909-
{`[1, 2, 3]`, []string{`[1]`, `[2]`, `[3]`}},
2910-
{`{"foo": {"bar": [1, 2, 3]}}`, []string{`{"foo": {"bar": [1]}}`, `{"foo": {"bar": [2]}}`, `{"foo": {"bar": [3]}}`}},
2911-
{
2912-
`{"a": [1, 2, true, false], "b": {"ba": 0, "bb": null}, "c": {}}`,
2913-
[]string{`{"a": [1]}`, `{"a": [2]}`, `{"a": [true]}`, `{"a": [false]}`, `{"b": {"ba": 0}}`, `{"b": {"bb": null}}`, `{"c": {}}`},
2914-
},
2915-
}
2916-
2917-
for i, tc := range cases {
2918-
t.Run(fmt.Sprintf("all paths - %d", i), func(t *testing.T) {
2919-
j := parseJSON(t, tc.json)
2920-
paths, err := AllPaths(j)
2921-
if err != nil {
2922-
t.Fatal(err)
2923-
}
2924-
if len(paths) != len(tc.expected) {
2925-
t.Fatalf("expected %d paths, got %d", len(tc.expected), len(paths))
2926-
}
2927-
for k, p := range paths {
2928-
cmp, err := p.Compare(parseJSON(t, tc.expected[k]))
2929-
if err != nil {
2930-
t.Fatal(err)
2931-
}
2932-
if cmp != 0 {
2933-
t.Fatalf("expected %s, got %s", tc.expected[k], p.String())
2934-
}
2935-
}
2936-
})
2937-
}
2938-
}
2939-
2940-
func TestAllPathsWithDepth(t *testing.T) {
2941-
cases := []struct {
2942-
json string
2943-
depthToExpected map[int][]string
2944-
}{
2945-
{`{}`, map[int][]string{
2946-
-1: {`{}`},
2947-
0: {`{}`},
2948-
1: {`{}`},
2949-
2: {`{}`},
2950-
}},
2951-
{`[]`, map[int][]string{
2952-
-1: {`[]`},
2953-
0: {`[]`},
2954-
1: {`[]`},
2955-
2: {`[]`},
2956-
}},
2957-
{`[1, 2, 3]`, map[int][]string{
2958-
-1: {`[1]`, `[2]`, `[3]`},
2959-
0: {`[1, 2, 3]`},
2960-
1: {`[1]`, `[2]`, `[3]`},
2961-
2: {`[1]`, `[2]`, `[3]`},
2962-
}},
2963-
{`{"foo": {"bar": [1, 2, 3]}}`, map[int][]string{
2964-
-1: {`{"foo": {"bar": [1]}}`, `{"foo": {"bar": [2]}}`, `{"foo": {"bar": [3]}}`},
2965-
0: {`{"foo": {"bar": [1, 2, 3]}}`},
2966-
1: {`{"foo": {"bar": [1, 2, 3]}}`},
2967-
2: {`{"foo": {"bar": [1, 2, 3]}}`},
2968-
3: {`{"foo": {"bar": [1]}}`, `{"foo": {"bar": [2]}}`, `{"foo": {"bar": [3]}}`},
2969-
}},
2970-
{`{"a": [1, 2, true, false], "b": {"ba": 0, "bb": null}, "c": {}}`, map[int][]string{
2971-
-1: {`{"a": [1]}`, `{"a": [2]}`, `{"a": [true]}`, `{"a": [false]}`, `{"b": {"ba": 0}}`, `{"b": {"bb": null}}`, `{"c": {}}`},
2972-
0: {`{"a": [1, 2, true, false], "b": {"ba": 0, "bb": null}, "c": {}}`},
2973-
1: {`{"a": [1, 2, true, false]}`, `{"b": {"ba": 0, "bb": null}}`, `{"c": {}}`},
2974-
2: {`{"a": [1]}`, `{"a": [2]}`, `{"a": [true]}`, `{"a": [false]}`, `{"b": {"ba": 0}}`, `{"b": {"bb": null}}`, `{"c": {}}`},
2975-
}},
2976-
}
2977-
2978-
for i, tc := range cases {
2979-
t.Run(fmt.Sprintf("all paths with depth - %d", i), func(t *testing.T) {
2980-
j := parseJSON(t, tc.json)
2981-
for depth, expected := range tc.depthToExpected {
2982-
paths, err := AllPathsWithDepth(j, depth)
2983-
if err != nil {
2984-
t.Fatal(err)
2985-
}
2986-
if len(paths) != len(expected) {
2987-
t.Fatalf("expected %d paths, got %d", len(expected), len(paths))
2988-
}
2989-
for k, p := range paths {
2990-
cmp, err := p.Compare(parseJSON(t, expected[k]))
2991-
if err != nil {
2992-
t.Fatal(err)
2993-
}
2994-
if cmp != 0 {
2995-
t.Fatalf("expected %s, got %s", expected[k], p.String())
2996-
}
2997-
}
2998-
}
2999-
})
3000-
}
3001-
}

0 commit comments

Comments
 (0)