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

[Console] SQL template with triple quote in completion #45248

Merged
merged 5 commits into from
Sep 11, 2019
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
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"
]
}
}
}