RowsToFields: Fix issue with field names that are numbers (#40580)

* RowsToFields: Fix issue with field names that are numbers

* Only add the index accessor if field name does not conflict with it

* fix lint

Co-authored-by: Ryan McKinley <ryantxu@gmail.com>
This commit is contained in:
Torkel Ödegaard
2021-10-19 07:27:24 +02:00
committed by GitHub
parent 7e59b92c8c
commit 25813db334
4 changed files with 44 additions and 5 deletions

View File

@@ -81,4 +81,19 @@ describe('dataFrameView', () => {
value: 1,
});
});
it('Can handle fields with number name', () => {
const view = new DataFrameView<MySpecialObject>(
new MutableDataFrame({
fields: [
{ name: '1', type: FieldType.string, values: ['a'] },
{ name: '2', type: FieldType.string, values: ['b'] },
],
})
);
const obj = view.get(0) as any;
expect(obj['1']).toEqual('a');
expect(obj['2']).toEqual('b');
});
});

View File

@@ -32,10 +32,12 @@ export class DataFrameView<T = any> extends FunctionalVector<T> {
});
}
Object.defineProperty(obj, i, {
enumerable: false, // Don't enumerate array index
get: getter,
});
if (!(obj as any).hasOwnProperty(i.toString())) {
Object.defineProperty(obj, i, {
enumerable: false, // Don't enumerate array index
get: getter,
});
}
}
this.obj = obj;

View File

@@ -151,4 +151,26 @@ describe('Rows to fields', () => {
expect(result.fields[0].name).toEqual('Stockholm');
expect(result.fields[0].values.get(0)).toEqual(20);
});
it('Can handle number fields as name field', () => {
const input = toDataFrame({
fields: [
{ name: 'SensorID', type: FieldType.number, values: [10, 20, 30] },
{ name: 'Value', type: FieldType.number, values: [1, 2, 3] },
],
});
const result = rowsToFields(
{
mappings: [
{ fieldName: 'SensorID', handlerKey: 'field.name' },
{ fieldName: 'Value', handlerKey: 'field.value' },
],
},
input
);
expect(result.fields[0].name).toEqual('10');
expect(result.fields[0].values.get(0)).toEqual(1);
});
});

View File

@@ -57,7 +57,7 @@ export function rowsToFields(options: RowToFieldsTransformOptions, data: DataFra
const labels = getLabelsFromRow(data, index, mappingResult);
const field: Field = {
name: name,
name: `${name}`,
type: valueField.type,
values: new ArrayVector([value]),
config: config,