Giter Club home page Giter Club logo

ycb's Introduction

Yahoo! Configuration Bundle

Build Status

YCB is a multi-dimensional configuration library that builds bundles from resource files describing a variety of values. The library allows applications to configure themselves based on multiple dimensions describing locations, languages, environments, etc.

Install

npm install ycb --save

Usage

import YCB from 'ycb';
const configArray = [
    {
        dimensions: [
            {
                environment: {
                    dev: null,
                    staging: null,
                    test: null,
                    prod: null,
                },
            },
            {
                device: {
                    desktop: null,
                    mobile: {
                        tablet: null,
                        smartphone: null,
                    },
                },
            },
        ],
    },
    {
        settings: ['main'],
        host: 'example.com',
        prefix: null,
    },
    {
        settings: ['environment:dev'],
        host: 'dev.example.com',
    },
    {
        settings: ['environment:staging,test'],
        host: 'stage.example.com',
    },
    {
        settings: ['device:smartphone'],
        prefix: 'm.',
    },
];

const ycbObj = new YCB.Ycb(configArray);
const computedConfig = ycbObj.read({ environment: 'dev' });

console.log(computedConfig.host); // dev.example.com

Scheduling Changes

We can schedule configuration changes ahead of time by defining an interval along with a config and using the time aware read method. For example the following program.

import YCB from 'ycb';
const configArray = [
    {
        dimensions: [
            {
                environment: {
                    dev: null,
                    staging: null,
                    test: null,
                    prod: null,
                },
            },
            {
                region: {
                    us: null,
                    ca: null,
                },
            },
        ],
    },
    {
        settings: ['main'],
        host: 'example.com',
    },
    {
        settings: {
            dimensions: ['region:us'],
        },
        logo: 'logo.png',
    },
    {
        settings: {
            dimensions: ['region:us'],
            schedule: {
                start: '2019-11-28T00:04:00Z',
                end: '2019-11-29T00:04:00Z',
            },
        },
        logo: 'thanksgiving-logo.png',
    },
];

const ycbObj = new YCB.Ycb(configArray, { cacheInfo: true });
const config1 = ycbObj.readTimeAware({ region: 'us' }, 0);
const config2 = ycbObj.readTimeAware({ region: 'us' }, 1574899440000);
const config3 = ycbObj.readTimeAware({ region: 'us' }, 1574985840001);
console.log(config1);
console.log(config2);
console.log(config3);

will print

{ host: 'example.com',
  logo: 'logo.png',
  __ycb_expires_at__: 1574899440000 }
{ host: 'example.com',
  logo: 'thanksgiving-logo.png',
  __ycb_expires_at__: 1574985840001 }
{ host: 'example.com', logo: 'logo.png' }

These intervals are closed and either start or end may be omitted to define a one sided interval.

To support proper cache expiration one may set the cacheInfo option, in which case the next time the config will change is added to the returned object.

Examples

Examples are provided in the tests directory.

License

BSD see LICENSE.txt

ycb's People

Contributors

billdorn avatar caridy avatar daltomania avatar dependabot-preview[bot] avatar dependabot[bot] avatar drewfish avatar imalberto avatar kaesonho avatar lingyan avatar mridgway avatar okuryu avatar redonkulus avatar roderickhsiao avatar snyamathi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ycb's Issues

ycb doesn't do replacement for empty values

Here, if we've "filterUrl" as "", ycb doesn't replace it.

e.g
1)
[{"dimensions":[{"environment":{"development":{"dev":null,"test":null},"production":{"int":null,"stage":null,"prod":null}}}]},{"settings":["master"],"name":"highlander_client","config":{"baseUrl":"http://finance.yahoo.com","filterUrl":""},"dataprovider":{"Test YHOO Original 1":{"group":"smoke","params":{"page":"$$config.baseUrl$$$$config.filterUrl$$","test":"test.js"}}}}]

after ycb replacement translates to

{"name":"highlander_client","config":{"baseUrl":"http://finance.yahoo.com","filterUrl":""},"dataprovider":{"Test YHOO Original 1":{"group":"smoke","params":{"page":"$$config.baseUrl$$$$config.filterUrl$$","test":"test.js"}}}}

In case of non-empty values, replacement works good

[{"dimensions":[{"environment":{"development":{"dev":null,"test":null},"production":{"int":null,"stage":null,"prod":null}}}]},{"settings":["master"],"name":"highlander_client","config":{"baseUrl":"http://finance.yahoo.com","filterUrl":"/q?s=YHOO"},"dataprovider":{"Test YHOO Original 1":{"group":"smoke","params":{"page":"$$config.baseUrl$$$$config.filterUrl$$","test":"test.js"}}}}]

