Skip to content

Commit

Permalink
Simplify namedtuples/objects to use regular vanilla {}s. (#310)
Browse files Browse the repository at this point in the history
While there drop the custom inspect, the introspection API (we can
introspect codecs), and kill implicit object IDs.
  • Loading branch information
1st1 committed Mar 25, 2022
1 parent 401cb05 commit e3676c4
Show file tree
Hide file tree
Showing 15 changed files with 52 additions and 567 deletions.
4 changes: 3 additions & 1 deletion qb/test/params.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,5 +250,7 @@ test("all param types", async () => {
tuple: args,
});

expect([...(complexResult.tuple as any)]).toEqual(Object.values(args));
expect(Object.values(complexResult.tuple as any)).toEqual(
Object.values(args)
);
});
9 changes: 3 additions & 6 deletions src/codecs/namedtuple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@
import {ICodec, Codec, uuid, IArgsCodec, CodecKind} from "./ifaces";
import {ReadBuffer, WriteBuffer} from "../primitives/buffer";
import {EmptyTupleCodec} from "./tuple";
import {generateType, NamedTupleConstructor} from "../datatypes/namedtuple";

export class NamedTupleCodec extends Codec implements ICodec, IArgsCodec {
private subCodecs: ICodec[];
private tupleCls: NamedTupleConstructor;
private names: string[];
private namesSet: Set<string>;

Expand All @@ -32,7 +30,6 @@ export class NamedTupleCodec extends Codec implements ICodec, IArgsCodec {
this.subCodecs = codecs;
this.names = names;
this.namesSet = new Set(names);
this.tupleCls = generateType(names);
}

encode(_buf: WriteBuffer, _object: any): void {
Expand Down Expand Up @@ -88,7 +85,6 @@ export class NamedTupleCodec extends Codec implements ICodec, IArgsCodec {
decode(buf: ReadBuffer): any {
const els = buf.readUInt32();
const subCodecs = this.subCodecs;
const cls = this.tupleCls;
if (els !== subCodecs.length) {
throw new Error(
`cannot decode NamedTuple: expected ` +
Expand All @@ -97,7 +93,8 @@ export class NamedTupleCodec extends Codec implements ICodec, IArgsCodec {
}

const elemBuf = ReadBuffer.alloc();
const result = new cls(els);
const names = this.names;
const result: any = {};
for (let i = 0; i < els; i++) {
buf.discard(4); // reserved
const elemLen = buf.readInt32();
Expand All @@ -107,7 +104,7 @@ export class NamedTupleCodec extends Codec implements ICodec, IArgsCodec {
val = subCodecs[i].decode(elemBuf);
elemBuf.finish();
}
result[i] = val;
result[names[i]] = val;
}

return result;
Expand Down
12 changes: 3 additions & 9 deletions src/codecs/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,14 @@
import {ICodec, Codec, uuid, CodecKind} from "./ifaces";
import {ReadBuffer, WriteBuffer} from "../primitives/buffer";
import {ONE, AT_LEAST_ONE} from "./consts";
import {
generateType,
ObjectConstructor,
EDGE_POINTER_IS_LINKPROP,
} from "../datatypes/object";

const EDGE_POINTER_IS_LINKPROP = 1 << 1;

export class ObjectCodec extends Codec implements ICodec {
private codecs: ICodec[];
private names: string[];
private namesSet: Set<string>;
private cardinalities: number[];
private objectType: ObjectConstructor;

constructor(
tid: uuid,
Expand All @@ -54,7 +50,6 @@ export class ObjectCodec extends Codec implements ICodec {
this.names = newNames;
this.namesSet = new Set(newNames);
this.cardinalities = cards;
this.objectType = generateType(newNames, flags);
}

encode(_buf: WriteBuffer, _object: any): void {
Expand Down Expand Up @@ -161,7 +156,6 @@ export class ObjectCodec extends Codec implements ICodec {
decode(buf: ReadBuffer): any {
const codecs = this.codecs;
const names = this.names;
const objType = this.objectType;

const els = buf.readUInt32();
if (els !== codecs.length) {
Expand All @@ -171,7 +165,7 @@ export class ObjectCodec extends Codec implements ICodec {
}

const elemBuf = ReadBuffer.alloc();
const result: any = new objType();
const result: any = {};
for (let i = 0; i < els; i++) {
buf.discard(4); // reserved
const elemLen = buf.readInt32();
Expand Down
9 changes: 4 additions & 5 deletions src/codecs/set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import {ICodec, Codec, uuid, CodecKind} from "./ifaces";
import {WriteBuffer, ReadBuffer} from "../primitives/buffer";
import {ArrayCodec} from "./array";
import {Set} from "../datatypes/set";

export class SetCodec extends Codec implements ICodec {
private subCodec: ICodec;
Expand Down Expand Up @@ -48,7 +47,7 @@ export class SetCodec extends Codec implements ICodec {
buf.discard(4); // reserved

if (ndims === 0) {
return new Set(0);
return [];
}
if (ndims !== 1) {
throw new Error(`expected 1-dimensional array of records of arrays`);
Expand All @@ -58,7 +57,7 @@ export class SetCodec extends Codec implements ICodec {

buf.discard(4); // ignore the lower bound info

const result = new Set(len);
const result = new Array(len);
const elemBuf = ReadBuffer.alloc();
const subCodec = this.subCodec;

Expand Down Expand Up @@ -95,7 +94,7 @@ export class SetCodec extends Codec implements ICodec {
buf.discard(4); // reserved

if (ndims === 0) {
return new Set(0);
return [];
}
if (ndims !== 1) {
throw new Error(`invalid set dimensinality: ${ndims}`);
Expand All @@ -105,7 +104,7 @@ export class SetCodec extends Codec implements ICodec {

buf.discard(4); // ignore the lower bound info

const result = new Set(len);
const result = new Array(len);
const elemBuf = ReadBuffer.alloc();
const subCodec = this.subCodec;

Expand Down
16 changes: 2 additions & 14 deletions src/codecs/tuple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,6 @@ import {KNOWN_TYPENAMES} from "./consts";
import {ICodec, Codec, uuid, IArgsCodec, CodecKind} from "./ifaces";
import {ReadBuffer, WriteBuffer} from "../primitives/buffer";

import {
introspectMethod,
IntrospectableType,
CollectionInfo,
} from "../datatypes/introspect";

class Tuple extends Array implements IntrospectableType {
[introspectMethod](): CollectionInfo {
return {kind: "tuple"};
}
}

export class TupleCodec extends Codec implements ICodec, IArgsCodec {
private subCodecs: ICodec[];

Expand Down Expand Up @@ -96,7 +84,7 @@ export class TupleCodec extends Codec implements ICodec, IArgsCodec {
}

const elemBuf = ReadBuffer.alloc();
const result = new Tuple(els);
const result = new Array(els);
for (let i = 0; i < els; i++) {
buf.discard(4); // reserved
const elemLen = buf.readInt32();
Expand Down Expand Up @@ -147,7 +135,7 @@ export class EmptyTupleCodec extends Codec implements ICodec {
`cannot decode empty Tuple: expected 0 elements, received ${els}`
);
}
return new Tuple();
return [];
}

getSubcodecs(): ICodec[] {
Expand Down
4 changes: 2 additions & 2 deletions src/compat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {ymd2ord, ord2ymd} from "./datatypes/dateutil";
browser and NodeJS environments.
*/

/* We customize the `console.log()` rendering of EdgeDB objects
/* We customize the `console.log()` rendering of some EdgeDB objects
in NodeJS. In browsers, however, it's not possible to customize that,
so we're just creating a shell of "util.inspect" so that NodeJS code
can compile unchanged for the browser environment.
Expand Down Expand Up @@ -61,7 +61,7 @@ export function decodeInt64ToString(buf: Buffer): string {
throw new Error("expected 8 bytes buffer");
}

let inp: number[] = Array.from(buf);
let inp: number[] = Array.from(buf);

let negative = false;
if (inp[0] & 0x80) {
Expand Down
43 changes: 0 additions & 43 deletions src/datatypes/introspect.ts

This file was deleted.

0 comments on commit e3676c4

Please sign in to comment.