aspark
aspark

Reputation: 33

Groovy script overwriting first byte?

Hi so this string I'm creating and appending in groovy is somehow corrupting the first byte and I have no idea why this happening. Its the second string creation. In this script I'm making a query and the first one works but the second initialization somehow messes up the first byte in the string and I have to do a substring of an extra index (it's two because I'm initializing a comma). Any insight would be very appreciated!!

Note: I'm using mulesoft runtime 3.8.5 in Anypoint studio 6.4.4. Not sure if this is the reason but it is a candidate in my mind...

 flowVars.queryIds = "Id IN ("
 for (Integer i = 0; i < payload.size(); i++) {
     flowVars.queryIds += "\'" + payload[i].Id + "\',"
 }
  flowVars.queryIds = flowVars.queryIds.substring(0,flowVars.queryIds.size() - 1) + ")"

  //Assigning comma because a random byte is getting inserted and this makes that error explicit & deterministic
  flowVars.queryFields = ",";
  for (String key : payload[0].keySet()) {
      flowVars.queryFields += key + ",";
  }
  //Skipping over custom field isMatch
  flowVars.queryFields = flowVars.queryFields.substring(2, flowVars.queryFields.size() - 9);

  return payload

Upvotes: 1

Views: 56

Answers (2)

aspark
aspark

Reputation: 33

So I found out the reason this issue was happening is actually because the csv file I am parsing is corrupted (I thought it was mulesoft and was mistaken). This blog does a greater job explaining the issue than I could. Thanks for your review on the groovy code though Rdmueller! Is definitely a lot cleaner with your suggestions. https://medium.freecodecamp.org/a-quick-tale-about-feff-the-invisible-character-cd25cd4630e7

Upvotes: 1

rdmueller
rdmueller

Reputation: 11022

I can't reproduce your problem but since you use groovy, you can write your code a bit shorter:

flowVars.queryIds = "Id IN ("
flowVars.queryIds += payload.collect{"'${it.Id}'"}.join(", ")
flowVars.queryIds += ")"
flowVars.queryFields = payload[0].keySet().join(", ");

this should produce the same output in a more understandable way

Upvotes: 1

Related Questions