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

Plugins - now allow falsy default values. #7071

Merged
merged 3 commits into from
Dec 13, 2019

Conversation

neverendingqs
Copy link
Contributor

@neverendingqs neverendingqs commented Dec 8, 2019

What did you implement

Previously, the plugin manager did not properly set the default value if the default value is 0, false, or ''. This PR fixes that.

Closes #XXXXX (none that I can find)

How can we verify it

serverless.yml:

service: foo

provider:
  name: aws
  runtime: nodejs10.x

plugins:
  - serverless-test-plugin

.serverless_plugins/serverless-test-plugin.js:

'use strict';

class TestPlugin {
  constructor(serverless, options) {
    this.options = options;
    this.commands = {
      options: {
        usage: 'Options viewer',
        lifecycleEvents: ['options'],
        commands: {
          log: {
            usage: 'Log all options',
            options: {
              zero: { default: 0 },
              'empty-string': { default: '' },
              false: { default: false }
            },
            lifecycleEvents: ['log']
          }
        }
      }
    };

    this.hooks = {
      'options:log:log': () => this.log()
    };
  }

  log() {
    console.log('log()');
    console.log(JSON.stringify(this.options));
  }
}

module.exports = TestPlugin;

Then run

sls options log

Before:

log()
{}

After:

log()
{"zero":0,"empty-string":"","false":false}

Todos

Useful Scripts
  • npm run test:ci --> Run all validation checks on proposed changes
  • npm run lint:updated --> Lint all the updated files
  • npm run lint:fix --> Automatically fix lint problems (if possible)
  • npm run prettier-check:updated --> Check if updated files adhere to Prettier config
  • npm run prettify:updated --> Prettify all the updated files
  • Write and run all tests
  • Write documentation
  • Enable "Allow edits from maintainers" for this PR
  • Update the messages below

Is this ready for review?: YES
Is it a breaking change?: NO

@codecov-io
Copy link

codecov-io commented Dec 8, 2019

Codecov Report

Merging #7071 into master will increase coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #7071      +/-   ##
==========================================
+ Coverage   88.48%   88.48%   +<.01%     
==========================================
  Files         231      231              
  Lines        8478     8479       +1     
==========================================
+ Hits         7502     7503       +1     
  Misses        976      976
Impacted Files Coverage Δ
lib/classes/PluginManager.js 96.67% <100%> (ø) ⬆️
lib/plugins/aws/package/compile/functions/index.js 95.45% <0%> (-0.38%) ⬇️
...plugins/aws/package/compile/events/stream/index.js 98.79% <0%> (-0.02%) ⬇️
lib/plugins/aws/lib/naming.js 99.21% <0%> (ø) ⬆️
lib/plugins/aws/lib/validate.js 100% <0%> (+4%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update be8e64c...d8f4b5b. Read the comment docs.

@neverendingqs neverendingqs marked this pull request as ready for review December 8, 2019 21:39
@pmuens pmuens changed the title plugins - now allow falsy default values. Plugins - now allow falsy default values. Dec 9, 2019
Copy link
Contributor

@pmuens pmuens left a comment

Choose a reason for hiding this comment

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

Hey @neverendingqs thanks for working on this 👍

The code looks good so far. The only thing I'm concerned about is whether this change would break existing plugins which rely on the current behavior of falsy default values.

@medikoo what are your thoughts around that? 🤔

Copy link
Contributor

@medikoo medikoo left a comment

Choose a reason for hiding this comment

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

@neverendingqs great thanks for that PR. It's a very good call!.

I've suggested one improvement, so we handle it more naturally (where JS language is concerned)

lib/classes/PluginManager.js Outdated Show resolved Hide resolved
@medikoo medikoo added bug and removed enhancement labels Dec 9, 2019
@medikoo
Copy link
Contributor

medikoo commented Dec 9, 2019

The only thing I'm concerned about is whether this change would break existing plugins which rely on the current behavior of falsy default values.

Yes, technically if one put default: false as a way to not provide a default value, it'll break.
However I'd say it's quite a corner case, and language wise, fact that we didn't support falsy values as default looks like a bug (I'd see default as type of any and not a boolean, and for any, it's only null and undefined that should be treated as no input).

I would drive it in, as bug fix

@pmuens
Copy link
Contributor

pmuens commented Dec 10, 2019

Yes, technically if one put default: false as a way to not provide a default value, it'll break.
However I'd say it's quite a corner case, and language wise, fact that we didn't support falsy values as default looks like a bug (I'd see default as type of any and not a boolean, and for any, it's only null and undefined that should be treated as no input).

100% agree with that sentiment 👍 I'd also treat it as a bug rather than a feature.

@medikoo medikoo added this to the 1.60.0 milestone Dec 10, 2019
Copy link
Contributor

@medikoo medikoo left a comment

Choose a reason for hiding this comment

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

Thank you @neverendingqs, looks great!

@medikoo medikoo merged commit 7e0e903 into serverless:master Dec 13, 2019
@neverendingqs neverendingqs deleted the plugin/falsydefaults branch December 18, 2019 19:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants