Skip to content

Commit

Permalink
fix(package): improve workaround for obj.toString
Browse files Browse the repository at this point in the history
add workaround for keySet/entrySet/size
add helpers 'walk'
add tests
  • Loading branch information
ToQoz committed Dec 14, 2015
1 parent b87f047 commit a45557f
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 14 deletions.
51 changes: 37 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,27 +79,50 @@ module.exports = function(template, payload, params, context) {
}
};

// API Gateway convert function to "{}" on toString.
// Workaround to followings
// When the tempalte is "$input.params" (= function)
// - AWS API Gateway returns "{}".
// - This is not org.apache.velocity's behaviour. It returns "$input.params".
// When the tempalte is "$input" or "$input"
// - AWS API Gateway returns "{}"
// - This is not org.apache.velocity's behaviour. It returns serialized hash.
var returnEmptyObject = function() { return "{}"; };
[
data.input,
data.input.params,
data.input.path,
data.input.json,
data.util,
data.util.escapeJavaScript,
data.util.urlEncode,
data.util.urlDecode,
data.util.base64Encode,
data.util.base64Decode,
].forEach(function(f) {
f.toString = returnEmptyObject;

// This never be called because velocity.js will process.
// But I want to handling only toString
var builtinMethod = function() { throw "unexpected error"; };
builtinMethod.toString = returnEmptyObject;

data.input.toString = returnEmptyObject;
data.util.toString = returnEmptyObject;
walk(data, function(obj) {
if (typeof(obj) == 'function') {
obj.toString = returnEmptyObject;
}

obj.keySet = builtinMethod;
obj.entrySet = builtinMethod;
obj.size = builtinMethod;
});

var ast = Velocity.parse(template.toString());
return (new Velocity.Compile(ast)).render(data);
};

function walk(obj, cb) {
cb(obj);

if (Array.isArray(obj)) {
obj.forEach(function(c) {
walk(c, cb);
});
} else if ({}.toString.call(obj) === '[object Object]') {
Object.keys(obj).forEach(function(k) {
walk(obj[k], cb);
});
}
}

function base64Encode(x) {
return (new Buffer(x)).toString('base64');
}
Expand Down
10 changes: 10 additions & 0 deletions misc/gen.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,16 @@ var post = require('./post');
payload: "",
headers: {},
},
{
template: '$input.keySet',
payload: "",
headers: {},
},
{
template: '$input.params.keySet',
payload: "",
headers: {},
},
{
template: '$util',
payload: "",
Expand Down
16 changes: 16 additions & 0 deletions test/_.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,22 @@ describe('$input.path|$input.json', function() {
assert.deepEqual(expected, actual);
});
});
// https://github.com/ToQoz/api-gateway-mapping-template/blob/master/test/_.md#example-ec2bf3b4
describe('H=`{}` P=`` ===> T=`$input.keySet`', function() {
it('return ', function() {
var expected = {};
var actual = JSON.parse(mappingTemplate("$input.keySet", "", {}));
assert.deepEqual(expected, actual);
});
});
// https://github.com/ToQoz/api-gateway-mapping-template/blob/master/test/_.md#example-3e03488f
describe('H=`{}` P=`` ===> T=`$input.params.keySet`', function() {
it('return ', function() {
var expected = {};
var actual = JSON.parse(mappingTemplate("$input.params.keySet", "", {}));
assert.deepEqual(expected, actual);
});
});
// https://github.com/ToQoz/api-gateway-mapping-template/blob/master/test/_.md#example-a35f2355
describe('H=`{}` P=`` ===> T=`$util`', function() {
it('return ', function() {
Expand Down
10 changes: 10 additions & 0 deletions test/_.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,16 @@ Template|Header|Payload|Status code|Result
--------|------|-------|-----------|------
`$input`|`None`|``|`200`|`{}`

## example-ec2bf3b4
Template|Header|Payload|Status code|Result
--------|------|-------|-----------|------
`$input.keySet`|`None`|``|`200`|`{}`

## example-3e03488f
Template|Header|Payload|Status code|Result
--------|------|-------|-----------|------
`$input.params.keySet`|`None`|``|`200`|`{}`

## example-a35f2355
Template|Header|Payload|Status code|Result
--------|------|-------|-----------|------
Expand Down

0 comments on commit a45557f

Please sign in to comment.