2011-06-22 03:37:48 +08:00
|
|
|
var textParsers = require(__dirname + "/textParsers"),
|
|
|
|
binaryParsers = require(__dirname + "/binaryParsers");
|
2011-03-03 13:28:17 +08:00
|
|
|
|
2011-06-22 03:37:48 +08:00
|
|
|
var typeParsers = {
|
|
|
|
text: {},
|
|
|
|
binary: {}
|
2011-03-03 13:28:17 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
//the empty parse function
|
|
|
|
var noParse = function(val) {
|
2011-11-21 18:23:26 +08:00
|
|
|
return String(val);
|
2011-03-03 13:28:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//returns a function used to convert a specific type (specified by
|
|
|
|
//oid) into a result javascript type
|
2011-06-22 03:37:48 +08:00
|
|
|
var getTypeParser = function(oid, format) {
|
|
|
|
if (!typeParsers[format])
|
|
|
|
return noParse;
|
2011-03-03 13:28:17 +08:00
|
|
|
|
2011-06-22 03:37:48 +08:00
|
|
|
return typeParsers[format][oid] || noParse;
|
2011-03-03 13:28:17 +08:00
|
|
|
};
|
|
|
|
|
2012-03-22 11:36:18 +08:00
|
|
|
var setTypeParser = function(oid, format, parseFn) {
|
|
|
|
if(typeof format == 'function') {
|
|
|
|
parseFn = format;
|
|
|
|
format = 'text';
|
|
|
|
}
|
|
|
|
typeParsers[format][oid] = parseFn;
|
|
|
|
}
|
|
|
|
|
2011-06-22 03:37:48 +08:00
|
|
|
textParsers.init(function(oid, converter) {
|
2011-11-21 18:23:26 +08:00
|
|
|
typeParsers.text[oid] = function(value) {
|
|
|
|
return converter(String(value));
|
|
|
|
};
|
2011-06-22 03:37:48 +08:00
|
|
|
});
|
2011-03-03 13:28:17 +08:00
|
|
|
|
2011-06-22 03:37:48 +08:00
|
|
|
binaryParsers.init(function(oid, converter) {
|
|
|
|
typeParsers.binary[oid] = converter;
|
|
|
|
});
|
2011-04-29 22:39:00 +08:00
|
|
|
|
2011-03-03 13:28:17 +08:00
|
|
|
module.exports = {
|
2011-06-22 03:37:48 +08:00
|
|
|
getTypeParser: getTypeParser,
|
2012-03-22 11:36:18 +08:00
|
|
|
setTypeParser: setTypeParser
|
2011-03-03 13:28:17 +08:00
|
|
|
}
|