hapipal / confidence

Dynamic, declarative configurations
Other
263 stars 43 forks source link

Allow pulling values from criteria into store #74

Closed devinivy closed 5 years ago

devinivy commented 7 years ago

It would be nice to be able to take values in criteria and use them as parameters for store values. Here's an example demonstrating how it might look,

const store = new Confidence.Store({
    c: {
        $filter: 'size',
        big: { $param: 'maxSize' },
        small: 1,
        $default: 50
    }
});

store.get('/c', { size: 'big', maxSize: 200 }); // Evaluates to 200

If this sounds like a useful feature I would be happy to write some code for it. Let me know if you'd like more information about my use-case.

patrickkettner commented 7 years ago

I feel like this sort of logic is something you would do with the result of the store, outside of confidence itself. My reasoning being that it is sort of unbounded in possibilities - truncating strings, array values, etc. I am going to pass on this being a feature now, but anyone else feel free to voice support for it below. If there is enough support I am willing to reconsider.

devinivy commented 5 years ago

@augnin with #81 and #82 landing, which allows setting values from environment variables, I would like to re-propose this feature, which would allow setting values additionally from criteria.

Nargonath commented 5 years ago

I don't have such use case right now but this feature feels useful. Perhaps we could see to standardize the format with the recent changes @augnin proposed. If you want to use the env as value you'd do: $env.myEnv perhaps we could follow the same syntax for your params proposal @devinivy aka:

const store = new Confidence.Store({
    c: {
        $filter: 'size',
        big: '$params.maxSize',
        small: 1,
        $default: 50
    }
});

store.get('/c', { size: 'big', maxSize: 200 }); // Evaluates to 200

or flip it onto its head and use the object notation for @augnin proposal with the environment variable: { $env: 'NODE_ENV' }

augnin commented 5 years ago

It probably makes sense to use object notation for env, with an additional $coerce operator. something like

{
    "mysql": {
        "$filter": "$env.NODE_ENV",
        "development": {
            "host": "127.0.0.1",
            "port": 3306,
            "user": "user",
            "password": "password"
        },
        "$default": {
            "host": { "$env" : "MYSQL_HOST" },
            "port": {
                "$env": "MYSQL_PORT",
                "$coerce": "number"
            },
            "user": { "$env": "MYSQL_USER" },
            "password": { "$env": "MYSQL_PASSWORD" }
        }
    }
}

the only problem with this is "$env.NODE_ENV" in filter is inconsistent with the remaining syntax.

Nargonath commented 5 years ago

Can't we use the same syntax in $filter? Allow for string or object so we don't break backward compat.

I have no clue if that's a big deal or not, I'm not really familiar with confidence codebase.