UNPKG

@cloudquery/plugin-sdk-javascript

Version:

This is the high-level package to use for developing CloudQuery plugins in JavaScript

82 lines 3.04 kB
import { List as ArrowList } from '@apache-arrow/esnext-esm'; import { FormatError } from '../errors/errors.js'; import { isInvalid, NULL_VALUE } from './util.js'; export class List { _childScalarInstance; _valid = false; _value = null; constructor(childScalarInstance, initialValue) { this._childScalarInstance = childScalarInstance; if (!isInvalid(initialValue)) { this._value = initialValue.map((value) => { const instance = Object.create(this._childScalarInstance); instance.value = value; return instance; }); } } get dataType() { return new ArrowList(this._childScalarInstance.dataType.ArrayType); } set value(inputValue) { if (isInvalid(inputValue)) { this._valid = false; this._value = []; return; } const inputArray = Array.isArray(inputValue) ? inputValue : [inputValue]; const temporaryVector = []; if (inputArray.length > 0) { this._childScalarInstance.value = inputArray[0]; const firstItemType = Object.getPrototypeOf(this._childScalarInstance).constructor; for (const item of inputArray) { try { this._childScalarInstance.value = item; } catch { throw new FormatError(`Type mismatch: All items should be of the same type as the first item. Expected type ${firstItemType.name}`, { props: { value: inputValue } }); } // Here, instead of creating a new scalar, we clone the existing instance with the current value const scalarClone = Object.create(this._childScalarInstance); scalarClone.value = item; temporaryVector.push(scalarClone); } this._value = temporaryVector; this._valid = true; // List becomes valid if we successfully process values return; } this._valid = true; // An empty list is valid } get valid() { return this._valid; } get value() { if (!this._valid) { return null; } return this._value; } toString() { if (!this._valid) { return NULL_VALUE; } return `[${this._value.map((v) => v.toString()).join(', ')}]`; } get length() { if (!this._valid) { return 0; } return this._value.length; } // If you need an equality method, you can add an equals method similar to the Python __eq__ equals(other) { if (!other) return false; if (this.constructor !== other.constructor) return false; if (this._valid !== other.valid) return false; return JSON.stringify(this._value) === JSON.stringify(other.value); // crude equality check for objects, might need refinement. } } //# sourceMappingURL=list.js.map