Skip to content

Commit

Permalink
handle nested arrays in toApiFieldNames
Browse files Browse the repository at this point in the history
  • Loading branch information
tsullivan committed Jul 23, 2018
1 parent c4e1242 commit f2474ee
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
40 changes: 38 additions & 2 deletions src/server/usage/classes/__tests__/collector_set.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ describe('CollectorSet', () => {
let server;
let init;
let fetch;

beforeEach(() => {
server = { log: sinon.spy() };
init = noop;
Expand Down Expand Up @@ -72,6 +73,12 @@ describe('CollectorSet', () => {
});

describe('toApiFieldNames', () => {
let collectorSet;

beforeEach(() => {
collectorSet = new CollectorSet();
});

it('should snake_case and convert field names to api standards', () => {
const apiData = {
os: {
Expand All @@ -94,8 +101,7 @@ describe('CollectorSet', () => {
]
};

const collectors = new CollectorSet();
const result = collectors.toApiFieldNames(apiData);
const result = collectorSet.toApiFieldNames(apiData);
expect(result).to.eql({
os: {
load: { '15m': 2.3525390625, '1m': 2.22412109375, '5m': 2.4462890625 },
Expand All @@ -105,5 +111,35 @@ describe('CollectorSet', () => {
days_of_the_week: ['monday', 'tuesday', 'wednesday'],
});
});

it('should correct object key fields nested in arrays', () => {
const apiData = {
daysOfTheWeek: [
{
dayName: 'monday',
dayIndex: 1
},
{
dayName: 'tuesday',
dayIndex: 2
},
{
dayName: 'wednesday',
dayIndex: 3
}
]
};

const result = collectorSet.toApiFieldNames(apiData);
expect(result).to.eql({
days_of_the_week: [
{ day_index: 1, day_name: 'monday' },
{ day_index: 2, day_name: 'tuesday' },
{ day_index: 3, day_name: 'wednesday' },
],
});
});
});
});


7 changes: 3 additions & 4 deletions src/server/usage/classes/collector_set.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,12 @@ export class CollectorSet {
}
};

if (!Array.isArray(apiData)) {
if (Array.isArray(apiData)) {
return apiData.map(getValueOrRecurse);
} else {
return Object.keys(apiData).reduce((accum, field) => {
const value = apiData[field];
let newName = field;

newName = snakeCase(newName);
newName = newName.replace(/^(1|5|15)_m/, '$1m'); // os.load.15m, os.load.5m, os.load.1m
newName = newName.replace('_in_bytes', '_bytes');
Expand All @@ -132,8 +133,6 @@ export class CollectorSet {
[newName]: getValueOrRecurse(value),
};
}, {});
} else {
return apiData;
}
}
}

0 comments on commit f2474ee

Please sign in to comment.