UNPKG

@cloudquery/plugin-sdk-javascript

Version:

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

68 lines 1.76 kB
import { Utf8 as ArrowString } from '@apache-arrow/esnext-esm'; import { FormatError } from '../errors/errors.js'; import { isInvalid, NULL_VALUE } from './util.js'; const validate = (value) => { try { JSON.parse(value); return true; } catch { return false; } }; class JSONType { _valid = false; _value = null; constructor(v) { this.value = v; return this; } get dataType() { return new ArrowString(); } get valid() { return this._valid; } get value() { if (!this._valid) { return null; } return this._value; } set value(value) { if (isInvalid(value)) { this._valid = false; return; } if (typeof value === 'string') { this._value = new TextEncoder().encode(value); this._valid = validate(value); return; } if (value instanceof Uint8Array) { this._value = value; this._valid = validate(new TextDecoder().decode(value)); return; } if (value instanceof JSONType) { this._value = value.value; this._valid = value.valid; return; } try { this._value = new TextEncoder().encode(JSON.stringify(value)); this._valid = true; } catch (error) { throw new FormatError(`Unable to set JSON from value`, { cause: error, props: { value } }); } } toString() { if (this._valid) { return new TextDecoder().decode(this._value); } return NULL_VALUE; } } export { JSONType as JSON }; //# sourceMappingURL=json.js.map