mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
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:
@@ -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');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user