Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: cast environment variable values to string #218

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 18 additions & 4 deletions package/lib/compileFunctions.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ module.exports = {
'nodejs8';
funcTemplate.properties.timeout =
_.get(funcObject, 'timeout') || _.get(this, 'serverless.service.provider.timeout') || '60s';
funcTemplate.properties.environmentVariables = _.merge(
{},
_.get(this, 'serverless.service.provider.environment'),
funcObject.environment // eslint-disable-line comma-dangle
funcTemplate.properties.environmentVariables = _.transform(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'll be nicer to use _.mapValues

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, I initially used that approach, but thought that it would be useful to have the key name in the error message as well so the user would know which key was configured incorrectly. I wasn't sure how to easily access keys from mapValues for the error message, so I went with transform.

If including the env var name feels unnecessary, I can update to use mapValues for sure

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In mapValues callback name is provided as second argument, and it's cleaner as you just return coerced value and do not need to overwrite it manually

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah okay. sorry about my confusion there - I had misread the docs. Thanks for talking me through this. Just pushed an update.

_.merge({}, _.get(this, 'serverless.service.provider.environment'), funcObject.environment),
(result, value, key) => coerceEnvOrError(result, key, value),
{}
);
funcTemplate.accessControl.gcpIamPolicy.bindings = _.unionBy(
_.get(funcObject, 'iam.bindings'),
Expand Down Expand Up @@ -198,6 +198,20 @@ const validateIamProperty = (funcObject, functionName) => {
}
};

const coerceEnvOrError = (result, key, value) => {
if (typeof value === 'string') {
result[key] = value;
} else if (typeof value === 'number') {
result[key] = value.toString();
} else {
const errorMessage = [
`The value for environment variable ${key} is an unsupported type: ${typeof value}.`,
' Values must either be strings or numbers (which are coerced into strings at package time).',
].join('');
throw new Error(errorMessage);
}
};

const getFunctionTemplate = (funcObject, projectName, region, sourceArchiveUrl) => {
//eslint-disable-line
return {
Expand Down
66 changes: 66 additions & 0 deletions package/lib/compileFunctions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -370,12 +370,72 @@ describe('CompileFunctions', () => {
});
});

it('should fail setting environment variable due to unsupported type (bool)', () => {
googlePackage.serverless.service.functions = {
func1: {
handler: 'func1',
environment: {
TEST_VAR: true,
},
events: [{ http: 'foo' }],
},
};

expect(() => googlePackage.compileFunctions()).toThrow(Error);
});

it('should fail setting environment variable due to unsupported type (null)', () => {
googlePackage.serverless.service.functions = {
func1: {
handler: 'func1',
environment: {
TEST_VAR: null,
},
events: [{ http: 'foo' }],
},
};

expect(() => googlePackage.compileFunctions()).toThrow(Error);
});

it('should fail setting environment variable due to unsupported type (object)', () => {
googlePackage.serverless.service.functions = {
func1: {
handler: 'func1',
environment: {
dev: {
TEST_VAR: 'test',
},
},
events: [{ http: 'foo' }],
},
};

expect(() => googlePackage.compileFunctions()).toThrow(Error);
});

it('should fail setting environment variable from provider due to unsupported type (bool)', () => {
googlePackage.serverless.service.functions = {
func1: {
handler: 'func1',
events: [{ http: 'foo' }],
},
};
googlePackage.serverless.service.provider.environment = {
TEST_VAR: true,
};

expect(() => googlePackage.compileFunctions()).toThrow(Error);
});

it('should set the environment variables based on the function configuration', () => {
googlePackage.serverless.service.functions = {
func1: {
handler: 'func1',
environment: {
TEST_VAR: 'test',
INT_VAR: 1,
FLOAT_VAR: 3.141,
},
events: [{ http: 'foo' }],
},
Expand All @@ -393,6 +453,8 @@ describe('CompileFunctions', () => {
availableMemoryMb: 256,
environmentVariables: {
TEST_VAR: 'test',
INT_VAR: '1',
FLOAT_VAR: '3.141',
},
timeout: '60s',
sourceArchiveUrl: 'gs://sls-my-service-dev-12345678/some-path/artifact.zip',
Expand Down Expand Up @@ -421,6 +483,8 @@ describe('CompileFunctions', () => {
};
googlePackage.serverless.service.provider.environment = {
TEST_VAR: 'test',
INT_VAR: 1,
FLOAT_VAR: 3.141,
};

const compiledResources = [
Expand All @@ -435,6 +499,8 @@ describe('CompileFunctions', () => {
availableMemoryMb: 256,
environmentVariables: {
TEST_VAR: 'test',
INT_VAR: '1',
FLOAT_VAR: '3.141',
},
timeout: '60s',
sourceArchiveUrl: 'gs://sls-my-service-dev-12345678/some-path/artifact.zip',
Expand Down