Code coverage report for dynamoDb-marshaler/lib/marshalService.js

Statements: 100% (49 / 49)      Branches: 100% (10 / 10)      Functions: 100% (2 / 2)      Lines: 100% (49 / 49)      Ignored: none     

All files » dynamoDb-marshaler/lib/ » marshalService.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130    2     2 2 2 2 2 2 2 2     2 2 2 2 2 2     2                   2                                               2 3 3 3 3 3 3   3 18 18   18 1       3 2     2     1                             2 2 2 2 2 2   2 8 8   8 1       2 1 1     1               2        
'use strict';
 
var _ = require('lodash');
 
// Marshal commands
var marshalString = require('./commands/marshalString');
var marshalNumber = require('./commands/marshalNumber');
var marshalBoolean = require('./commands/marshalBoolean');
var marshalNull = require('./commands/marshalNull');
var marshalStringSet = require('./commands/marshalStringSet');
var marshalNumberSet = require('./commands/marshalNumberSet');
var marshalList = require('./commands/marshalList');
var marshalMap = require('./commands/marshalMap');
 
// Unmarshal commands
var unmarshalPassThrough = require('./commands/unmarshalPassThrough');
var unmarshalNumber = require('./commands/unmarshalNumber');
var unmarshalNumberSet = require('./commands/unmarshalNumberSet');
var unmarshalNull = require('./commands/unmarshalNull');
var unmarshalMap = require('./commands/unmarshalMap');
var unmarshalList = require('./commands/unmarshalList');
 
// Command lists
var marshalCommandList = [
  marshalString,
  marshalNumber,
  marshalBoolean,
  marshalNull,
  marshalStringSet,
  marshalNumberSet,
  marshalList,
  marshalMap
];
var unmarshalCommandList = [
  unmarshalPassThrough,
  unmarshalNumber,
  unmarshalNumberSet,
  unmarshalNull,
  unmarshalMap,
  unmarshalList
];
 
/**
 * Uses the chain of command pattern to select the marshaling algorithm.
 * Once a command marshals the item it is assigned to the return data and
 * no further commands are called.
 *
 * Each command is passed a reference to this marshal function as well because
 * in the case of the "M" (Map) and "L" (List) DynamoDb types their subvalues
 * also need to be marshaled.
 *
 * input: {foo: 'bar'}
 * output: {M: {foo: {S: 'bar'}}}
 *
 * @param {*} item A value to marshal.
 * @returns {Object} A DynamoDb AttributeValue object.
 */
var marshal = function(item) {
  var i;
  var len = marshalCommandList.length;
  var command;
  var result;
  var errorMessage;
  var marshaledItem = {};
 
  for (i = 0; i < len; i++) {
    command = marshalCommandList[i];
    result = command(item, marshal);
 
    if (!_.isUndefined(result)) {
      break;
    }
  }
 
  if (_.isUndefined(result)) {
    errorMessage = 'Marshaling error: encountered ' +
      ((item) ? ('unexpected item ' + item) : 'empty value');
 
    throw new TypeError(errorMessage);
  }
 
  return _.assign(marshaledItem, result);
};
 
/**
 * Uses the chain of command pattern to select the unmarshaling algorithm to
 * use for the given AttributeValue object based on the type key. Once a
 * command handles the item, no further commands are called and the unmarshaled
 * data is returned.
 *
 * input: {M: {foo: {S: 'bar'}}}
 * output: {foo: 'bar'}
 *
 * @param {Object} item A DynamoDb formatted AttributeValue object.
 * @returns {*} Unmarshaled value.
 */
var unmarshal = function(item) {
  var i;
  var len = unmarshalCommandList.length;
  var command;
  var errorMessage;
  var unmarshaledItem;
 
  for (i = 0; i < len; i++) {
    command = unmarshalCommandList[i];
    unmarshaledItem = command(item, unmarshal);
 
    if (!_.isUndefined(unmarshaledItem)) {
      break;
    }
  }
 
  if (_.isUndefined(unmarshaledItem)) {
    errorMessage = 'Unmarshal error: encountered unexpected item ' + item;
    throw new TypeError(errorMessage);
  }
 
  return unmarshaledItem;
};
 
/**
 * marshalService
 *
 * @type {{marshal: Function, unmarshal: Function}}
 */
module.exports = {
  marshal: marshal,
  unmarshal: unmarshal
};