Skip to content

Commit

Permalink
Only ignore embedded JSON/YAML/properties file extensions
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
benwebber committed Dec 7, 2016
1 parent 0e572dc commit 025abf1
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions lib/consul/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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('/');
Expand Down

0 comments on commit 025abf1

Please sign in to comment.