"use strict"; const matchValue = require("match-value"); const unreachable = require("@joepie91/unreachable")("zapdb-kv"); const stringCoder = require("./string"); const bytesCoder = require("./bytes"); const booleanCoder = require("./boolean"); const integerCoder = require("./integer"); const decimalCoder = require("./decimal"); const createDateCoder = require("./date"); const dateCoder = createDateCoder(false); const tzDateCoder = createDateCoder(true); function $unimplemented() { throw unreachable("Not implemented yet"); } function getCoderForType(fieldType, withTimezone) { return matchValue(fieldType, { string: stringCoder, bytes: bytesCoder, boolean: booleanCoder, integer: integerCoder, decimal: decimalCoder, date: (withTimezone) ? tzDateCoder : dateCoder, array: $unimplemented, object: $unimplemented, float: $unimplemented, duration: $unimplemented, _: () => unreachable(`Unrecognized field type '${fieldType}'`) }); } module.exports = { encode: function (fieldType, value, options = {}) { let { withTimezone, asIndexKey, ... coderOptions } = options; // TODO: Handle key length constraints; this needs to be handled at a high level since *any* type should be assumed to potentially create a too-long key // TODO: Figure out how to expose the auxiliaryBlob store to the decoders; maybe we shouldn't, and instead the decoder should just return a key which is handled higher-up? if (value == null) { // TODO: Test that this is handled correctly upstream return null; } else { let coder = getCoderForType(fieldType, withTimezone); return coder.encode(value, asIndexKey, coderOptions); } }, decode: function (fieldType, buffer, offset, options = {}) { let { withTimezone, ... coderOptions } = options; let coder = getCoderForType(fieldType, withTimezone); return coder.decode(buffer, offset, coderOptions); } };