grafana-data: Fix error when field values are null (#106375)

* grafana-data: Fix error when field values are null

* Slightly terser

* update frame test to have falsy value that should be kept
remove null string output from null value test

* handle falsy frame values as non-null

---------

Co-authored-by: samsch <git@samsch.org>
This commit is contained in:
kay delaney
2025-06-06 14:41:09 +01:00
committed by GitHub
co-authored by samsch
parent f81031f945
commit 1d69c8558d
2 changed files with 32 additions and 10 deletions
+23 -3
View File
@@ -164,11 +164,11 @@ describe('DataFrame to CSV', () => {
it('should handle field type frame', () => {
const dataFrame = new MutableDataFrame({
fields: [
{ name: 'Time', values: [1589455688623] },
{ name: 'Time', values: [1589455688623, 1589455692345] },
{
name: 'Value',
type: FieldType.frame,
values: [{ value: '1234' }],
values: [{ value: '1234' }, { value: '0' }],
},
],
});
@@ -176,7 +176,27 @@ describe('DataFrame to CSV', () => {
const csv = toCSV([dataFrame]);
expect(csv).toMatchInlineSnapshot(`
""Time","Value"
1589455688623,1234"
1589455688623,1234
1589455692345,0"
`);
});
it('should handle null values', () => {
const dataFrame = new MutableDataFrame({
fields: [
{ name: 'Time', values: [1589455688623] },
{
name: 'Value',
type: FieldType.other,
values: [null],
},
],
});
const csv = toCSV([dataFrame]);
expect(csv).toMatchInlineSnapshot(`
""Time","Value"
1589455688623,"
`);
});
});
+9 -7
View File
@@ -306,21 +306,23 @@ export function toCSV(data: DataFrame[], config?: CSVConfig): string {
for (let i = 0; i < length; i++) {
for (let j = 0; j < fields.length; j++) {
if (j > 0) {
csv = csv + config.delimiter;
csv += config.delimiter;
}
let v = fields[j].values[i];
// For FieldType frame, use value if it exists to prevent exporting [object object]
if (fields[j].type === FieldType.frame && fields[j].values[i].value) {
v = fields[j].values[i].value;
}
if (v !== null) {
csv = csv + writers[j](v);
// For FieldType frame, use value if it exists to prevent exporting [object object]
if (fields[j].type === FieldType.frame && 'value' in v) {
v = v.value;
}
csv += writers[j](v);
}
}
if (i !== length - 1) {
csv = csv + config.newline;
csv += config.newline;
}
}
}