Skip to content

Commit

Permalink
Refactor codec to TS generation for fine grained customization (#889)
Browse files Browse the repository at this point in the history
  • Loading branch information
CarsonF committed Mar 20, 2024
1 parent 78bf38b commit cb3569f
Show file tree
Hide file tree
Showing 8 changed files with 254 additions and 128 deletions.
@@ -0,0 +1 @@
select <array<tuple<str, str>>>$deep;
28 changes: 28 additions & 0 deletions integration-tests/lts/queries.test.ts
Expand Up @@ -6,6 +6,8 @@ import {
getMoviesStarring,
type GetMoviesStarringArgs,
type GetMoviesStarringReturns,
deepArrayInput,
type DeepArrayInputArgs,
} from "./dbschema/queries";
import { setupTests, teardownTests } from "./setupTeardown";

Expand Down Expand Up @@ -68,4 +70,30 @@ describe("queries", () => {

tc.assert<tc.IsExact<GetMoviesStarringReturns, result>>(true);
});

test("deep array input", async () => {
const result = await deepArrayInput(client, {
deep: [
['name', 'Stark'],
['color', 'red'],
] as const,
});

type result = typeof result;
tc.assert<
tc.IsExact<
result,
Array<[string, string]>
>
>(true);

tc.assert<
tc.IsExact<
DeepArrayInputArgs,
{
deep: ReadonlyArray<readonly [string, string]>;
}
>
>(true);
});
});
23 changes: 23 additions & 0 deletions packages/driver/src/baseClient.ts
Expand Up @@ -38,6 +38,7 @@ import Event from "./primitives/event";
import { LifoQueue } from "./primitives/queues";
import { BaseRawConnection } from "./baseConn";
import { ConnectWithTimeout, retryingConnect } from "./retry";
import { util } from "./reflection/util";
import { Transaction } from "./transaction";
import { sleep } from "./utils";

Expand Down Expand Up @@ -660,4 +661,26 @@ export class Client implements Executor {
await holder.release();
}
}

async parse(query: string) {
const holder = await this.pool.acquireHolder(this.options);
try {
const cxn = await holder._getConnection();
const result = await cxn._parse(
query,
OutputFormat.BINARY,
Cardinality.MANY,
this.options.session
);
const cardinality = util.parseCardinality(result[0]);

return {
in: result[1],
out: result[2],
cardinality,
};
} finally {
await holder.release();
}
}
}
6 changes: 0 additions & 6 deletions packages/driver/src/codecs/consts.ts
Expand Up @@ -57,9 +57,3 @@ export const KNOWN_TYPENAMES = (() => {
}
return res;
})();

export const NO_RESULT = 0x6e;
export const AT_MOST_ONE = 0x6f;
export const ONE = 0x41;
export const MANY = 0x6d;
export const AT_LEAST_ONE = 0x4d;
8 changes: 4 additions & 4 deletions packages/driver/src/codecs/object.ts
Expand Up @@ -16,9 +16,9 @@
* limitations under the License.
*/

import { Cardinality } from "../ifaces";
import { ICodec, Codec, uuid, CodecKind } from "./ifaces";
import { ReadBuffer, WriteBuffer } from "../primitives/buffer";
import { ONE, AT_LEAST_ONE } from "./consts";
import {
InvalidArgumentError,
MissingArgumentError,
Expand All @@ -34,7 +34,7 @@ export interface ObjectFieldInfo {
name: string;
implicit: boolean;
linkprop: boolean;
cardinality: number;
cardinality: Cardinality;
}

export class ObjectCodec extends Codec implements ICodec {
Expand Down Expand Up @@ -104,7 +104,7 @@ export class ObjectCodec extends Codec implements ICodec {
const arg = args[i];
if (arg == null) {
const card = this.cardinalities[i];
if (card === ONE || card === AT_LEAST_ONE) {
if (card === Cardinality.ONE || card === Cardinality.AT_LEAST_ONE) {
throw new MissingArgumentError(
`argument ${this.fields[i].name} is required, but received ${arg}`
);
Expand Down Expand Up @@ -154,7 +154,7 @@ export class ObjectCodec extends Codec implements ICodec {
elemData.writeInt32(0); // reserved bytes
if (val == null) {
const card = this.cardinalities[i];
if (card === ONE || card === AT_LEAST_ONE) {
if (card === Cardinality.ONE || card === Cardinality.AT_LEAST_ONE) {
throw new MissingArgumentError(
`argument ${this.fields[i].name} is required, but received ${val}`
);
Expand Down

0 comments on commit cb3569f

Please sign in to comment.