From 025abf1c10f5d5991300c8810c580f115d6809e0 Mon Sep 17 00:00:00 2001 From: Ben Webber Date: Fri, 25 Nov 2016 21:57:24 -0500 Subject: [PATCH] Only ignore embedded JSON/YAML/properties file extensions Prior to this patch, setting `ignore_file_extension` had two unexpected effects: 1. Any path containing `.` would be truncated 2. Paths which did not include periods would simply be removed from the key/value store: ``` > var file = 'foo/bar/baz'; undefined > file = file.substr(0, file.lastIndexOf('.')); '' ``` This patch makes `ignore_file_extensions` less greedy. `create_key_name()` will only remove the extension when: 1. the extension matches `.json`, `.yaml`, `.yml`, or `.properties`, and 2. `expand_keys` is enabled. Closes #56. --- lib/consul/index.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/consul/index.js b/lib/consul/index.js index 578d773..1204933 100644 --- a/lib/consul/index.js +++ b/lib/consul/index.js @@ -10,6 +10,8 @@ var consul = require('consul')({'host': global.endpoint, 'port': global.port, 's var token = undefined; +const EXPAND_EXTENSIONS = ['json', 'yaml', 'yml', 'properties']; + // This makes life a bit easier for expand_keys mode, allowing us to check for a .json // extension with less code per line. String.prototype.endsWith = function(suffix) { @@ -60,9 +62,12 @@ var create_key_name = function(branch, file, ref) { file = file.substring(branch.source_root.length + 1); } - // Ignore file extensoion on demand - if (branch.ignore_file_extension) { - file = file.substr(0, file.lastIndexOf(".")); + // Remove extension from the key name when expanding an embedded document. + if (branch.ignore_file_extension && (branch.expand_keys || branch.expand_keys_diff)) { + var extension = file.substr(file.lastIndexOf('.') + 1); + if (_.contains(EXPAND_EXTENSIONS, extension)) { + file = file.substr(0, file.lastIndexOf('.')); + } } key_parts.push(file); return key_parts.join('/');