|
1 | 1 | import { v4 as uuid } from 'uuid' |
2 | 2 |
|
| 3 | +import { flattenData } from '../data-lake/data-flattener' |
| 4 | + |
3 | 5 | /** |
4 | 6 | * A variable to be used on a Cockpit action |
5 | 7 | * @param { string } id - The id of the variable |
@@ -52,28 +54,47 @@ export const getDataLakeVariableData = (id: string): string | number | boolean | |
52 | 54 | return dataLakeVariableData[id] |
53 | 55 | } |
54 | 56 |
|
55 | | -export const setDataLakeVariableData = (id: string, data: object | string | number | boolean): void => { |
56 | | - const newData = data |
57 | | - if (data === null) { |
| 57 | +export const setDataLakeVariableData = ( |
| 58 | + id: string, |
| 59 | + data: object | string | number | boolean | Array<string | number> |
| 60 | +): void => { |
| 61 | + if (data === null) return |
| 62 | + |
| 63 | + // Handle already-flat primitive types first |
| 64 | + if (typeof data === 'string' || typeof data === 'number') { |
| 65 | + if (dataLakeVariableData[id] === undefined) { |
| 66 | + createDataLakeVariable(new DataLakeVariable(id, id, typeof data)) |
| 67 | + } |
| 68 | + dataLakeVariableData[id] = data |
| 69 | + notifyDataLakeVariableListeners(id) |
| 70 | + return |
| 71 | + } |
| 72 | + |
| 73 | + // Try to flatten complex data |
| 74 | + const flattenedData = flattenData(data, (value, index) => { |
| 75 | + setDataLakeVariableData(`${id}/${index}`, value) |
| 76 | + }) |
| 77 | + |
| 78 | + if (!flattenedData) return |
| 79 | + |
| 80 | + const { type, value } = flattenedData |
| 81 | + |
| 82 | + // Only proceed with string or number types |
| 83 | + if (type !== 'string' && type !== 'number') { |
| 84 | + console.debug(`attempting to create a variable with type ${type}. Skipping`) |
58 | 85 | return |
59 | 86 | } |
| 87 | + |
| 88 | + // Create variable if it doesn't exist |
60 | 89 | if (dataLakeVariableData[id] === undefined) { |
61 | | - console.trace(`Cockpit action variable with id '${id}' does not exist. Creating it.`) |
62 | | - const type_of_variable = typeof data |
63 | | - if (type_of_variable === 'object') { |
64 | | - // TODO: support strings |
65 | | - } |
66 | | - if (type_of_variable !== 'string' && type_of_variable !== 'number') { |
67 | | - console.debug(`attempting to create a variable with type ${type_of_variable}. Skipping`) |
68 | | - return |
69 | | - } |
70 | | - createDataLakeVariable(new DataLakeVariable(id, id, typeof data)) |
| 90 | + createDataLakeVariable(new DataLakeVariable(id, id, type)) |
71 | 91 | } |
72 | | - if (newData === undefined || typeof newData === 'object') { |
73 | | - return |
| 92 | + |
| 93 | + // Update the value and notify listeners |
| 94 | + if (typeof value === 'string' || typeof value === 'number') { |
| 95 | + dataLakeVariableData[id] = value |
| 96 | + notifyDataLakeVariableListeners(id) |
74 | 97 | } |
75 | | - dataLakeVariableData[id] = newData |
76 | | - notifyDataLakeVariableListeners(id) |
77 | 98 | } |
78 | 99 |
|
79 | 100 | export const deleteDataLakeVariable = (id: string): void => { |
|
0 commit comments