after ycb replacement translates to

{"name":"highlander_client","config":{"baseUrl":"http://finance.yahoo.com","filterUrl":"/q?s=YHOO"},"dataprovider":{"Test YHOO Original 1":{"group":"smoke","params":{"page":"http://finance.yahoo.com/q?s=YHOO","test":"test.js"}}}}

merging to an undefined destination

For some reason, while debugging a mojito app using node-inspector, I found a weird situation where to was undefined, and to[key] = ... was triggering an error that was silenced. Probably we should add guarding for that to avoid doing extra computations when the destination does not exists.

    function objectMerge(from, to) {
        var key;
        for (key in from) {
            if (from.hasOwnProperty(key)) {
                try {
                    // Property in destination object set; update its value.
                    if (from[key].constructor === Object) {
                        to[key] = objectMerge(from[key], to[key]);
                    } else {
                        to[key] = from[key];
                    }
                } catch(err) {
                    // Property in destination object not set; create it and set its value.
                    to[key] = from[key];
                }
            }
        }
        return to;
    }

Settings under unknown dimension value overwrite master settings

I notice that if I add some settings under an dimension value not listed in dimension.json, it overwrites the value in master. I can use this simple example to reproduce:

If I add this:

  {
    "settings": [ "environment:stage" ],
    "appPort": 81
  }

stage is not listed in dimension.json. It overwrites the "appPort": 8666 in master and the test fails. Is it an intended behavior?

The current README example is erroneous

1

There is a comma missing between this two items:

{
    settings: ["environment:dev"],
    host: "dev.example.com"
}
{
    settings: ["environment:staging,test"],
    host: "stage.example.com"
},

2

After correcting that, the code breaks with ...

            this._dimensionHierarchies[name] = this._calculateHierarchy(DEFAULT_LOOKUP, this.dimensions[pos][name]);
                                                                                                            ^

TypeError: Cannot read property 'device' of undefined
    at Object.Ycb._calculateHierarchies (/Users/rose119/Sites/ycb-webpacked/node_modules/ycb/index.js:591:109)
    at Object.Ycb._processRawBundle (/Users/rose119/Sites/ycb-webpacked/node_modules/ycb/index.js:437:22)
    at Object.Ycb (/Users/rose119/Sites/ycb-webpacked/node_modules/ycb/index.js:76:10)
    at Object.<anonymous> (/Users/rose119/Sites/ycb-webpacked/index.js:41:14)
    at Module._compile (module.js:413:34)
    at Object.Module._extensions..js (module.js:422:10)
    at Module.load (module.js:357:32)
    at Function.Module._load (module.js:314:12)
    at Function.Module.runMain (module.js:447:10)
    at startup (node.js:139:18)

Improve the documentation

Is it possible to improve the README a bit to include the bundle specifications and possibly some examples? I know the tests covers some of the examples, but it would be really nice to cover these in the documentation.

better user-focused logging

The current "debug" logging in YCB is very much oriented towards the team who is authoring YCB. Instead, users of YCB might benefit from a logging approach which helps them understand how YCB is processing/interpretting their config files.

Support multi-value dimensions in a single config block

We often find ourselves copying and pasting blocks of configs such as:

[
    {
        settings: ['region:US'],
        value: 'foo'
    },
    {
        settings: ['region:CA'],
        value: 'foo'
    }
]

It would be convenient to be able to collapse these duplicated sections together like so:

[
    {
        settings: ['region:CA,US'],
        value: 'foo'
    }
]

YCB should not use YUI

YCB, as today, is a server side only component, you can corroborate this by looking at index.js, which uses require. By looking at the code, there are only 3 YUI features used by this:

  • Y.JSON., which could be replaced with JSON.
  • Y.Object.values, to iterate, which could be done by just using in
  • Y.Array.forEach, to iterate, which could be done by just using for

Using YUI on the server, in its current form, is expensive, and in this particular case, it looks like a waste.

further ycb objects using same config return an empty object

If two Ycb objects are created with the same data, read calls on the second ycb object always return an empty object, instead of a resolved configuration bundle.

To reproduce, add these lines to the end of https://github.com/yahoo/ycb/blob/master/examples/simple/app.js

var foo = new libycb.Ycb(data, {});
config = foo.read({});
assert.equal(8666, config.appPort);

node examples/simple/app will result in:
AssertionError: 8666 == "undefined" because the output of foo.read({}) is {}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.