UNPKG

@brimdata/zealot

Version:

The Javascript Client for Zed Lakes

132 lines (131 loc) 4.26 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); function _export(target, all) { for(var name in all)Object.defineProperty(target, name, { enumerable: true, get: all[name] }); } _export(exports, { ZedContext: ()=>ZedContext, DefaultContext: ()=>DefaultContext }); const _decodeStream = require("./decode-stream"); const _encodeStream = require("./encode-stream"); const _typeAlias = require("./types/type-alias"); const _typeArray = require("./types/type-array"); const _typeError = require("./types/type-error"); const _typeMap = require("./types/type-map"); const _typeRecord = require("./types/type-record"); const _typeSet = require("./types/type-set"); const _typeUnion = require("./types/type-union"); function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } class ZedContext { decode(objects, stream = new _decodeStream.DecodeStream(this)) { return objects.map((o)=>this.decodeOne(o, stream)); } decodeOne(object, stream = new _decodeStream.DecodeStream(this)) { return stream.decode(object); } encode(values) { const stream = new _encodeStream.EncodeStream(); return values.map((v)=>this.encodeOne(v, stream)); } encodeOne(value, stream = new _encodeStream.EncodeStream()) { return stream.encode(value); } decodeField(obj) { // Grab the first field and return it const transport = this.decodeOne(obj.record); return transport.getField(obj.path); } encodeField(field) { // Wrap a field in a record to encode const root = field.rootRecord; if (!root) throw new Error("Unable to encode field, no root record"); return { record: this.encodeOne(root), path: field.path }; } lookupErrorType(type) { const key = _typeError.TypeError.stringify(type); if (key in this.typeByShape) { return this.typeByShape[key]; } else { return this.alloc(key, new _typeError.TypeError(type)); } } lookupTypeRecord(fields) { const key = _typeRecord.TypeRecord.stringify(fields); if (key in this.typeByShape) { const record = this.typeByShape[key]; record.fields = fields; return record; } else { return this.alloc(key, new _typeRecord.TypeRecord(fields)); } } lookupTypeArray(type) { const key = _typeArray.TypeArray.stringify(type); if (key in this.typeByShape) { return this.typeByShape[key]; } else { return this.alloc(key, new _typeArray.TypeArray(type)); } } lookupTypeSet(type) { const key = _typeSet.TypeSet.stringify(type); if (key in this.typeByShape) { return this.typeByShape[key]; } else { return this.alloc(key, new _typeSet.TypeSet(type)); } } lookupTypeUnion(types) { const key = _typeUnion.TypeUnion.stringify(types); if (key in this.typeByShape) { return this.typeByShape[key]; } else { return this.alloc(key, new _typeUnion.TypeUnion(types)); } } lookupTypeMap(keyType, valType) { const key = _typeMap.TypeMap.stringify(keyType, valType); if (key in this.typeByShape) { return this.typeByShape[key]; } else { return this.alloc(key, new _typeMap.TypeMap(keyType, valType)); } } lookupTypeAlias(name, type) { const key = _typeAlias.TypeAlias.stringify(name, type); if (key in this.typeByShape) { return this.typeByShape[key]; } else { return this.alloc(key, new _typeAlias.TypeAlias(name, type)); } } alloc(key, type) { this.typeByShape[key] = type; return type; } constructor(){ _defineProperty(this, "typeByShape", {}); } } const DefaultContext = new ZedContext();