Skip to content

Commit

Permalink
Build from edgedb-js 3d789fb906d50101035862d31f93103dc8bccac0
Browse files Browse the repository at this point in the history
  • Loading branch information
edgedb-ci committed Mar 1, 2023
1 parent c306d2d commit 827c229
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 13 deletions.
6 changes: 5 additions & 1 deletion _src/codecs/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {WriteBuffer, ReadBuffer} from "../primitives/buffer.ts";
import {TupleCodec} from "./tuple.ts";
import {RangeCodec} from "./range.ts";
import {InvalidArgumentError, ProtocolError} from "../errors/index.ts";
import {NamedTupleCodec} from "./namedtuple.ts";

export class ArrayCodec extends Codec implements ICodec {
private subCodec: ICodec;
Expand All @@ -37,10 +38,13 @@ export class ArrayCodec extends Codec implements ICodec {
!(
this.subCodec instanceof ScalarCodec ||
this.subCodec instanceof TupleCodec ||
this.subCodec instanceof NamedTupleCodec ||
this.subCodec instanceof RangeCodec
)
) {
throw new InvalidArgumentError("only arrays of scalars are supported");
throw new InvalidArgumentError(
"only arrays of scalars or tuples are supported"
);
}

if (!Array.isArray(obj) && !isTypedArray(obj)) {
Expand Down
53 changes: 48 additions & 5 deletions _src/codecs/namedtuple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ import {
InvalidArgumentError,
MissingArgumentError,
UnknownArgumentError,
ProtocolError
ProtocolError,
QueryArgumentError
} from "../errors/index.ts";

export class NamedTupleCodec extends Codec implements ICodec, IArgsCodec {
Expand All @@ -38,10 +39,52 @@ export class NamedTupleCodec extends Codec implements ICodec, IArgsCodec {
this.namesSet = new Set(names);
}

encode(_buf: WriteBuffer, _object: any): void {
throw new InvalidArgumentError(
"Named tuples cannot be passed in query arguments"
);
encode(buf: WriteBuffer, object: any): void {
if (typeof object !== "object" || Array.isArray(object)) {
throw new InvalidArgumentError(
`an object was expected, got "${object}"`
);
}

const codecsLen = this.subCodecs.length;

if (Object.keys(object).length !== codecsLen) {
throw new QueryArgumentError(
`expected ${codecsLen} element${
codecsLen === 1 ? "" : "s"
} in named tuple, got ${Object.keys(object).length}`
);
}

const elemData = new WriteBuffer();
for (let i = 0; i < codecsLen; i++) {
const key = this.names[i];
const val = object[key];

if (val == null) {
throw new MissingArgumentError(
`element '${key}' in named tuple cannot be 'null'`
);
} else {
elemData.writeInt32(0); // reserved
try {
this.subCodecs[i].encode(elemData, val);
} catch (e) {
if (e instanceof QueryArgumentError) {
throw new InvalidArgumentError(
`invalid element '${key}' in named tuple: ${e.message}`
);
} else {
throw e;
}
}
}
}

const elemBuf = elemData.unwrap();
buf.writeInt32(4 + elemBuf.length);
buf.writeInt32(codecsLen);
buf.writeBuffer(elemBuf);
}

encodeArgs(args: any): Uint8Array {
Expand Down
34 changes: 27 additions & 7 deletions _src/codecs/tuple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ import {KNOWN_TYPENAMES} from "./consts.ts";

import {ICodec, Codec, uuid, IArgsCodec, CodecKind} from "./ifaces.ts";
import {ReadBuffer, WriteBuffer} from "../primitives/buffer.ts";
import {InvalidArgumentError, ProtocolError} from "../errors/index.ts";
import {
InvalidArgumentError,
MissingArgumentError,
ProtocolError,
QueryArgumentError
} from "../errors/index.ts";

export class TupleCodec extends Codec implements ICodec, IArgsCodec {
private subCodecs: ICodec[];
Expand All @@ -30,9 +35,9 @@ export class TupleCodec extends Codec implements ICodec, IArgsCodec {
this.subCodecs = codecs;
}

encode(buf: WriteBuffer, object: any): void {
encode(buf: WriteBuffer, object: any, allowNull: boolean = false): void {
if (!Array.isArray(object)) {
throw new InvalidArgumentError(`an array was expected, got ${object}`);
throw new InvalidArgumentError(`an array was expected, got "${object}"`);
}

const codecs = this.subCodecs;
Expand All @@ -55,10 +60,25 @@ export class TupleCodec extends Codec implements ICodec, IArgsCodec {
const elem = object[i];
elemData.writeInt32(0); // reserved bytes
if (elem == null) {
elemData.writeInt32(-1);
if (allowNull) {
elemData.writeInt32(-1);
} else {
throw new MissingArgumentError(
`element at index ${i} in tuple cannot be 'null'`
);
}
} else {
const codec = codecs[i];
codec.encode(elemData, elem);
try {
codecs[i].encode(elemData, elem);
} catch (e) {
if (e instanceof QueryArgumentError) {
throw new InvalidArgumentError(
`invalid element at index ${i} in tuple: ${e.message}`
);
} else {
throw e;
}
}
}
}

Expand All @@ -84,7 +104,7 @@ export class TupleCodec extends Codec implements ICodec, IArgsCodec {
}

const buf = new WriteBuffer();
this.encode(buf, args);
this.encode(buf, args, true);
return buf.unwrap();
}

Expand Down

0 comments on commit 827c229

Please sign in to comment.