Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Translate or morph hash keys to different names

by sundialsvc4 (Abbot)
on Nov 07, 2013 at 14:43 UTC ( #1061592=note: print w/ replies, xml ) Need Help??


in reply to Translate or morph hash keys to different names

I would simply define a package that “represents” the RESTful interface to the rest of the system(s), and, within that package, define a hash that maps old-names to new ones:

my $FIELD_MAP = { // LIST ALL SERVER-SUPPLIED FIELDS EVEN IF NAME DOESN'T CHANGE // SERVER'S NAME ... => OUR NAME 'incidentAbstract' => 'abstract', 'cross_reference' => 'crossReference, ... };

Then, simply iterate through the keys in this map to populate a new hash that has the preferred field-names in it.   (Since both hashes contain references to the data in most cases, there probably won’t be too-much data actually moving around in memory as you do this.)

(Notice the comment that I made in the code-block.   $FIELD_MAP is supposed to define every field-name that this program expects to deal with in the data, and I think that your code should die if it ever encounters a field-name that is not.   I think that code should never be “trustful,” especially of remote systems.   (Data-providers and data-consumers have bugs in their code all the time, just like we all do, so don’t let ’em grind you down...)

This approach will have a number of advantages:   it will encapsulate the interface on behalf of the rest of the system or systems, and it will provide a consistent (and well-documented in the code ...) naming convention that remains the same as before.   (If you have a number of inconsistent systems on your side, subclassing could be used to good effect.)   The package is also suspicious of its host, therefore trustworthy:   if it runs to completion, it must be doing the right thing.


Comment on Re: Translate or morph hash keys to different names
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1061592]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (8)
As of 2014-09-22 15:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (198 votes), past polls