Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(jmespath): add parser component #2266

Merged
merged 3 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions packages/jmespath/src/ParsedResult.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { TreeInterpreter } from './TreeInterpreter.js';
import {
ArityError,
JMESPathTypeError,
UnknownFunctionError,
VariadicArityError,
} from './errors.js';
import type { Node, ParsingOptions, JSONObject } from './types.js';

class ParsedResult {
public expression: string;
public parsed: Node;

public constructor(expression: string, parsed: Node) {
this.expression = expression;
this.parsed = parsed;
}

/**
* Perform a JMESPath search on a JSON value.
*
* @param value The JSON value to search
* @param options The parsing options to use
*/
public search(value: JSONObject, options?: ParsingOptions): unknown {
const interpreter = new TreeInterpreter(options);

try {
return interpreter.visit(this.parsed, value);
} catch (error) {
if (
error instanceof JMESPathTypeError ||
error instanceof UnknownFunctionError ||
error instanceof ArityError ||
error instanceof VariadicArityError
) {
error.setExpression(this.expression);
}
throw error;
}
}
}

export { ParsedResult };
Loading