Skip to content

Commit 650f43f

Browse files
author
Thomas Charlot
committed
Fix tabify with only one element in array
1 parent 4f78cd0 commit 650f43f

7 files changed

Lines changed: 234 additions & 51 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ vendor/
22
bin/
33
data/
44
.DS_Store
5+
.env

.vscode/launch.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": []
7+
}

tabify/tabify.go

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -117,29 +117,20 @@ func (t *tabify) collect(node *jsonmap.Json, keys ...string) {
117117
t.collect(node.Get(key), append(keys, key)...)
118118
}
119119
} else if node.IsArray() {
120-
121120
nvalues := node.Values()
122-
switch l := len(nvalues); l {
123-
case 0:
124-
return
125-
case 1:
126-
// treat one line array as object
127-
t.collect(nvalues[0], keys...)
128-
default:
129-
for _, item := range nvalues {
130-
t.nodes <- &nodeValue{
131-
eventType: startRow,
132-
key: t.opts.KeyFormatter(keys),
133-
deep: len(keys),
134-
}
135-
136-
t.collect(item, keys...)
137-
138-
t.nodes <- &nodeValue{
139-
eventType: endRow,
140-
key: t.opts.KeyFormatter(keys),
141-
deep: len(keys),
142-
}
121+
for _, item := range nvalues {
122+
t.nodes <- &nodeValue{
123+
eventType: startRow,
124+
key: t.opts.KeyFormatter(keys),
125+
deep: len(keys),
126+
}
127+
128+
t.collect(item, keys...)
129+
130+
t.nodes <- &nodeValue{
131+
eventType: endRow,
132+
key: t.opts.KeyFormatter(keys),
133+
deep: len(keys),
143134
}
144135
}
145136
} else {

tabify/tabify_test.go

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package tabify_test
22

33
import (
44
"encoding/json"
5-
"io/ioutil"
5+
"os"
66
"strings"
77
"testing"
88

@@ -23,6 +23,10 @@ func TestNested(t *testing.T) {
2323
testfile(t, "nested_agg")
2424
}
2525

26+
func TestDoubleTerms(t *testing.T) {
27+
testfile(t, "terms_terms_sum_agg")
28+
}
29+
2630
func TestTripleAgg(t *testing.T) {
2731
testfile(t, "triple_agg")
2832
}
@@ -50,41 +54,46 @@ func testfile(t *testing.T, filename string) {
5054
}
5155

5256
// SliceTableWriter
53-
st, err := tabify.Slice(src, tabify.KeyExcluder(excluder), tabify.KeyFormatter(formatter))
54-
assert.NoError(t, err, "slice table writer")
55-
stw := readJSON(t, "./tests/"+filename+"_slice.json")
56-
var arr []interface{}
57-
assert.NoError(t, json.Unmarshal([]byte(stw), &arr))
58-
assert.Equal(t, len(arr), len(st))
59-
for i, row := range arr {
60-
assert.Equalf(t, row, st[i], "at index %d", i)
61-
}
57+
t.Run("can write to slice "+filename, func(t *testing.T) {
58+
st, err := tabify.Slice(src, tabify.KeyExcluder(excluder), tabify.KeyFormatter(formatter))
59+
assert.NoError(t, err, "slice table writer")
60+
stw := readJSON(t, "./tests/"+filename+"_slice.json")
61+
var arr []interface{}
62+
assert.NoError(t, json.Unmarshal([]byte(stw), &arr))
63+
assert.Equal(t, len(arr), len(st))
64+
for i, row := range arr {
65+
assert.Equalf(t, row, st[i], "at index %d", i)
66+
}
6267

63-
//assert.JSONEq(t, stw, mst.String(), "slice table writer")
68+
//assert.JSONEq(t, stw, mst.String(), "slice table writer")
69+
})
6470

6571
// MapTableWriter
66-
// mt, err := tabify.Map(src, tabify.KeyExcluder(excluder), tabify.KeyFormatter(formatter))
67-
// assert.NoError(t, err, "map table writer")
68-
// mmt, err := json.Marshal(mt)
69-
// if err != nil {
70-
// t.Fatal("slice json marshal")
71-
// }
72-
// mtw := readJSON(t, "./tests/"+filename+"_expected.json")
73-
// assert.JSONEq(t, mtw, string(mmt[:]), "map table writer")
74-
75-
// // JSONTableWriter
76-
// jt, err := tabify.JSON(src, tabify.KeyExcluder(excluder), tabify.KeyFormatter(formatter))
77-
// assert.NoError(t, err, "json table writer")
78-
// jtw := readJSON(t, "./tests/"+filename+"_expected.json")
79-
// jo := jsonmap.New()
80-
// jo.Set("", jt)
81-
// assert.JSONEq(t, jtw, jo.Stringify(), "json table writer")
82-
72+
t.Run("can write to map "+filename, func(t *testing.T) {
73+
mt, err := tabify.Map(src, tabify.KeyExcluder(excluder), tabify.KeyFormatter(formatter))
74+
assert.NoError(t, err, "map table writer")
75+
mmt, err := json.Marshal(mt)
76+
if err != nil {
77+
t.Fatal("slice json marshal")
78+
}
79+
mtw := readJSON(t, "./tests/"+filename+"_expected.json")
80+
assert.JSONEq(t, mtw, string(mmt[:]), "map table writer")
81+
})
82+
83+
// JSONTableWriter
84+
t.Run("can write to json "+filename, func(t *testing.T) {
85+
jt, err := tabify.JSON(src, tabify.KeyExcluder(excluder), tabify.KeyFormatter(formatter))
86+
assert.NoError(t, err, "json table writer")
87+
jtw := readJSON(t, "./tests/"+filename+"_expected.json")
88+
jo := jsonmap.New()
89+
jo.Set("", jt)
90+
assert.JSONEq(t, jtw, jo.Stringify(), "json table writer")
91+
})
8392
}
8493

8594
// readJSON to read a json file and store to a jsonmap
8695
func readJSON(t *testing.T, filename string) string {
87-
data, err := ioutil.ReadFile(filename)
96+
data, err := os.ReadFile(filename)
8897
if err != nil {
8998
t.Fatalf("unable to read %s", filename)
9099
}
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
{
2+
"took": 4,
3+
"timed_out": false,
4+
"_shards": {
5+
"total": 5,
6+
"successful": 5,
7+
"skipped": 0,
8+
"failed": 0
9+
},
10+
"hits": {
11+
"total": 111396,
12+
"max_score": 0,
13+
"hits": []
14+
},
15+
"aggregations": {
16+
"2": {
17+
"doc_count_error_upper_bound": -1,
18+
"sum_other_doc_count": 105158,
19+
"buckets": [
20+
{
21+
"1": {
22+
"value": 94547
23+
},
24+
"3": {
25+
"doc_count_error_upper_bound": 0,
26+
"sum_other_doc_count": 0,
27+
"buckets": [
28+
{
29+
"1": {
30+
"value": 94547
31+
},
32+
"key": "Hamlet",
33+
"doc_count": 1582
34+
}
35+
]
36+
},
37+
"key": "HAMLET",
38+
"doc_count": 1582
39+
},
40+
{
41+
"1": {
42+
"value": 76899
43+
},
44+
"3": {
45+
"doc_count_error_upper_bound": 0,
46+
"sum_other_doc_count": 0,
47+
"buckets": [
48+
{
49+
"1": {
50+
"value": 76899
51+
},
52+
"key": "Othello",
53+
"doc_count": 1161
54+
}
55+
]
56+
},
57+
"key": "IAGO",
58+
"doc_count": 1161
59+
},
60+
{
61+
"1": {
62+
"value": 71866
63+
},
64+
"3": {
65+
"doc_count_error_upper_bound": 0,
66+
"sum_other_doc_count": 0,
67+
"buckets": [
68+
{
69+
"1": {
70+
"value": 71866
71+
},
72+
"key": "Loves Labours Lost",
73+
"doc_count": 647
74+
}
75+
]
76+
},
77+
"key": "BIRON",
78+
"doc_count": 647
79+
},
80+
{
81+
"1": {
82+
"value": 62945
83+
},
84+
"3": {
85+
"doc_count_error_upper_bound": 0,
86+
"sum_other_doc_count": 0,
87+
"buckets": [
88+
{
89+
"1": {
90+
"value": 62945
91+
},
92+
"key": "Othello",
93+
"doc_count": 928
94+
}
95+
]
96+
},
97+
"key": "OTHELLO",
98+
"doc_count": 928
99+
},
100+
{
101+
"1": {
102+
"value": 60243
103+
},
104+
"3": {
105+
"doc_count_error_upper_bound": 0,
106+
"sum_other_doc_count": 5,
107+
"buckets": [
108+
{
109+
"1": {
110+
"value": 28740
111+
},
112+
"key": "Richard III",
113+
"doc_count": 743
114+
},
115+
{
116+
"1": {
117+
"value": 9809
118+
},
119+
"key": "King Lear",
120+
"doc_count": 364
121+
},
122+
{
123+
"1": {
124+
"value": 9238
125+
},
126+
"key": "Henry VI Part 3",
127+
"doc_count": 266
128+
},
129+
{
130+
"1": {
131+
"value": 9156
132+
},
133+
"key": "Henry VI Part 2",
134+
"doc_count": 320
135+
},
136+
{
137+
"1": {
138+
"value": 3161
139+
},
140+
"key": "Henry VI Part 1",
141+
"doc_count": 222
142+
}
143+
]
144+
},
145+
"key": "GLOUCESTER",
146+
"doc_count": 1920
147+
}
148+
]
149+
}
150+
},
151+
"status": 200
152+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[
2+
{ "2": "HAMLET", "2#doc_count": 1582, "2#1": 94547, "2#3": "Hamlet", "2#3#doc_count": 1582, "2#3#1": 94547 },
3+
{ "2": "IAGO", "2#doc_count": 1161, "2#1": 76899, "2#3": "Othello", "2#3#doc_count": 1161, "2#3#1": 76899 },
4+
{ "2": "BIRON", "2#doc_count": 647, "2#1": 71866, "2#3": "Loves Labours Lost", "2#3#doc_count": 647, "2#3#1": 71866 },
5+
{ "2": "OTHELLO", "2#doc_count": 928, "2#1": 62945, "2#3": "Othello", "2#3#doc_count": 928, "2#3#1": 62945 },
6+
{ "2": "GLOUCESTER", "2#doc_count": 1920, "2#1": 60243, "2#3": "Richard III", "2#3#doc_count": 743, "2#3#1": 28740 },
7+
{ "2": "GLOUCESTER", "2#doc_count": 1920, "2#1": 60243, "2#3": "King Lear", "2#3#doc_count": 364, "2#3#1": 9809 },
8+
{ "2": "GLOUCESTER", "2#doc_count": 1920, "2#1": 60243, "2#3": "Henry VI Part 3", "2#3#doc_count": 266, "2#3#1": 9238 },
9+
{ "2": "GLOUCESTER", "2#doc_count": 1920, "2#1": 60243, "2#3": "Henry VI Part 2", "2#3#doc_count": 320, "2#3#1": 9156 },
10+
{ "2": "GLOUCESTER", "2#doc_count": 1920, "2#1": 60243, "2#3": "Henry VI Part 1", "2#3#doc_count": 222, "2#3#1": 3161 }
11+
]
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[
2+
["2", "2#doc_count", "2#1", "2#3", "2#3#doc_count", "2#3#1"],
3+
["HAMLET",1582,94547,"Hamlet",1582,94547 ],
4+
["IAGO",1161,76899,"Othello",1161,76899 ],
5+
["BIRON",647,71866,"Loves Labours Lost",647,71866 ],
6+
["OTHELLO",928,62945,"Othello",928,62945 ],
7+
["GLOUCESTER",1920,60243,"Richard III",743,28740 ],
8+
["GLOUCESTER",1920,60243,"King Lear",364,9809 ],
9+
["GLOUCESTER",1920,60243,"Henry VI Part 3",266,9238 ],
10+
["GLOUCESTER",1920,60243,"Henry VI Part 2",320,9156 ],
11+
["GLOUCESTER",1920,60243,"Henry VI Part 1",222,3161]
12+
]

0 commit comments

Comments
 (0)