@brimdata/zealot
Version:
The Javascript Client for Zed Lakes
69 lines (68 loc) • 2.38 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "DecodeStream", {
enumerable: true,
get: ()=>DecodeStream
});
const _typeField = require("./types/type-field");
const _getPrimitiveType = require("./utils/get-primitive-type");
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 DecodeStream {
decode(object) {
const type = this.decodeType(object.type);
return type.create(object.value, this);
}
decodeType(obj) {
const type = this.buildType(obj);
if ("id" in obj && obj.kind !== "ref") {
this.typedefs[obj.id] = type;
}
return type;
}
buildType(obj) {
switch(obj.kind){
case "primitive":
return (0, _getPrimitiveType.getPrimitiveType)(obj.name);
case "ref":
return this.typedefs[obj.id];
case "named":
return this.context.lookupTypeAlias(obj.name, this.decodeType(obj.type));
case "array":
return this.context.lookupTypeArray(this.decodeType(obj.type));
case "set":
return this.context.lookupTypeSet(this.decodeType(obj.type));
case "error":
return this.context.lookupErrorType(this.decodeType(obj.type));
case "union":
return this.context.lookupTypeUnion(obj.types.map((t)=>this.decodeType(t)));
case "map":
return this.context.lookupTypeMap(this.decodeType(obj.key_type), this.decodeType(obj.val_type));
case "record":
return this.context.lookupTypeRecord(obj.fields === null ? null : obj.fields.map(({ name , type })=>{
return new _typeField.TypeField(name, this.decodeType(type));
}));
default:
throw `Implement decoding: ${obj.kind}`;
}
}
constructor(context){
_defineProperty(this, "context", void 0);
_defineProperty(this, "typedefs", void 0);
this.context = context;
this.typedefs = {};
}
}