Skip to content

Commit a8dda02

Browse files
Print all columns in CSV output for heterogeneous collection (#164)
* csv changes * simpler code * added test case
1 parent a2bf217 commit a8dda02

File tree

5 files changed

+92
-5
lines changed

5 files changed

+92
-5
lines changed

package-lock.json

Lines changed: 1 addition & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/box-command.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -860,7 +860,12 @@ class BoxCommand extends Command {
860860
objectArray = [objectArray];
861861
DEBUG.output('Creating tabular output from single object');
862862
}
863-
let keyPaths = this.getNestedKeys(objectArray[0]);
863+
864+
let keyPaths = [];
865+
for (let object of objectArray) {
866+
keyPaths = _.union(keyPaths, this.getNestedKeys(object));
867+
}
868+
864869
DEBUG.output('Found %d keys for tabular output', keyPaths.length);
865870
formattedData.push(keyPaths);
866871
for (let object of objectArray) {

test/commands/bulk.test.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -808,9 +808,11 @@ describe('Bulk', () => {
808808
let inputFilePath = path.join(__dirname, '../fixtures/bulk/bulk_files_tasks_list_input.json'),
809809
fixture = getFixture('files/get_files_id_tasks_page_1'),
810810
fixture2 = getFixture('files/get_files_id_tasks_page_2'),
811+
fixture3 = getFixture('folders/get_folders_id_items'),
811812
jsonCollectionOutput = getFixture('output/bulk_collection_output_json.txt'),
812813
tableCollectionOutput = getFixture('output/bulk_collection_output_table.txt'),
813-
csvCollectionOutput = getFixture('output/bulk_collection_output_csv.txt');
814+
csvCollectionOutput = getFixture('output/bulk_collection_output_csv.txt'),
815+
csvItemsOutput = getFixture('output/bulk_items_output_csv.txt');
814816

815817
test
816818
.nock(TEST_API_ROOT, api => api
@@ -919,6 +921,24 @@ describe('Bulk', () => {
919921
assert.equal(ctx.stdout, csvCollectionOutput);
920922
});
921923

924+
test
925+
.nock(TEST_API_ROOT, api => api
926+
.get('/2.0/folders/0/items')
927+
.query({ usemarker: true })
928+
.reply(200, fixture3)
929+
)
930+
.stdout()
931+
.stderr({print: true})
932+
.command([
933+
'folders:items',
934+
'0',
935+
'--csv',
936+
'--token=test'
937+
])
938+
.it('should output flattened CSV with union of all fields present in each item when each command run returns an array (CSV Output)', ctx => {
939+
assert.equal(ctx.stdout, csvItemsOutput);
940+
});
941+
922942
});
923943

924944
});
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
{
2+
"total_count": 5,
3+
"entries": [
4+
{
5+
"type": "folder",
6+
"id": "1234567",
7+
"sequence_id": "10",
8+
"etag": "10",
9+
"name": "Box"
10+
},
11+
{
12+
"type": "folder",
13+
"id": "22585432",
14+
"sequence_id": "9",
15+
"etag": "9",
16+
"name": "Human Resources"
17+
},
18+
{
19+
"type": "folder",
20+
"id": "24432981",
21+
"sequence_id": "8",
22+
"etag": "8",
23+
"name": "Engineering"
24+
},
25+
{
26+
"type": "file",
27+
"id": "124000",
28+
"file_version": {
29+
"type": "file_version",
30+
"id": "431000",
31+
"sha1": "b7e432rew9bc2850a5780a86a25a8574d1b473f"
32+
},
33+
"sequence_id": "79",
34+
"etag": "79",
35+
"sha1": "b7e9fsdfe329bc2850a5780a86a25a8574d1b473f",
36+
"name": "Draymond.boxnote"
37+
},
38+
{
39+
"type": "web_link",
40+
"id": "13413421543",
41+
"sequence_id": "77",
42+
"etag": "77",
43+
"name": "77 Crunchy Dragon Rolls",
44+
"url": "https://cloud.app.box.com/s/432fesr32fsdfw"
45+
}
46+
],
47+
"next_marker": null,
48+
"order": [
49+
{
50+
"by": "type",
51+
"direction": "ASC"
52+
},
53+
{
54+
"by": "name",
55+
"direction": "ASC"
56+
}
57+
]
58+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
type,id,sequence_id,etag,name,file_version.type,file_version.id,file_version.sha1,sha1,url
2+
folder,1234567,10,10,Box,,,,,
3+
folder,22585432,9,9,Human Resources,,,,,
4+
folder,24432981,8,8,Engineering,,,,,
5+
file,124000,79,79,Draymond.boxnote,file_version,431000,b7e432rew9bc2850a5780a86a25a8574d1b473f,b7e9fsdfe329bc2850a5780a86a25a8574d1b473f,
6+
web_link,13413421543,77,77,77 Crunchy Dragon Rolls,,,,,https://cloud.app.box.com/s/432fesr32fsdfw

0 commit comments

Comments
 (0)