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

[WIP]: wet/dry JSON transformation improvements #6

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion .eslintrc.yml
Expand Up @@ -180,7 +180,7 @@ rules:
no-useless-concat:
- error
no-useless-escape:
- error
- off
no-void:
- error
no-warning-comments:
Expand Down
145 changes: 145 additions & 0 deletions lib/configuration.js
@@ -0,0 +1,145 @@
/*
* Copyright 2016 Resin.io
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

/**
* @module Reconfix.Configuration
*/

const _ = require('lodash');
const State = require('./state');

/**
* @summary Generate configuration
* @function
* @public
*
* @param {Object} schema - schema
* @param {Object} state - state
* @returns {Object} configuration
*
* @example
* const configuration = Configuration.generate({
* config_json: {
* connector: {
* type: 'json'
* path: [ 'config.json' ],
* partition: {
* primary: 4,
* logical: 1
* }
* },
* properties: {
* network: {
* ssid: {
* type: [ 'string' ],
* mapping: [
* [ 'network', 'default', 'ssid' ]
* ]
* }
* }
* }
* }
* }, {
* network: {
* ssid: 'mynetwork'
* }
* });
*
* console.log(configuration);
* > {
* > config_json: {
* > network: {
* > default: {
* > ssid: "mynetwork"
* > }
* > }
* > }
* > }
*/
exports.generate = (schema, state) => {
return _.mapValues(schema, (entity) => {
return State.compile(entity.properties, state);
});
};

/**
* @summary Extract configuration
* @function
* @public
*
* @param {Object} schema - schema
* @param {Object} configuration - configuration
* @returns {Object} state
*
* @example
* const state = Configuration.extract({
* config_json: {
* connector: {
* type: 'json'
* path: [ 'config.json' ],
* partition: {
* primary: 4,
* logical: 1
* }
* },
* properties: {
* network: {
* ssid: {
* type: [ 'string' ],
* mapping: [
* [ 'network', 'default', 'ssid' ]
* ]
* }
* }
* }
* }
* }, {
* config_json: {
* network: {
* default: {
* ssid: "mynetwork"
* }
* }
* }
* });
*
* console.log(state);
* > {
* > tainted: [],
* > result: {
* > network: {
* > ssid: 'mynetwork'
* > }
* > }
* > }
*/
exports.extract = (schema, configuration) => {
return _.reduce(schema, (accumulator, entity, name) => {
try {
return _.merge(accumulator, {
result: State.decompile(entity.properties, _.get(configuration, name))
});
} catch (error) {
accumulator.tainted.push(name);
return accumulator;
}
}, {
tainted: [],
result: {}
});
};
125 changes: 125 additions & 0 deletions lib/connector.js
@@ -0,0 +1,125 @@
/*
* Copyright 2016 Resin.io
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

/**
* @module Reconfix.Connector
*/

const _ = require('lodash');

/**
* @summary Built-in connectors
* @type Object
* @constant
* @public
*/
exports.BUILTIN_CONNECTORS = {
json: require('./connectors/json')
};

/**
* @summary Get the type of a connector
* @function
* @private
*
* @param {Object} connector - connector
* @returns {String} type - type
*
* @example
* const type = Connector.getType({
* type: 'json',
* path: [ 'config.txt' ],
* partition: {
* primary: 1
* }
* });
*/
exports.getType = (connector) => {
return _.get(connector, 'type');
};

/**
* @summary Get the options of a connector
* @function
* @private
*
* @param {Object} connector - connector
* @returns {Object} options - options
*
* @example
* const options = Connector.getOptions({
* type: 'json',
* path: [ 'config.txt' ],
* partition: {
* primary: 1
* }
* });
*
* console.log(options);
* > {
* > path: [ 'config.txt' ],
* > partition: {
* > primary: 1
* > }
* > }
*/
exports.getOptions = (connector) => {
return _.omit(connector, [ 'type' ]);
};

/**
* @summary Set data using a connector
* @function
* @public
*
* @param {Object} connector - connector
* @param {Object} data - data
* @param {Object} options - options
* @param {Object} options.connectors - available connectors
* @returns {Promise}
*
* @example
* Connector.set({
* type: 'json',
* path: [ 'config.json' ],
* partition: {
* primary: 1
* }
* }, {
* foo: 'bar'
* }, {
* connectors: Connector.BUILTIN_CONNECTORS
* }).then(() => {
* console.log('Done!');
* });
*/
exports.set = (connector, data, options) => {
const type = exports.getType(connector);
const executor = _.get(options.connectors, [ type, 'set' ]);

if (!_.has(options.connectors, type)) {
throw new Error(`Unknown connector type: "${type}"`);
}

if (!_.isFunction(executor)) {
throw new Error(`Invalid connector type: "${type}", "set" is not a function`);
}

const connectorOptions = exports.getOptions(connector);
return executor(connectorOptions, data);
};
62 changes: 62 additions & 0 deletions lib/connectors/formats/json.js
@@ -0,0 +1,62 @@
/*
* Copyright 2016 Resin.io
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

/**
* @module Reconfix.Connectors.Formats.Json
*/

const _ = require('lodash');

/**
* @summary Decode the contents of a JSON string
* @function
* @public
*
* @param {String} string - json string
* @returns {Object} object
*
* @example
* const object = Json.decode('{"foo":"bar"}');
* console.log(object.foo);
* > 'bar'
*/
exports.decode = _.unary(JSON.parse);

/**
* @summary Encode a JSON object as an JSON string
* @function
* @public
*
* @param {Object} object - object
* @returns {String} json string
*
* @example
* const string = json.encode({
* mysection: {
* foo: 'bar'
* }
* });
*
* console.log(string);
* > {
* > mysection: {
* > foo: 'bar'
* > }
* > }
*/
exports.encode = _.partialRight(JSON.stringify, null, 2);
33 changes: 33 additions & 0 deletions lib/connectors/json.js
@@ -0,0 +1,33 @@
/*
* Copyright 2016 Resin.io
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

/**
* @module Reconfix.Connectors.Json
*/

const Bluebird = require('bluebird');
const Json = require('./formats/json');

exports.set = (options, data) => {
console.log('Writing...');
console.log('Options:');
console.log(Json.encode(options));
console.log('Data:');
console.log(Json.encode(data));
return Bluebird.resolve();
};