diff --git a/lib/cartodb/backends/template_maps.js b/lib/cartodb/backends/template_maps.js index 60a6de2c..5d856976 100644 --- a/lib/cartodb/backends/template_maps.js +++ b/lib/cartodb/backends/template_maps.js @@ -296,7 +296,7 @@ TemplateMaps.prototype.delTemplate = function(owner, tpl_id, callback) { // @param callback function(err) // TemplateMaps.prototype.updTemplate = function(owner, tpl_id, template, callback) { - + var self = this; template = templateDefaults(template); @@ -430,13 +430,15 @@ var _reNumber = /^([-+]?[\d\.]?\d+([eE][+-]?\d+)?)$/, _reCSSColorVal = /^#[0-9a-fA-F]{3,6}$/; function _replaceVars (str, params) { - //return _.template(str, params); // lazy way, possibly dangerous // Construct regular expressions for each param - Object.keys(params).forEach(function(k) { + Object.keys(params).forEach(function(k) { str = str.replace(new RegExp("<%=\\s*" + k + "\\s*%>", "g"), params[k]); }); return str; } +function isDictionary(val) { + return ( _.isObject(val) && !_.isArray(val) && !_.isFunction(val)); +} TemplateMaps.prototype.instance = function(template, params) { var all_params = {}; var phold = template.placeholders || {}; @@ -465,6 +467,11 @@ TemplateMaps.prototype.instance = function(template, params) { throw new Error("Invalid css_color value for template parameter '" + k + "': " + val); } } + else if ( type === 'dictionary' ) { + if (!isDictionary(val)) { + throw new Error("Invalid dictionary value for template parameter '" + k + "': " + val); + } + } else { // NOTE: should be checked at template create/update time throw new Error("Invalid placeholder type '" + type + "'"); @@ -475,8 +482,12 @@ TemplateMaps.prototype.instance = function(template, params) { // NOTE: we're deep-cloning the layergroup here var layergroup = JSON.parse(JSON.stringify(template.layergroup)); - if (layergroup.buffersize) { - layergroup.buffersize = _replaceVars(layergroup.buffersize, all_params); + if (typeof layergroup.buffersize === 'string') { + layergroup.buffersize = Number(_replaceVars(layergroup.buffersize, all_params)); + } else if (isDictionary(layergroup.buffersize)) { + Object.keys(layergroup.buffersize).forEach(function(k) { + layergroup.buffersize[k] = Number(_replaceVars(layergroup.buffersize[k], all_params)); + }); } for (var i=0; i'), assert: function (tile, callback) { assert.imageIsSimilarToFile(tile, this.fixturePath, IMAGE_TOLERANCE_PER_MIL, callback); } @@ -161,7 +161,7 @@ describe.only('buffer size per format for named maps', function () { format: 'png', placeholders: { buffersize: 128 }, fixturePath: './test/fixtures/buffer-size/tile-7.64.48-buffer-size-128.png', - template: createBufferSizeTemplate('named-custom-buffer-size'), + template: createBufferSizeTemplate('named-custom-buffer-size', '<%= buffersize %>'), assert: function (tile, callback) { assert.imageIsSimilarToFile(tile, this.fixturePath, IMAGE_TOLERANCE_PER_MIL, callback); } @@ -170,9 +170,10 @@ describe.only('buffer size per format for named maps', function () { desc: 'should get png tile using buffer-size 0 (default value in template by format)', coords: { z: 7, x: 64, y: 48 }, format: 'png', + placeholders: { buffersize_png: 0 }, fixturePath: './test/fixtures/buffer-size/tile-7.64.48-buffer-size-0.png', - template: createBufferSizeTemplate('named-default-buffer-size-by-format', { - png: '<%= buffersize_png %>' + template: createBufferSizeTemplate('named-default-buffer-size-by-format', { + png: '<%= buffersize_png %>' }, { "buffersize_png": { "type": "number", @@ -187,10 +188,10 @@ describe.only('buffer size per format for named maps', function () { desc: 'should get png tile using buffer-size 128 (placehoder value in template by format)', coords: { z: 7, x: 64, y: 48 }, format: 'png', - placeholders: { buffersize: 128 }, + placeholders: { buffersize_png: 128 }, fixturePath: './test/fixtures/buffer-size/tile-7.64.48-buffer-size-128.png', - template: createBufferSizeTemplate('named-custom-buffer-size-by-format', { - png: '<%= buffersize_png %>' + template: createBufferSizeTemplate('named-custom-buffer-size-by-format', { + png: '<%= buffersize_png %>' }, { "buffersize_png": { "type": "number", @@ -207,15 +208,16 @@ describe.only('buffer size per format for named maps', function () { it(test.desc, function (done) { var testClient = new TestClient(undefined, 1234, test.template); var coords = test.coords; - var options = { - format: test.format, - placeholders: test.placeholders - } + var options = { + format: test.format, + placeholders: test.placeholders + }; testClient.getNamedTile(coords.z, coords.x, coords.y, options, function (err, res, tile) { assert.ifError(err); // To generate images use: - // tile.save('./test/fixtures/buffer-size/tile-7.64.48-buffer-size-64.png'); - test.assert(tile, function () { + //tile.save('./test/fixtures/buffer-size/tile-7.64.48-buffer-size-0-test.png'); + test.assert(tile, function (err) { + assert.ifError(err); testClient.drain(done); }); }); diff --git a/test/support/test-client.js b/test/support/test-client.js index 578be296..3e3edf55 100644 --- a/test/support/test-client.js +++ b/test/support/test-client.js @@ -504,8 +504,8 @@ TestClient.prototype.getTile = function(z, x, y, params, callback) { else if (isMvt) { obj = new mapnik.VectorTile(z, x, y); obj.setDataSync(new Buffer(res.body, 'binary')); - } - + } + else { obj = JSON.parse(res.body); } @@ -711,7 +711,7 @@ TestClient.prototype.getNamedTile = function(z, x, y, params, callback) { } if (!params.placeholders) { - params.placeholder = {}; + params.placeholders = {}; } var urlNamed = '/api/v1/map/named'; @@ -843,8 +843,8 @@ TestClient.prototype.getNamedTile = function(z, x, y, params, callback) { else if (isMvt) { obj = new mapnik.VectorTile(z, x, y); obj.setDataSync(new Buffer(res.body, 'binary')); - } - + } + else { obj = JSON.parse(res.body); }