mirror of
https://github.com/grafana/grafana.git
synced 2025-07-31 16:42:36 +08:00
ConvertFieldType: Update "join with" to work on array of strings (#105074)
* fix: update join with to work on array of strings * chore: simplify * chore: cleanup * chore: add tests! * fix: more robust check
This commit is contained in:
@ -359,7 +359,7 @@ describe('field convert types transformer', () => {
|
||||
]);
|
||||
});
|
||||
|
||||
it('will support custom join separators', () => {
|
||||
it('will support custom join separators for field type other', () => {
|
||||
const options = {
|
||||
conversions: [{ targetField: 'vals', destinationType: FieldType.string, joinWith: '|' }],
|
||||
};
|
||||
@ -391,6 +391,38 @@ describe('field convert types transformer', () => {
|
||||
]);
|
||||
});
|
||||
|
||||
it('will support custom join separators for field type string', () => {
|
||||
const options = {
|
||||
conversions: [{ targetField: 'string_arrays', destinationType: FieldType.string, joinWith: '&' }],
|
||||
};
|
||||
|
||||
const stringArrayValues = toDataFrame({
|
||||
fields: [
|
||||
{
|
||||
name: 'string_arrays',
|
||||
type: FieldType.string,
|
||||
values: [
|
||||
['a', 'b', 'c'],
|
||||
['d', 'e', 'f'],
|
||||
],
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
const stringified = convertFieldTypes(options, [stringArrayValues]);
|
||||
expect(
|
||||
stringified[0].fields.map(({ type, values }) => ({
|
||||
type,
|
||||
values,
|
||||
}))
|
||||
).toEqual([
|
||||
{
|
||||
type: FieldType.string,
|
||||
values: ['a&b&c', 'd&e&f'],
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it('will convert time fields to strings', () => {
|
||||
const options = {
|
||||
conversions: [{ targetField: 'time', destinationType: FieldType.string, dateFormat: 'YYYY-MM' }],
|
||||
|
@ -210,12 +210,15 @@ export function fieldToStringField(
|
||||
values = values.map((v) => dateTimeParse(v, parseOptions).format(dateFormat));
|
||||
break;
|
||||
|
||||
// Handle both "string" and "other" types to ensure compatibility across Grafana versions (10 & 11)
|
||||
// In some cases fields are classified as 'other' in Grafana 10 but as 'string' in Grafana 11
|
||||
case FieldType.string:
|
||||
case FieldType.other:
|
||||
values = values.map((v) => {
|
||||
if (joinWith?.length && Array.isArray(v)) {
|
||||
return v.join(joinWith);
|
||||
}
|
||||
return JSON.stringify(v); // will quote strings and avoid "object"
|
||||
return field.type === FieldType.other ? JSON.stringify(v) : `${v}`;
|
||||
});
|
||||
break;
|
||||
|
||||
|
Reference in New Issue
Block a user