From a31186ee56639340a7a7a8c9f55920e02cbbdf63 Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Tue, 10 Oct 2023 15:42:09 +0100 Subject: [PATCH] Improve custom data fetch parsing to better support csv-like strings --- .../frontend-core/src/fetch/CustomFetch.js | 30 +++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/packages/frontend-core/src/fetch/CustomFetch.js b/packages/frontend-core/src/fetch/CustomFetch.js index 09504b08f8..fc62d790e2 100644 --- a/packages/frontend-core/src/fetch/CustomFetch.js +++ b/packages/frontend-core/src/fetch/CustomFetch.js @@ -40,6 +40,13 @@ export default class CustomFetch extends DataFetch { } catch (error) { // Ignore } + + // Try splitting by newlines first + if (data.includes("\n")) { + return data.split("\n").map(x => x.trim()) + } + + // Split by commas next return data.split(",").map(x => x.trim()) } @@ -57,7 +64,26 @@ export default class CustomFetch extends DataFetch { data = data.filter(x => x != null && x !== "" && !Array.isArray(x)) // Ensure all values are packed into objects - return data.map(x => (typeof x === "object" ? x : { value: x })) + return data.map(value => { + if (typeof value === "object") { + return value + } + + // Try parsing strings + if (typeof value === "string") { + const split = value.split(",").map(x => x.trim()) + let obj = {} + for (let i = 0; i < split.length; i++) { + const suffix = i === 0 ? "" : ` ${i + 1}` + const key = `Value${suffix}` + obj[key] = split[i] + } + return obj + } + + // For anything else, wrap in an object + return { Value: value } + }) } // Extracts and parses the custom data from the datasource definition @@ -87,7 +113,7 @@ export default class CustomFetch extends DataFetch { if (type === "string") { const uniqueValues = [...new Set(data.map(x => x[key]))] const uniqueness = uniqueValues.length / data.length - if (uniqueness <= 0.8) { + if (uniqueness <= 0.8 && uniqueValues.length > 1) { type = "options" constraints.inclusion = uniqueValues }