Skip to content

Commit

Permalink
Merge pull request #280 from zalando-incubator/web-ui-fix-boolean-env…
Browse files Browse the repository at this point in the history
…-variable

web-ui: fix boolean as string ENV variables parsing
  • Loading branch information
fmueller authored Apr 18, 2017
2 parents d01d232 + c3fcaa7 commit c7be360
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 3 deletions.
30 changes: 30 additions & 0 deletions web-ui/src/server/__tests__/util.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict';

const {stringToBool} = require('../util');


describe('server.util', () => {
describe('stringToBool', () => {
test('transform "false" values into real booleans while keeping other values as they are', () => {
expect(stringToBool({
foo: 'false',
bar: false
})).toEqual({
foo: false,
bar: false
});
});

test('transform "true" values into real booleans while keeping other values as they are', () => {
expect(stringToBool({
foo: 'true',
bar: 'true',
john: 'doe'
})).toEqual({
foo: true,
bar: true,
john: 'doe'
});
});
});
});
8 changes: 5 additions & 3 deletions web-ui/src/server/env.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
'use strict';

const {stringToBool} = require('./util');
const dotenvParseVariables = require('dotenv-parse-variables').default;
const dotenv = require('dotenv').config();
const dotenvParsedVariables = require('dotenv-parse-variables').default(dotenv.parsed || {});
const dotenvParsedVariables = dotenvParseVariables(dotenv.parsed || {});

const defaults = {
PORT: 8442,
Expand Down Expand Up @@ -39,11 +41,11 @@ const publicEnvKeys = [
'DEBUG'
];

const env = Object.assign(
const env = stringToBool(Object.assign(
defaults,
process.env,
dotenvParsedVariables
);
));

const publicEnv = publicEnvKeys.reduce((acc, key) => {
acc[key] = env[key];
Expand Down
26 changes: 26 additions & 0 deletions web-ui/src/server/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict';

/**
* Dummy function to transform object that contains
* boolean values expressed as strings (eg. 'true' or 'false')
* in real boolean values. Doesn't traverse the object tree.
*
* @param {Object} object
* @return {Object}
*/
function stringToBool (object) {
return Object.keys(object).reduce((acc, key) => {
if (object[key] === 'true') {
acc[key] = true;
} else if (object[key] === 'false') {
acc[key] = false;
} else {
acc[key] = object[key];
}
return acc;
}, {});
}

module.exports = {
stringToBool
};

0 comments on commit c7be360

Please sign in to comment.