Files
signoz/frontend/src/lib/logql/reverseParser.ts
Amol Umbark faeaeb61a0 fix: added validations on query builder (#1906)
Co-authored-by: mindhash <mindhash@mindhashs-MacBook-Pro.local>
Co-authored-by: Pranay Prateek <pranay@signoz.io>
Co-authored-by: Ankit Nayan <ankit@signoz.io>
2022-12-26 15:10:01 +05:30

40 lines
1.1 KiB
TypeScript

/* eslint-disable */
// @ts-ignore
// @ts-nocheck
import { QueryTypes, StringTypeQueryOperators } from "./tokens";
export const reverseParser = (
parserQueryArr: { type: string; value: any }[] = [],
) => {
let queryString = '';
let lastToken: { type: string; value: any };
parserQueryArr.forEach((query) => {
if (queryString) {
queryString += ' ';
}
if (Array.isArray(query.value) && query.value.length > 0) {
// if the values are array type, here we spread them in
// ('a', 'b') format
queryString += `(${query.value.map((val) => `'${val}'`).join(',')})`;
} else {
if (query.type === QueryTypes.QUERY_VALUE
&& lastToken.type === QueryTypes.QUERY_OPERATOR
&& Object.values(StringTypeQueryOperators).includes(lastToken.value) ) {
// for operators that need string type value, here we append single
// quotes. if the content has single quote they would be removed
queryString += `'${query.value?.replace(/'/g, '')}'`;
} else {
queryString += query.value;
}
}
lastToken = query;
});
// console.log(queryString);
return queryString;
};
export default reverseParser;