Skip to content

Commit

Permalink
Make it possible to unset tag
Browse files Browse the repository at this point in the history
  • Loading branch information
jaclarke committed Nov 27, 2024
1 parent ff04440 commit dd8edc1
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 19 deletions.
2 changes: 1 addition & 1 deletion packages/driver/src/baseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,7 @@ export class Client implements Executor {
);
}

withTag(tag: string): Client {
withTag(tag: string | null): Client {
return new Client(
this.pool,
this.options.withSession(this.options.session.withTag(tag)),
Expand Down
5 changes: 2 additions & 3 deletions packages/driver/src/baseConn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -899,9 +899,8 @@ export class BaseRawConnection {
options: QueryOptions | undefined,
language: Language,
) {
const annotations = Object.entries(state.annotations);
wb.writeUInt16(annotations.length);
for (const [name, value] of annotations) {
wb.writeUInt16(state.annotations.size);
for (const [name, value] of state.annotations) {
wb.writeString(name);
wb.writeString(value);
}
Expand Down
36 changes: 21 additions & 15 deletions packages/driver/src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,11 @@ export class Session {
readonly globals: Record<string, any>;

/** @internal */
annotations: Record<string, string> = {};
annotations = new Map<string, string>();

get tag(): string | null {
return this.annotations.get(TAG_ANNOTATION_KEY) ?? null;
}

constructor({
module = "default",
Expand Down Expand Up @@ -186,21 +190,23 @@ export class Session {
});
}

withTag(tag: string): Session {
if (tag.startsWith("edgedb/")) {
throw new errors.InterfaceError("reserved tag: edgedb/*");
}
if (tag.startsWith("gel/")) {
throw new errors.InterfaceError("reserved tag: gel/*");
}
if (utf8Encoder.encode(tag).length > 128) {
throw new errors.InterfaceError("tag too long (> 128 bytes)");
}
withTag(tag: string | null): Session {
const session = new Session({ ...this });
session.annotations = {
...this.annotations,
[TAG_ANNOTATION_KEY]: tag,
};
session.annotations = new Map(this.annotations);
if (tag != null) {
if (tag.startsWith("edgedb/")) {
throw new errors.InterfaceError("reserved tag: edgedb/*");
}
if (tag.startsWith("gel/")) {
throw new errors.InterfaceError("reserved tag: gel/*");
}
if (utf8Encoder.encode(tag).length > 128) {
throw new errors.InterfaceError("tag too long (> 128 bytes)");
}
session.annotations.set(TAG_ANNOTATION_KEY, tag);
} else {
session.annotations.delete(TAG_ANNOTATION_KEY);
}
return session;
}

Expand Down

0 comments on commit dd8edc1

Please sign in to comment.