Skip to content

Commit

Permalink
Merge pull request ceph#24632 from Devp00l/wip-issue-36468
Browse files Browse the repository at this point in the history
mgr/dashboard: Handle class objects as regular objects in KV-table

Reviewed-by: Ricardo Marques <[email protected]>
Reviewed-by: Tiago Melo <[email protected]>
  • Loading branch information
LenzGr authored Oct 23, 2018
2 parents 7ba9455 + c37023c commit 549e002
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,46 @@ describe('TableKeyValueComponent', () => {
component.data = [['someKey', 0, 3]];
expect(() => component.ngOnInit()).toThrowError('Wrong array format: [string, any][]');
component.data = [{ somekey: 939, somethingElse: 'test' }];
expect(() => component.ngOnInit()).toThrowError(
'Wrong object array format: {key: string, value: any}[]'
);
});

describe('Class objects equal plain objects', () => {
class Example {
sth = 'something';
deep?: Example;
constructor(deep: boolean) {
if (deep) {
this.deep = new Example(false);
}
}
}

const classExample = new Example(true);
const objectExample = {
sth: 'something',
deep: {
sth: 'something'
}
};

const getTableData = (data) => {
component.data = data;
expect(() => component.ngOnInit()).not.toThrow();
return component.tableData;
};

const doesClassEqualsObject = (classData, objectData, dataLength) => {
const classTableData = getTableData(classData);
expect(classTableData).toEqual(getTableData(objectData));
expect(classTableData.length).toBe(dataLength);
};

it('should convert class objects the same way as plain objects', () => {
doesClassEqualsObject(classExample, objectExample, 1);
doesClassEqualsObject([classExample], [objectExample], 1);
component.renderObjects = true;
doesClassEqualsObject(classExample, objectExample, 2);
doesClassEqualsObject([classExample], [objectExample], 2);
});
});

it('tests _makePairs', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export class TableKeyValueComponent implements OnInit, OnChanges {
return; // Wait for data
} else if (_.isArray(data)) {
temp = this._makePairsFromArray(data);
} else if (_.isPlainObject(data)) {
} else if (_.isObject(data)) {
temp = this._makePairsFromObject(data);
} else {
throw new Error('Wrong data format');
Expand All @@ -109,22 +109,23 @@ export class TableKeyValueComponent implements OnInit, OnChanges {
_makePairsFromArray(data: any[]) {
let temp = [];
const first = data[0];
if (_.isPlainObject(first)) {
if (_.isArray(first)) {
if (first.length === 2) {
temp = data.map((a) => ({
key: a[0],
value: a[1]
}));
} else {
throw new Error('Wrong array format: [string, any][]');
}
} else if (_.isObject(first)) {
if (_.has(first, 'key') && _.has(first, 'value')) {
temp = [...data];
} else {
throw new Error('Wrong object array format: {key: string, value: any}[]');
}
} else {
if (_.isArray(first)) {
if (first.length === 2) {
temp = data.map((a) => ({
key: a[0],
value: a[1]
}));
} else {
throw new Error('Wrong array format: [string, any][]');
}
temp = data.reduce(
(previous: any[], item) => previous.concat(this._makePairsFromObject(item)),
temp
);
}
}
return temp;
Expand All @@ -139,7 +140,7 @@ export class TableKeyValueComponent implements OnInit, OnChanges {

_insertFlattenObjects(temp: any[]) {
temp.forEach((v, i) => {
if (_.isPlainObject(v.value)) {
if (_.isObject(v.value)) {
temp.splice(i, 1);
this._makePairs(v.value).forEach((item) => {
if (this.appendParentKey) {
Expand All @@ -155,10 +156,8 @@ export class TableKeyValueComponent implements OnInit, OnChanges {

_convertValue(v: any) {
if (_.isArray(v.value)) {
v.value = v.value
.map((item) => (_.isPlainObject(item) ? JSON.stringify(item) : item))
.join(', ');
} else if (_.isPlainObject(v.value) && !this.renderObjects) {
v.value = v.value.map((item) => (_.isObject(item) ? JSON.stringify(item) : item)).join(', ');
} else if (_.isObject(v.value) && !this.renderObjects) {
return;
}
return v;
Expand Down

0 comments on commit 549e002

Please sign in to comment.