mirror of
https://github.com/denoland/deno.git
synced 2024-11-28 16:20:57 -05:00
fix(ext/kv): keys must be arrays (#18655)
There was some leftover code from previous iterations, where keys could be single parts instead of arrays also. This didn't match the types.
This commit is contained in:
parent
766cd5653f
commit
7ced470477
2 changed files with 49 additions and 17 deletions
|
@ -1138,6 +1138,51 @@ dbTest("operation size limit", async (db) => {
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
dbTest("keys must be arrays", async (db) => {
|
||||||
|
await assertRejects(
|
||||||
|
// @ts-expect-error invalid type
|
||||||
|
async () => await db.get("a"),
|
||||||
|
TypeError,
|
||||||
|
);
|
||||||
|
|
||||||
|
await assertRejects(
|
||||||
|
// @ts-expect-error invalid type
|
||||||
|
async () => await db.getMany(["a"]),
|
||||||
|
TypeError,
|
||||||
|
);
|
||||||
|
|
||||||
|
await assertRejects(
|
||||||
|
// @ts-expect-error invalid type
|
||||||
|
async () => await db.set("a", 1),
|
||||||
|
TypeError,
|
||||||
|
);
|
||||||
|
|
||||||
|
await assertRejects(
|
||||||
|
// @ts-expect-error invalid type
|
||||||
|
async () => await db.delete("a"),
|
||||||
|
TypeError,
|
||||||
|
);
|
||||||
|
|
||||||
|
await assertRejects(
|
||||||
|
async () =>
|
||||||
|
await db.atomic()
|
||||||
|
// @ts-expect-error invalid type
|
||||||
|
.mutate({ key: "a", type: "set", value: 1 } satisfies Deno.KvMutation)
|
||||||
|
.commit(),
|
||||||
|
TypeError,
|
||||||
|
);
|
||||||
|
|
||||||
|
await assertRejects(
|
||||||
|
async () =>
|
||||||
|
await db.atomic()
|
||||||
|
// @ts-expect-error invalid type
|
||||||
|
.check({ key: "a", versionstamp: null })
|
||||||
|
.set(["a"], 1)
|
||||||
|
.commit(),
|
||||||
|
TypeError,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
// This function is never called, it is just used to check that all the types
|
// This function is never called, it is just used to check that all the types
|
||||||
// are behaving as expected.
|
// are behaving as expected.
|
||||||
async function _typeCheckingTests() {
|
async function _typeCheckingTests() {
|
||||||
|
|
|
@ -48,7 +48,6 @@ class Kv {
|
||||||
}
|
}
|
||||||
|
|
||||||
async get(key: Deno.KvKey, opts?: { consistency?: Deno.KvConsistencyLevel }) {
|
async get(key: Deno.KvKey, opts?: { consistency?: Deno.KvConsistencyLevel }) {
|
||||||
key = convertKey(key);
|
|
||||||
const [entries]: [RawKvEntry[]] = await core.opAsync(
|
const [entries]: [RawKvEntry[]] = await core.opAsync(
|
||||||
"op_kv_snapshot_read",
|
"op_kv_snapshot_read",
|
||||||
this.#rid,
|
this.#rid,
|
||||||
|
@ -76,7 +75,6 @@ class Kv {
|
||||||
keys: Deno.KvKey[],
|
keys: Deno.KvKey[],
|
||||||
opts?: { consistency?: Deno.KvConsistencyLevel },
|
opts?: { consistency?: Deno.KvConsistencyLevel },
|
||||||
): Promise<Deno.KvEntry<unknown>[]> {
|
): Promise<Deno.KvEntry<unknown>[]> {
|
||||||
keys = keys.map(convertKey);
|
|
||||||
const ranges: RawKvEntry[][] = await core.opAsync(
|
const ranges: RawKvEntry[][] = await core.opAsync(
|
||||||
"op_kv_snapshot_read",
|
"op_kv_snapshot_read",
|
||||||
this.#rid,
|
this.#rid,
|
||||||
|
@ -103,7 +101,6 @@ class Kv {
|
||||||
}
|
}
|
||||||
|
|
||||||
async set(key: Deno.KvKey, value: unknown) {
|
async set(key: Deno.KvKey, value: unknown) {
|
||||||
key = convertKey(key);
|
|
||||||
value = serializeValue(value);
|
value = serializeValue(value);
|
||||||
|
|
||||||
const checks: Deno.AtomicCheck[] = [];
|
const checks: Deno.AtomicCheck[] = [];
|
||||||
|
@ -123,8 +120,6 @@ class Kv {
|
||||||
}
|
}
|
||||||
|
|
||||||
async delete(key: Deno.KvKey) {
|
async delete(key: Deno.KvKey) {
|
||||||
key = convertKey(key);
|
|
||||||
|
|
||||||
const checks: Deno.AtomicCheck[] = [];
|
const checks: Deno.AtomicCheck[] = [];
|
||||||
const mutations = [
|
const mutations = [
|
||||||
[key, "delete", null],
|
[key, "delete", null],
|
||||||
|
@ -211,14 +206,14 @@ class AtomicOperation {
|
||||||
|
|
||||||
check(...checks: Deno.AtomicCheck[]): this {
|
check(...checks: Deno.AtomicCheck[]): this {
|
||||||
for (const check of checks) {
|
for (const check of checks) {
|
||||||
this.#checks.push([convertKey(check.key), check.versionstamp]);
|
this.#checks.push([check.key, check.versionstamp]);
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
mutate(...mutations: Deno.KvMutation[]): this {
|
mutate(...mutations: Deno.KvMutation[]): this {
|
||||||
for (const mutation of mutations) {
|
for (const mutation of mutations) {
|
||||||
const key = convertKey(mutation.key);
|
const key = mutation.key;
|
||||||
let type: string;
|
let type: string;
|
||||||
let value: RawValue | null;
|
let value: RawValue | null;
|
||||||
switch (mutation.type) {
|
switch (mutation.type) {
|
||||||
|
@ -247,12 +242,12 @@ class AtomicOperation {
|
||||||
}
|
}
|
||||||
|
|
||||||
set(key: Deno.KvKey, value: unknown): this {
|
set(key: Deno.KvKey, value: unknown): this {
|
||||||
this.#mutations.push([convertKey(key), "set", serializeValue(value)]);
|
this.#mutations.push([key, "set", serializeValue(value)]);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
delete(key: Deno.KvKey): this {
|
delete(key: Deno.KvKey): this {
|
||||||
this.#mutations.push([convertKey(key), "delete", null]);
|
this.#mutations.push([key, "delete", null]);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -296,14 +291,6 @@ class KvU64 {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function convertKey(key: Deno.KvKey | Deno.KvKeyPart): Deno.KvKey {
|
|
||||||
if (Array.isArray(key)) {
|
|
||||||
return key;
|
|
||||||
} else {
|
|
||||||
return [key as Deno.KvKeyPart];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function deserializeValue(entry: RawKvEntry): Deno.KvEntry<unknown> {
|
function deserializeValue(entry: RawKvEntry): Deno.KvEntry<unknown> {
|
||||||
const { kind, value } = entry.value;
|
const { kind, value } = entry.value;
|
||||||
switch (kind) {
|
switch (kind) {
|
||||||
|
|
Loading…
Reference in a new issue