Couch DB escape characters - couchdb

I have a list function for CouchDB, but I can't get it into the DB because I'm constantly getting syntax erros. Here is the function:
function(head,req) {
var headers;
start({'headers':{'Content-Type' : 'text/csv; charset=utf-8; header=present'}});
while(r = getRow()) {
if(!headers) {
headers = Object.keys(r.value);
send('"' + headers.join('","') + '"\n');
}
headers.forEach(function(v,i) {
send(String(r.value[v]).replace(/\"/g,'""').replace(/^|$/g,'"'));
(i + 1 < headers.length) ? send(',') : send('\n');
});
}
}
Can anyone show me an example of this function formatted that can be inserted into CouchDB?

List functions are stored in design documents. Design documents are JSON documents, so you need to make sure they conform to the JSON standard. Since List functions are string values in JSON, you need to make sure that you:
Escape any double quotes in the function, so " becomes \". Where possible, you should use single quotes instead of double quotes.
Make sure you replace any line breaks with \n or just remove line breaks as Javascript ignores them anyway.
Escape any backslashes, so \ becomes \\.

Related

Why postgres is returning additional backslash in a simple query

So in my node code postgres query is returning double quotes when it's returning its values.
As opposed to the query at pgAdmin.
I already tried to solve it using regex but this attempt was innefective. So if anyone had a problem like this and could help me, I would be glad.
Thanks in advance
There are neither quotes nor extra back slashes in the string. They are part of the string representation as literal.
Try console.log(value) - or even directly console.log('/\\w/g') - and you'll see the output is /\w/g as expected.
To answer my own question, after a lot of reading and researching, I managed to discover that because a backslash character is a special character it will create some problems around its implementation in regex, because it is not permitted to have a lone backslash stored in a variable for example.
This would never work stored inside a variable because the backslash have to be escaped.
/\w+/ig
Javascript will transform it automatically to be able to perform.
/\w+/ig
When reading
RegExp - Javascript documentation, I came across an interesting statement, the RegExp function will recognize and use a double slash regex, thankfully!
So I just adapted my regex to split it's statement from it's flags and mount it again using RegExp.
Below is the code that I used to solve this problem
// Getting values from postgres
const values = (await pgConn.admRead.query(clientQuery)).rows[0].value || [];
// Splitting regex ( values: /\w/g )
const valuesSplit = values.split('/'); // RESULT -> ['', w, g]
// Removing first array item when it's empty
if (valuesSplit[0].length === 0) {
valuesSplit.shift();
}
// Creating regex from splitted array
const regexOperation = new RegExp(valuesSplit[0], valuesSplit[1]);
// Executing replace function
const messageMasked = message.replace(regexOperation, '*');
return messageMasked;

Jenkins groovy writeYaml method: single quote replaced with triple quotes when writing to yaml file

I want to update a string in quotes in yaml file in Jenkins Job. While updating the file, single quotes around the string are replaced by triple quotes. Following is the method that I have written:
{
def fileName = 'config.yml'
datas = readYaml file: fileName
var = "'" + params.ReleaseBranchName + "'"
println var // this shows output as expected, string in single quotes -> 'rel-21.9'
datas.branchName = var
println datas // this prints the yaml with single quotes -> productiveBranch='rel-21.9',
writeYaml file: fileName, data: datas, overwrite: true //this show value in triple quotes -> productiveBranch: '''rel-21.9'''
}
Could someone suggest how can I save string with single quotes in yaml file? Thanks!
The value of var, as written, is 'rel-21.9', i.e. the single quotes are part of the value.
In YAML input, when 'rel-21.9' is encountered, the single quotes are not part of the value; they are part of the syntax and enclose the value, so the value is rel-21.9.
Therefore, if you want your value to be rel-21.9, which is most probably what you want, do not put single quotes in the value; just do var = params.ReleaseBranchName.
Your code does not do anything with var; I assume what you're trying is to put it into datas. This would result in YAML writing out "'rel-21.9'" (not triple single quotes, that can't happen since it would be invalid YAML). By surrounding the value with double quotes, the single quotes become part of the value just like your code requested.
When you do not put single quotes into the data, YAML will probably serialise it without any quotes. This is expected since rel-21.9 does not contain any special characters that would require quoting. There are ways to force a YAML processor to quote a value, but they are complex and I am unsure whether the API is exposed to Groovy. For references, this is how you would do it in Java.
Since you are editing a YAML file, you might want to read this question which details how and why updating YAML files in code can lead to style changes.

nodejs how to replace ; with ',' to make an sql query

I have a query that looks like this:
INSERT INTO table VALUES ('47677;2019;2019;10T-1001-10010AS;A05;International;TieLineKoman-KosovoB;L_KOM-KOSB;2018;NULL;NULL;;NULL;Tieline;NULL;10XAL-KESH-----J;0;3')
that is produced by parsing a csv file.
The query is not in a valid form, I have to replace all semicolons with the string ',' (comma inside single quotes). What I want to get is:
('47677','2019','2019','10T-1001-10010AS','A05','International','TieLineKoman-KosovoB','L_KOM-KOSB','2018','NULL','NULL','','NULL','Tieline','NULL','10XAL-KESH-----J','0','3')
I have tried to do this in many different ways, but I end up with backshlashes added in my string. This is what I get:
"INSERT INTO AllocatedEICDetail VALUES ('47677\\',\\'2019\\',\\'2019\\',\\'10T-1001-10010AS\\',\\'A05\\',\\'International\\',\\'TieLineKoman-KosovoB\\',\\'L_KOM-KOSB\\',\\'2018\\',\\'NULL\\',\\'NULL\\',\\'\\',\\'NULL\\',\\'Tieline\\',\\'NULL\\',\\'10XAL-KESH-----J\\',\\'0\\',\\'3')"
Any ideas how to do this properly without having the backslashes added?
Thank you!
//the string you have
const string = '47677;2019;2019;10T-1001-10010AS;A05;International;TieLineKoman-KosovoB;L_KOM-KOSB;2018;NULL;NULL;;NULL;Tieline;NULL;10XAL-KESH-----J;0;3';
//the string you need:
const targetString = string.replace(/\;/g,',');
You specify a small regex between the forward slashes in replace which is a simple ';', give it a 'g' flag for global which will replace all instances, and in the second argument supply what you need it replaced with.

golang remove characters (used for readability) in const string at compile time (spaces, \n and \t)

Spaces are useful to indent urls, sql queries to make it more readable.
Is there a way to remove characters from a const string at compile time in golang ?
ex: (runtime version)
const url = `https://example.com/path?
attr1=test
&attr2=test
`
// this is the code to be replaced
urlTrim := strings.Replace(
strings.Replace(url, "\n", "", -1)
)
Constant expressions cannot contain function calls (except a few built-in functions). So what you want cannot be done using a raw string literal.
If your goal with using multiple lines is just for readability, simply use multiple literals and concatenate them:
const url = "https://example.com/path?" +
"attr1=test" +
"&attr2=test"
Try it on the Go Playground.
See related question: Initialize const variable

nodejs skipping single quote from json key in output

I see a very weird problem when json when used in nodejs, it is skipping single quote from revision key . I want to pass this json as input to node request module and since single quote is missing from 'revision' key so it is not taking as valid json input. Could someone help how to retain it so that I can use it. I have tried multiple attempts but not able to get it correct.
What did I try ?
console.log(jsondata)
jsondata = {
'splits': {
'os-name': 'ubuntu',
'platform-version': 'os',
'traffic-percent': 100,
'revision': 'master'
}
}
Expected :-
{ splits:
{ 'os-name': 'ubuntu',
'platform-version': 'os',
'traffic-percent': 100,
'revision': 'master'
}
}
But in actual output single quote is missing from revision key :-
{ splits:
{ 'os-name': 'ubuntu',
'platform-version': 'os',
'traffic-percent': 100,
revision: 'master'
}
}
Run 2 :- Tried below code this also produce same thing.
data = JSON.stringify(jsondata)
result = JSON.parse(data)
console.log(result)
Run 3:- Used another way to achieve it
jsondata = {}
temp = {}
splits = []
temp['revision'] = 'master',
temp['os-name'] = 'ubuntu'
temp['platform-version'] = 'os'
temp['traffic-percent'] = 100
splits.push(temp)
jsondata['splits'] = splits
console.log(jsondata)
Run 4: tries replacing single quotes to double quotes
Run 5 : Change the order of revision line
This is what is supposed to happen. The quotes are kept only if the object key it’s not a valid JavaScript identifier. In your example, the 'splits' & 'revision' don't have a dash in their name, so they are the only ones with the quotes removed.
You shouldn't receive any error using this object - if you do, update this post mentioning the scenario and the error.
You should note that JSON and JavaScript are not the same things.
JSON is a format where all keys and values are surrounded by double quotes ("key" and "value"). A JSON string is produced by JSON.stringify, and is required by JSON.parse.
A JavaScript object has very similar syntax to the JSON file format, but is more flexible - the values can be surrounded by double quotes or single quotes, and the keys can have no quotes at all as long as they are valid JavaScript identifiers. If the keys have spaces, dashes, or other non-valid characters, then they need to be surrounded by single quotes or double quotes.
If you need your string to be valid JSON, generate it with JSON.stringify. If it's OK for it to be just valid JavaScript, then it's already fine - it does not matter whether the quotes are there or not.
If, for some reason, you need some imaginary third option (perhaps you are interacting with an API where someone has written their own custom string parser, and they are demanding that all keys are surrounded by single quotes?) you will probably need to write your own little string generator.

Resources