Refactor the MSH changes
Contents
We now restructure the code a little to group the MSH changes nicely. This is good practice for learning how to write a well-structured Translator script.
We do the following:
- Create a function called AlterMSH with the argument of an MSH segment
- Cut the existing statements altering the MSH segment and move them into this function
- Then call the function
Your screen should look something like this:
We need to change Out.MSH to MSH. We can do that by hand, or use the search and replace functionality in the Translator editor. Another good trick is to return MSH at the end of the function. This way it’s easy to see the MSH segment before and after we have altered it.
The code should now look like this:
And just to emphasize the point about comparing input and output of a function:
Here is the up-to-date code:
require 'split' require 'zsegment' local function trace(a,b,c,d) return end function main(Data) local Orig = hl7.parse {vmd = 'transform.vmd', data = Data} if Orig:nodeName() == 'Catchall' then iguana.logInfo('Filtered '..Orig.MSH[9][1]..'^'..Orig.MSH[9][2]) return end local Out = hl7.message{vmd = 'transform.vmd', name = Orig:nodeName()} Out:mapTree(Orig) local Copy = zsegment.copyZSegments(Data, Out:S()) CheckTransform(Data:StripLastReturns(), Copy:StripLastReturns()) AlterMSH(Out.MSH) trace(Out) local DataOut = Out:S() DataOut = zsegment.copyZSegments(Data, DataOut) DataOut = DataOut:StripLastReturns() trace(DataOut) queue.push{data = DataOut} end function AlterMSH(MSH) MSH[3][1] = 'Acme' MSH[4][1] = 'Lab' return MSH end function CheckTransform(Orig, Copy) if Orig ~= Copy then trace(Orig) trace(Copy) error('Copy of HL7 message does not match the original') end end function string.StripLastReturns(S) -- strip return(s) "\r" & "\n" from the end of a string local i = #S while S:byte(i) == 10 or S:byte(i) == 13 do i = i - 1 end return S:sub(1,i) end
Continue: Use the diff module