-1

I have created an application to import CSV files, each CSV file that the application imports can contain different data, with new formats of the CSV files being added in the future. Rather than hard code each import I have created a definition in JSON that details each file. I have written a basic import class that imports each line from the files.

I would like to be able to perform small transformations / modifications to some of the data being imported. I store that transformation as

right(%data%,5)

Where %data% is the column of the CSV file i'm processing.

I started to write transformers and would process each column using the processor. Here is an example of the right transformer

public function transformRight($string,$numChars) {
   return substr($string,(-1 * abs($numChars)));
}

The issue i have now is that I want to perform more complex transformations - i want to be able to use multiple functions, i want to be able to concatenate values and take the last x characters of a string.

I would write a transform method for each unique type of transformation I want to make but that doesn't seem good practice / very efficient. I have been looking at creating a lexer / parser but everything i look at seems way to complex for the simple thing i want to create. I would store the transformation as

trim(concat('ABC',right(%data%,5)))

Would using a regex be a better way ? or lexer / parser ? or something i havent looked at ?

  • It sounds like you want a domain specific language (DSL) for transforming your data. – BobDalgleish Feb 14 at 14:19
  • You can write a DSL and incorporate it somewhere; you can use regular expressions in some tool; you can also write the transformations as code in an existing programming language -- sometimes that's the easiest. – Erik Eidt Feb 14 at 23:03
  • Why did get downvoted ? i posted this question on stackoverflow and it got closed as too broad, so i asked here - I didnt expect it to get a million up votes ... but a downvote and no comment ... what is wrong with people ?! The attitude of some people on these sites actually discourages people from using them – ManseUK Feb 15 at 9:08
0

I found a Symfony Component called Expression Language - does exactly what i wanted - easy to add custom expressions too

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.