Skip to content

Commit

Permalink
[Console] SQL template with triple quote in completion (elastic#45248)
Browse files Browse the repository at this point in the history
* SQL template with triple quote in completion

* Slight update to SQL query template and bugfix for collapse/expand of request bodies

* Add comment and slight update to triple quote expansion

* Updated tests after changes to newline behaviour

* Restore old backslash triple quote expansion behaviour
  • Loading branch information
jloleysens committed Sep 11, 2019
1 parent 8ad0c71 commit 42f4c8c
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 12 deletions.
9 changes: 8 additions & 1 deletion src/legacy/core_plugins/console/public/src/autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,14 @@ export default function (editor) {
let valueToInsert = termAsString;
let templateInserted = false;
if (context.addTemplate && !_.isUndefined(term.template) && !_.isNull(term.template)) {
const indentedTemplateLines = utils.jsonToString(term.template, true).split('\n');
let indentedTemplateLines;
// In order to allow triple quoted strings in template completion we check the `__raw_`
// attribute to determine whether this template should go through JSON formatting.
if (term.template.__raw && term.template.value) {
indentedTemplateLines = term.template.value.split('\n');
} else {
indentedTemplateLines = utils.jsonToString(term.template, true).split('\n');
}
let currentIndentation = session.getLine(context.rangeToReplace.start.row);
currentIndentation = currentIndentation.match(/^\s*/)[0];
for (let i = 1; i < indentedTemplateLines.length; i++) // skip first line
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,18 @@ class ScopeResolver extends SharedComponent {
}
function getTemplate(description) {
if (description.__template) {
if (description.__raw && _.isString(description.__template)) {
return {
// This is a special secret attribute that gets passed through to indicate that
// the raw value should be passed through to the console without JSON.stringifying it
// first.
//
// Primary use case is to allow __templates to contain extended JSON special values like
// triple quotes.
__raw: true,
value: description.__template,
};
}
return description.__template;
} else if (description.__one_of) {
return getTemplate(description.__one_of[0]);
Expand Down
11 changes: 6 additions & 5 deletions src/legacy/core_plugins/console/public/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,19 @@ utils.reformatData = function (data, indent) {
};

utils.collapseLiteralStrings = function (data) {
return data.replace(/"""(?:\s*\r?\n)?((?:.|\r?\n)*?)(?:\r?\n\s*)?"""/g, function (match, literal) {
return JSON.stringify(literal);
});
const splitData = data.split(`"""`);
for (let idx = 1; idx < splitData.length - 1; idx += 2) {
splitData[idx] = JSON.stringify(splitData[idx]);
}
return splitData.join('');
};

utils.expandLiteralStrings = function (data) {
return data.replace(/("(?:\\"|[^"])*?")/g, function (match, string) {
// expand things with two slashes or more
if (string.split(/\\./).length > 2) {
string = JSON.parse(string).replace('^\s*\n', '').replace('\n\s*^', '');
const append = string.includes('\n') ? '\n' : ''; // only go multi line if the string has multiline
return '"""' + append + string + append + '"""';
return '"""' + string + '"""';
} else {
return string;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ to you """
==========
String only 2
-------------------------------------
"""
"""
starting with new lines and ending as well
"""
"""
-------------------------------------
"starting with new lines and ending as well"
"\nstarting with new lines and ending as well\n"
==========
Strings in requests
-------------------------------------
Expand All @@ -27,8 +27,8 @@ test2
}
-------------------------------------
{
"f": { "somefield" : "test\ntest2" },
"f": { "somefield" : "\ntest\ntest2\n" },
"g": { "script" : "second + \"\\\";" },
"h": 1,
"script": "a + 2"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Scripts in requests
-------------------------------------
{
"f": { "script" : { "source": "test\ntest\\2" } },
"f": { "script" : { "source": "\ntest\ntest\\2\n" } },
"g": { "script" : "second + \"\\\";" },
"f": "short with \\",
"h": 1,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"sql.query": {
"data_autocomplete_rules": {
"query": {
"__template": {
"__raw": true,
"value": "\"\"\"\nSELECT * FROM \"TABLE\"\n\"\"\""
}
}
},
"url_params": {
"format": [
"csv",
"json",
"tsv",
"txt",
"yaml",
"cbor",
"smile"
]
}
}
}

0 comments on commit 42f4c8c

Please sign in to comment.