Simplification of groovy substring call with null argument - if-statement

I'm using some archaic version control, and am trying to parse the comments using Groovy for Jenkins usage.
I managed to get the Groovy code working, but was wondering if there might be a way to simplify the code.
The test string which I use is as follows:
text = "IL!21234 12/3/18 3:46 PM user_d Some comments with new\nlines interspersed and pointers to commits\n(IL!1234)\nIL!1234 1/2/17 2:46 AM user_x Some other commit\n"
The expected result is:
tasks = ["IL!21234 12/3/18 3:46 PM user_d Some comments with new lines interspersed and pointers to commits (IL!1234)", "IL!1234 1/2/17 2:46 AM user_x Some other commit"]
Here follows my code:
m = (text =~ /IL!\d+ \d{1,2}\/\d{1,2}\/\d{1,2} \d{1,2}:\d{1,2} [AP]M [a-z_]* .*/)
matchStarts = []
tasks = []
while(m.find()) {
matchStarts << m.start()
}
matchStarts.eachWithIndex { matchStart, index ->
if (matchStarts[index + 1]) {
tasks << text.substring(matchStart, matchStarts[index + 1]).replace("\n", " ").trim()
} else {
tasks << text.substring(matchStart).replace("\n", " ").trim()
}
}
This works, but I was wondering if there would be a nicer way to deal with the if/else.
Thanks,

Related

Too much information

I have this code running fine but then it shows too much information and I only want the last line in the code.
Code here:
import scala.io.Source
object CovidWorld {
def main(args: Array[String]): Unit = {
val filename = Source.fromFile("OOPAssignment3.txt")
try{
for (line <- filename.getLines.toList) {
if (line.contains("Malaysia") && line.split(",").apply(7).nonEmpty) {
val allDeathString: String = line.split(",").apply(7)
print("\n\n Malaysia latest total amount of death: " + allDeathString)
}
}
}
finally{
filename.close
//print("\nThe file is now closed")
}
}
}
This is the result I obtain from it.result of the running code
I just want the last line of the information instead of the entire thing. Anyone can figure out how? Thanks in advance for the help :)
You can replace the inside of your try block with:
filename
.getLines
.filter(line => line.contains("Malaysia") && line.split(",").apply(7).nonEmpty)
.toList
.takeRight(1)
.foreach(line => {
val allDeathString: String = line.split(",").apply(7)
print("\n\n Malaysia latest total amount of death: " + allDeathString)
})
The key part for your purposes is takeRight which selects n elements from the end of the list. When n == 1 you're taking only the last match which is what you want here.

Typescript regex exclude whole string if followed by specific string

I'm been running into weird issues with regex and Typescript in which I'm trying to have my expression replace the value of test minus the first instance if followed by test. In other words, replace the first two lines that have test but for the third line below, replace only the second value of test.
[test]
[test].[db]
[test].[test]
Where it should look like:
[newvalue]
[newvalue].[db]
[test].[newvalue]
I've come up with lots of variations but this is the one that I thought was simple enough to solve it and regex101 can confirm this works:
\[(\w+)\](?!\.\[test\])
But when using Typescript (custom task in VSTS build), it actually replaces the values like this:
[newvalue]
[newvalue].[db]
[newvalue].[test]
Update: It looks like a regex like (test)(?!.test) breaks when changing the use cases removing the square brackets, which makes me think this might be somewhere in the code. Could the problem be with the index that the value is replaced at?
Some of the code in Typescript that is calling this:
var filePattern = tl.getInput("filePattern", true);
var tokenRegex = tl.getInput("tokenRegex", true);
for (var i = 0; i < files.length; i++) {
var file = files[i];
console.info(`Starting regex replacement in [${file}]`);
var contents = fs.readFileSync(file).toString();
var reg = new RegExp(tokenRegex, "g");
// loop through each match
var match: RegExpExecArray;
// keep a separate var for the contents so that the regex index doesn't get messed up
// by replacing items underneath it
var newContents = contents;
while((match = reg.exec(contents)) !== null) {
var vName = match[1];
// find the variable value in the environment
var vValue = tl.getVariable(vName);
if (typeof vValue === 'undefined') {
tl.warning(`Token [${vName}] does not have an environment value`);
} else {
newContents = newContents.replace(match[0], vValue);
console.info(`Replaced token [${vName }]`);
}
}
}
Full code is for the task I'm using this with: https://github.com/colindembovsky/cols-agent-tasks/blob/master/Tasks/ReplaceTokens/replaceTokens.ts
For me this regex is working like you are expecting:
\[(test)\](?!\.\[test\])
with a Typescript code like that
myString.replace(/\[(test)\](?!\.\[test\])/g, "[newvalue]");
Instead, the regex you are using should replace also the [db] part.
I've tried with this code:
class Greeter {
myString1: string;
myString2: string;
myString3: string;
greeting: string;
constructor(str1: string, str2: string, str3: string) {
this.myString1 = str1.replace(/\[(test)\](?!\.\[test\])/g, "[newvalue]");
this.myString2 = str2.replace(/\[(test)\](?!\.\[test\])/g, "[newvalue]");
this.myString3 = str3.replace(/\[(test)\](?!\.\[test\])/g, "[newvalue]");
this.greeting = this.myString1 + "\n" + this.myString2 + "\n" + this.myString3;
}
greet() {
return "Hello, these are your replacements:\n" + this.greeting;
}
}
let greeter = new Greeter("[test]", "[test].[db]", "[test].[test]");
let button = document.createElement('button');
button.textContent = "Say Hello";
button.onclick = function() {
alert(greeter.greet());
}
document.body.appendChild(button);
Online playground here.

