Skip to content

Commit

Permalink
check explain value in explain command constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
HanaPearlman committed Nov 2, 2020
1 parent fee4091 commit 35d1814
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions src/explain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ export abstract class ExplainableCommand<

constructor(parent?: OperationParent, options?: T) {
super(parent, options);

if (!Explain.explainOptionsValid(options)) {
throw new MongoError(`explain must be one of ${Object.keys(Verbosity)} or a boolean`);
}

this.explain = Explain.fromOptions(options);
}

Expand Down Expand Up @@ -57,15 +62,15 @@ export type VerbosityLike = Verbosity | boolean;

/** @internal */
export class Explain {
explain: Verbosity;
verbosity: Verbosity;

constructor(explain: VerbosityLike) {
if (typeof explain === 'boolean') {
constructor(verbosity: VerbosityLike) {
if (typeof verbosity === 'boolean') {
// For backwards compatibility, true is interpreted as
// "allPlansExecution" and false as "queryPlanner".
this.explain = explain ? Verbosity.allPlansExecution : Verbosity.queryPlanner;
this.verbosity = verbosity ? Verbosity.allPlansExecution : Verbosity.queryPlanner;
} else {
this.explain = Verbosity[explain];
this.verbosity = Verbosity[verbosity];
}
}

Expand All @@ -76,6 +81,14 @@ export class Explain {
return new Explain(options.explain);
}

static explainOptionsValid(options?: ExplainOptions): boolean {
if (options == null || options.explain === undefined) {
return true;
}
const explain = options.explain;
return typeof explain === 'boolean' || explain in Verbosity;
}

/**
* Checks that the server supports explain on the given operation.
* @internal
Expand Down Expand Up @@ -113,15 +126,12 @@ export class Explain {
* @param command - the command on which to apply the read concern
* @param options - the options containing the explain verbosity
*/
export function decorateWithExplain(command: Document, options: ExplainOptions): Document {
const explain = Explain.fromOptions(options);
if (explain === undefined) return command;

export function decorateWithExplain(command: Document, explain: Explain): Document {
// A command being explained may not have an explain field directly on it
if (command.explain !== undefined) {
delete command.explain;
}

command = { explain: command, verbosity: explain.explain };
command = { explain: command, verbosity: explain.verbosity };
return command;
}

0 comments on commit 35d1814

Please sign in to comment.