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

Custom Getter #313

Closed
A-312 opened this issue Aug 18, 2019 · 5 comments · Fixed by A-312/node-blueconfig#1
Closed

Custom Getter #313

A-312 opened this issue Aug 18, 2019 · 5 comments · Fixed by A-312/node-blueconfig#1

Comments

@A-312
Copy link
Contributor

A-312 commented Aug 18, 2019

We should allow to set custom getter to add more choice between env, arg and .load(.

Example: Custom Getter for file

const schema = {
  db: {
    name: {
      doc: 'Database name',
      format: String,
      default: 'users',
      getter: 'file',
      filepath: '/mycustompath/togetmy/name'
    }
  }
}

// convict.addGetter(name, priority, getter)
convict.addGetter('file', 70, (name, val, schema) => readFile(schema.filepath))

Custom priority level

And we should allow to set custom priority level : https://github.com/mozilla/node-convict/blob/master/lib/convict.js#L600-L601

Priority level Getter
0 Default value
20 File or json set in function argument
40 Environment variables (only used when env property is set in schema)
60 Command line arguments (only used when arg property is set in schema)
@A-312

This comment has been minimized.

@A-312

This comment has been minimized.

@A-312
Copy link
Contributor Author

A-312 commented Dec 7, 2019

Instead todo (6 loops) :

(Order : default, value (=.load or .loadFile), env, arg, set (= .set))

// loading schema

  1. default loop
  2. env loop
  3. arg loop

// .load or .loadFile

  1. value loop (-> json of .load or .loadFile)
  2. env loop
  3. arg loop

I will do (2 loops) :

// loading schema

  1. Check for the value with the most priority --> 1) arg, 2) env, 3) default and get the value. I will write a variable _cvtActualGetter in schema (by the getter name arg/env/default.

// .load or .loadFile

  1. value loop (-> json of .load or .loadFile) apply the value depending of the priority (saved in _cvtActualGetter) to run < priority(value) => 'default'.

_cvtActualGetter will be usefull for #260

@A-312
Copy link
Contributor Author

A-312 commented Dec 7, 2019

I referenced some issue to fix it in my PR (to keep them in my head).

@A-312
Copy link
Contributor Author

A-312 commented Dec 7, 2019

The actual prototype is : addGetter(property, getter, useCoerce, usedOnlyOnce, rewrite)

  • property : Name & keyname in schema
  • getter : function to get the value
  • usedOnlyOnce : boolean or function, if true will fire : throw new Error("'" + fullName + "' reuses '" + getterName + "' getter value: " + value + advice); ;
  • rewrite : If true you can rewrite a getter with the same name (property).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant