Skip to content

Commit f5820cb

Browse files
committed
Updated parser for configuration files and resources.
1 parent b6da30b commit f5820cb

7 files changed

Lines changed: 76 additions & 5 deletions

File tree

changes.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
- updated: `U.send(name, stream, url, callback, [cookies], [headers], [method], [timeout])` supports cookies and timeout
6161
- updated: `U.request()` supports a new flag `< 200` (kB), it means that the method stores a content with maxixmum size 200 kB.
6262
- updated: `ErrorBuilder` instance contains a new property `instance.unexpected` when is `instance.push()` a classic Error's instance.
63+
- updated: configuration files + resources support types like String, Number, Array, Date, etc. via `key (type) : value`
6364

6465
- fixed: Too many open files with `F.log()` and `F.logger()`
6566
- fixed: `String.isJSON()` the problem with `\n` character

index.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8413,6 +8413,14 @@ Framework.prototype._configure = function(arr, rewrite) {
84138413
continue;
84148414

84158415
var value = str.substring(index + 1).trim();
8416+
var subtype;
8417+
8418+
index = name.indexOf('(');
8419+
if (index !== -1) {
8420+
subtype = name.substring(index + 1, name.indexOf(')')).trim().toLowerCase();
8421+
name = name.substring(0, index).trim();
8422+
}
8423+
84168424
switch (name) {
84178425
case 'default-cors-maxage':
84188426
case 'default-request-length':
@@ -8464,8 +8472,24 @@ Framework.prototype._configure = function(arr, rewrite) {
84648472
break;
84658473

84668474
default:
8467-
obj[name] = value.isNumber() ? framework_utils.parseInt(value) : value.isNumber(true) ? framework_utils.parseFloat(value) : value.isBoolean() ? value.toLowerCase() === 'true' : value;
8475+
8476+
if (subtype === 'string')
8477+
obj[name] = value;
8478+
else if (subtype === 'number' || subtype === 'currency' || subtype === 'float' || subtype === 'double')
8479+
obj[name] = value.isNumber(true) ? value.parseFloat() : value.parseInt();
8480+
else if (subtype === 'boolean' || subtype === 'bool')
8481+
obj[name] = value.parseBoolean();
8482+
else if (subtype === 'eval' || subtype === 'object' || subtype === 'array')
8483+
obj[name] = new Function('return ' + value)();
8484+
else if (subtype === 'json')
8485+
obj[name] = value.parseJSON();
8486+
else if (subtype === 'date' || subtype === 'datetime' || subtype === 'time')
8487+
obj[name] = value.parseDate();
8488+
else
8489+
obj[name] = value.isNumber() ? framework_utils.parseInt(value) : value.isNumber(true) ? framework_utils.parseFloat(value) : value.isBoolean() ? value.toLowerCase() === 'true' : value;
8490+
84688491
break;
8492+
84698493
}
84708494
}
84718495

test/config-debug

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
etag-version : 1
2-
secret : total.js test
2+
secret : total.js test
3+
array (Array) : [1, 2, 3, 4]

test/config-release

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
etag-version : 1
2-
secret : total.js test
2+
secret : total.js test
3+
array (Array) : [1, 2, 3, 4]

test/test-framework-debug.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ framework.on('ready', function() {
2929
t.on('exit', function() {
3030
assert.ok(a === true, 'F.load() in worker');
3131
});
32+
33+
assert.ok(F.config.array.length === 4, 'Problem with config sub types.');
3234
});
3335

3436
framework.onAuthorize = function(req, res, flags, cb) {

test/test-framework-release.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ framework.on('ready', function() {
2929
t.on('exit', function() {
3030
assert.ok(a === true, 'F.load() in worker');
3131
});
32+
33+
assert.ok(F.config.array.length === 4, 'Problem with config sub types.');
3234
});
3335

3436
framework.onAuthorize = function(req, res, flags, cb) {

utils.js

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2860,7 +2860,47 @@ String.prototype.parseConfig = function(def) {
28602860
continue;
28612861
}
28622862

2863-
obj[str.substring(0, index).trim()] = str.substring(index + 2).trim();
2863+
var name = str.substring(0, index).trim();
2864+
var value = str.substring(index + 2).trim();
2865+
var subtype;
2866+
2867+
index = name.indexOf('(');
2868+
if (index !== -1) {
2869+
subtype = name.substring(index + 1, name.indexOf(')')).trim().toLowerCase();
2870+
name = name.substring(0, index).trim();
2871+
}
2872+
2873+
switch (subtype) {
2874+
case 'string':
2875+
obj[name] = value;
2876+
break;
2877+
case 'number':
2878+
case 'float':
2879+
case 'double':
2880+
case 'currency':
2881+
obj[name] = value.isNumber(true) ? value.parseFloat() : value.parseInt();
2882+
break;
2883+
case 'boolean':
2884+
case 'bool':
2885+
obj[name] = value.parseBoolean();
2886+
break;
2887+
case 'eval':
2888+
case 'object':
2889+
case 'array':
2890+
obj[name] = new Function('return ' + value)();
2891+
break;
2892+
case 'json':
2893+
obj[name] = value.parseJSON();
2894+
break;
2895+
case 'date':
2896+
case 'time':
2897+
case 'datetime':
2898+
obj[name] = value.parseDate();
2899+
break;
2900+
default:
2901+
obj[name] = value;
2902+
break;
2903+
}
28642904
}
28652905

28662906
return obj;
@@ -3326,7 +3366,7 @@ String.prototype.isNumber = function(isDecimal) {
33263366
var self = this;
33273367
var length = self.length;
33283368

3329-
if (length === 0)
3369+
if (!length)
33303370
return false;
33313371

33323372
isDecimal = isDecimal || false;

0 commit comments

Comments
 (0)