-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(jmespath): add parser component
- Loading branch information
1 parent
ba4561d
commit d340d50
Showing
2 changed files
with
878 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
Oops, something went wrong.