Created
February 18, 2013 08:51
-
-
Save mattpowell/4976003 to your computer and use it in GitHub Desktop.
restli - While working w/ some rest.li services I found I needed to translate some of their data-structs to more usable/logical components... in JavaScript. This is what I have so far and it has been serving me well; however, I will concede that there may be a better approach :) Either case, creating this gist allows me to `npm install` without …
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
TODO: these come back from restli requests... should we do anything w/ them? | |
X-LinkedIn-Type: com.linkedin.restli.common.ActionResponse | |
X-LinkedIn-Sub-Type: com.linkedin.janus.rest.api.JsonContentModel | |
*/ | |
var keys = { | |
'0': 'string', | |
'1': 'int', | |
'2': 'long', | |
'5': 'boolean', | |
'10': 'com.linkedin.janus.rest.api.List', | |
'11': 'com.linkedin.janus.rest.api.Map', | |
'13': 'com.linkedin.janus.rest.api.R2ContentModel', | |
'21': 'com.linkedin.janus.rest.api.Location', | |
'23': 'com.linkedin.janus.rest.api.GeoLocation', | |
'26': 'com.linkedin.janus.rest.api.MonthYear', | |
'27': 'com.linkedin.janus.rest.api.Duration', | |
'28': 'com.linkedin.janus.rest.api.RichText', | |
'31': 'com.linkedin.janus.rest.api.Locale', | |
'32': 'com.linkedin.janus.rest.api.R2ContentMap', | |
'company': 'company', | |
'positions': 'positions', | |
'memberArticleSet': 'memberArticleSet', | |
'miniProfile': 'miniProfile', | |
'multipleMiniProfiles': 'multipleMiniProfiles', | |
'multipleMiniProfilePictures': 'multipleMiniProfilePictures', | |
'M': 'M' | |
}; | |
var fns = { | |
'string': function(obj) { | |
return obj.element['string']; | |
}, | |
'int': function(obj) { | |
return obj.element['int']; | |
}, | |
'long': function(obj) { | |
return obj.element['long']; | |
}, | |
'boolean': function(obj) { | |
return obj.element['boolean']; | |
}, | |
'positions': function(obj) { | |
return obj.contentMap.mapField; | |
}, | |
'company': function(obj) { | |
return obj.contentMap.mapField; | |
}, | |
'M': function(obj) { | |
return obj.duration; //TODO: any math here? | |
}, | |
'memberArticleSet': function(obj) { | |
return obj.contentMap.article || {}; | |
}, | |
'miniProfile': function(obj) { | |
return obj.contentMap.mapField; | |
}, | |
'multipleMiniProfiles': function(obj) { | |
return obj.contentMap.mapField; | |
}, | |
'multipleMiniProfilePictures': function(obj) { | |
return obj.contentMap.mapField; | |
}, | |
'com.linkedin.janus.rest.api.List': function(obj) { | |
var visitedObj = obj | |
.element['com.linkedin.janus.rest.api.List'] | |
.elements | |
.map(function(element){ | |
return visit(element); | |
}); | |
return visitedObj; | |
}, | |
'com.linkedin.janus.rest.api.Map': function(obj) { | |
var visitedObj = obj.element['com.linkedin.janus.rest.api.Map'].element; | |
visitedObj = visitedObj.map(function(element) { | |
var newO = {}, | |
k = visit(element.key), | |
v = visit(element.value); | |
newO[k] = v; | |
return newO; | |
}); | |
return visitedObj; | |
}, | |
'com.linkedin.janus.rest.api.R2ContentModel': function(obj) { | |
return obj.element['com.linkedin.janus.rest.api.R2ContentModel'].base; | |
}, | |
'com.linkedin.janus.rest.api.Location': function(obj) { | |
return obj.element['com.linkedin.janus.rest.api.Location']; | |
}, | |
'com.linkedin.janus.rest.api.GeoLocation': function(obj) { | |
return obj.element['com.linkedin.janus.rest.api.GeoLocation']; | |
}, | |
'com.linkedin.janus.rest.api.MonthYear': function(obj) { | |
return obj.element['com.linkedin.janus.rest.api.MonthYear']; | |
}, | |
'com.linkedin.janus.rest.api.Duration': function(obj) { | |
return obj.element['com.linkedin.janus.rest.api.Duration']; | |
}, | |
'com.linkedin.janus.rest.api.RichText': function(obj) { | |
return obj.element['com.linkedin.janus.rest.api.RichText'].rawText; // also include s a field called rtData and is an array. was empty, not sure what to do w/ it. | |
}, | |
'com.linkedin.janus.rest.api.Locale': function(obj) { | |
return obj.element['com.linkedin.janus.rest.api.Locale']; | |
}, | |
'com.linkedin.janus.rest.api.R2ContentMap': function(obj) { | |
return obj.element['com.linkedin.janus.rest.api.R2ContentMap'].mapField; | |
} | |
}; | |
function visit(obj){ | |
var visitedObj = obj; | |
while (typeof(visitedObj.type) !== 'undefined' && typeof(visitedObj.type) !== 'object') { | |
try{ | |
visitedObj = fns[keys[visitedObj.type]](visitedObj); | |
}catch(unknownVisitor) { | |
console.log('unknown visitor',visitedObj); | |
throw unknownVisitor; | |
} | |
} | |
return visitedObj; | |
} | |
function transform(obj) { | |
var keys = typeof(obj) === 'object' && Object.keys(obj); | |
if (keys) { | |
keys.forEach(function(key) { | |
var o = obj[key], | |
visited = visit(o); | |
obj[key] = visited; | |
transform(visited); | |
}); | |
}else if (Array.isArray(obj)){ | |
obj = obj.map(function(element) { | |
transform(element); | |
return visit(element); | |
}); | |
} | |
return obj; | |
} | |
module.exports = { | |
transform: transform | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "restli", | |
"author": "Matt Powell", | |
"description": "tbd", | |
"main": "./index.js", | |
"version": "0.0.1" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Now, you can run this:
npm install https://gist.github.com/mattpowell/4976003/download