Why isn't Roslyn respecting Whitespace trivia, forcing tab indentation instead?

I am developing a little hobby project trying to bring C# down to be compilable to the NES over at this repository. As of commit 9b532f739be some of the unit tests on my first Code Fix Provider are failing only because Roslyn is ignoring the Whitespace trivia that is coming from a node that I want replaced, in line 90 of the ForbidMuliplicationCodeFixProvider.cs file. I've also tried NormalizeWhiteSpace on the new node with the same WhiteSpaceTrivia indent size, and a couple other failed attemps, only to always end up with standard tab-size indents on the modified document. Will I have to live with this little bug?
var assignment = SyntaxFactory.AssignmentExpression(SyntaxKind.SimpleAssignmentExpression, variableIdentifier, invocation);
assignment = assignment.WithTriviaFrom(node);
var syntaxRoot = await document.GetSyntaxRootAsync();
var modifiedRoot = syntaxRoot.ReplaceNode(node, assignment);
var modifiedDocument = document.WithSyntaxRoot(modifiedRoot);
modifiedDocument = await AddUsingStatementAsync(cancelToken, modifiedDocument);
return modifiedDocument;
The node variable comes from the original document with 6 spaces (3 tiers of indentation) of WhiteSpaceTrivia, which are supposed to be copied into the replacement node but the output of the modified document has 3 tabs of indentation instead.
My test fails with this message:
The checked string has different spaces than expected one. At line 7, col 12, expected '... test = NESMath....' was '... test = NE...'.
The checked string:
["using NINNES.Platform.Shims;
namespace NINNES.RoslynAnalyzers.Tests.Assets {
class MultiplicationAssignment {
public void Multiply() {
var test = 2;
test = NESMath.Multiply(test, 42);
}
}
}
"]
The expected string:
["using NINNES.Platform.Shims;
namespace NINNES.RoslynAnalyzers.Tests.Assets {
class MultiplicationAssignment {
public void Multiply() {
var test = 2;
test = NESMath.Multiply(test, 42);
}
}
}
"]
As an aside: Commentary on the Roslyn code would be greatly appreciated, just learning the ropes of the code modification stuff.

Create stored procedure from x++

Got myself into trouble today trying to create a stored procedure from ax.
Here is a simple example:
static void testProcedureCreation(Args _args)
{
MyParamsTable myParams;
SqlStatementExecutePermission perm;
str sqlStatement;
LogInProperty Lp = new LogInProperty();
OdbcConnection myConnection;
Statement myStatement;
ResultSet myResult;
str temp;
;
select myParams;
LP.setServer(myParams.Server);
LP.setDatabase(myParams.Database);
//Lp.setUsername("sa");
//Lp.setPassword("sa");
sqlStatement = #"create procedure testproc
as begin
print 'a'
end";
//sqlStatement = strFmt(sqlStatement, myStr);
info(sqlStatement);
perm = new SqlStatementExecutePermission(sqlStatement);
perm.assert();
try
{
myConnection = new OdbcConnection(LP);
}
catch
{
info("Check username/password.");
return;
}
myStatement = myConnection.createStatement();
myResult = myStatement.executeQuery(sqlStatement);
while (myResult.next())
{
temp = myResult.getString(1);
info(temp);
if (strScan(temp, 'Error', 1, strLen(temp)) > 0)
throw error(temp);
}
myStatement.close();
CodeAccessPermission::revertAssert();
}
To be honest, in my real example I am using BCP and some string concat with a lot of | ' and "".
Anyway, here is what I got:
For a couple of hours I kept changing and retrying a lot of things and, a good thought came into my mind.
"Let's try with a much easier example and check the results!"
OK, no luck, the results were the same, as you can see in the pic above.
But for no reason, I tried to :
exec testproc
in my ssms instance and to my surprise, it worked. My small procedure was there.
It would be so nice if someone could explain this behavior and maybe what should be the correct approach.
This Q/A should provide an answer.
How to get the results of a direct SQL call to a stored procedure?
executeQuery vs executeUpdate

Struggling with simple Groovy List operations

I've abstracted a very simple situation here, in which I want to pass a list of strings into my cleanLines function and get a list of strings back out. Unfortunately, I'm new to Groovy and I've spent about a day trying to get this to work with no avail. Here's a stand-alone test that exhibits the problem I'm having:
import static org.junit.Assert.*;
import java.util.List;
import org.junit.Test;
class ConfigFileTest {
private def tab = '\t'
private def returnCarriage = '\r'
private def equals = '='
List <String> cleanLines(List <String> lines) {
lines = lines.collect(){it.findAll(){c -> c != tab && c != returnCarriage}}
lines = lines.findAll(){it.contains(equals)}
lines = lines.collect{it.trim()}
}
#Test
public void test() {
List <String> dirtyLines = [" Colour=Red",
"Shape=Square "]
List <String> cleanedLines = ["Colour=Red",
"Shape=Square"]
assert cleanLines(dirtyLines) == cleanedLine
}
}
I believe that I've followed the correct usage for collect(), findAll() and trim(). But when I run the test, it crashes on the trim() line stating
groovy.lang.MissingMethodException: No signature of method:
java.util.ArrayList.trim() is applicable for argument types: ()
values: []
. Something's suspicious.
I've been staring at this for too long and noticed that my IDE thinks the type of my first lines within the cleanLines function is List<String>, but that by the second line it has type Collection and by the third it expects type List<Object<E>>. I think that String is an Object and so this might be okay, but it certainly hints at a misunderstanding on my part. What am I doing wrong? How can I get my test to pass here?
Here's a corrected script:
import groovy.transform.Field
#Field
def tab = '\t'
#Field
def returnCarriage = '\r'
#Field
def equals = '='
List <String> cleanLines(List <String> lines) {
lines = lines.findAll { it.contains(equals) }
lines = lines.collect { it.replaceAll('\\s+', '') }
lines = lines.collect { it.trim() }
}
def dirtyLines = [" Colour=Red",
"Shape=Square "]
def cleanedLines = ["Colour=Red", "Shape=Square"]
assert cleanLines(dirtyLines) == cleanedLines
In general findAll and collect are maybe not mutually exclusive but have different purposes. Use findAll to find elements that matches certain criteria, whereas collect when you need to process/transform the whole list.
this line
lines = lines.collect(){it.findAll(){c -> c != tab && c != returnCarriage}}`
replaces the original list of Strings with the list of lists. Hence NSME for ArrayList.trim(). You might want to replace findAll{} with find{}
You can clean the lines like this:
def dirtyLines = [" Colour=Red", "Shape=Square "]
def cleanedLines = ["Colour=Red", "Shape=Square"]
assert dirtyLines.collect { it.trim() } == cleanedLines
If you separate the first line of cleanLines() into two separate lines and print lines after each, you will see the problem.
it.findAll { c -> c != tab && c != returnCarriage }
will return a list of strings that match the criteria. The collect method is called on every string in the list of lines. So you end up with a list of lists of strings. I think what you are looking for is something like this:
def cleanLines(lines) {
return lines.findAll { it.contains(equals) }
.collect { it.replaceAll(/\s+/, '') }
}