-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
53 lines (50 loc) · 1.65 KB
/
index.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
'use strict';
var _ = require('lodash');
var etcdConfigs = module.exports = {
retrieve: function retrieve(etcdClient, path){
var req = etcdClient.getSync(path);
var objResult = {};
var arrResult = [];
var result = objResult;
_.forEach(req.body.node.nodes, function(n) {
// key is the node name without path
var subKey = n.key.slice(path.length + 1);
var v;
if (!n.dir) {
v = n.value;
// turn true and false to boolean
if (v === 'false' || v === 'true'){
v = v === 'true';
}
} else {
// if the node is a dir, then recursive
v = retrieve(etcdClient, n.key);
}
// we supposed that if the key is a number, the properties are in array format
if (isNaN(parseInt(subKey, 10))){
// normal key: value
objResult[subKey] = v;
result = objResult;
} else {
// v may be everything
// if dictionary name is number, then it will turns to something like [{}, {}] or [[],[]]
// but the same level properties or dictionaries should also be number, otherwise the final result will be decided by the last element.
arrResult.push(v);
result = arrResult;
}
});
return result;
},
retrieveMultiPath: function retrieveMultiPath(etcdClient, paths){
var result = {};
if (_.isArray(paths)){
// The latter value will overwrite the previous value, if they have the same key
_.forEach(paths, function(path){
_.assign(result, etcdConfigs.retrieve(etcdClient, path));
});
} else {
result = etcdConfigs.retrieve(etcdClient, paths);
}
return result;
}
};