Skip to content

Commit

Permalink
Merge 7dc8d5b into 8d5a358
Browse files Browse the repository at this point in the history
  • Loading branch information
Loïc Calvy committed Sep 26, 2016
2 parents 8d5a358 + 7dc8d5b commit 69574fa
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 28 deletions.
10 changes: 8 additions & 2 deletions src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import {
startsWith,
curry
} from 'lodash/fp';
import {stringify, parseValue} from './parse';
import {set$} from './util/etcd';
import createDebug from 'debug';

const debug = createDebug('squirrel');

const createAPI = (store, client) => {
const createAPI = (store, client, options = {cwd: '/'}) => {
const getBy = (index, key) => {
debug(`getBy: ${index} => ${key}`);
return store('indexes').then(getOr(null, [index, key, 'value']));
Expand All @@ -30,7 +31,12 @@ const createAPI = (store, client) => {
};

const set = (_path, value) => {
return set$(client, _path, value).toPromise();
_path = path.join(options.cwd, _path);
debug(`set => ${_path}`);
return set$(client, _path, stringify(value))
.pluck('node', 'value')
.map(parseValue)
.toPromise();
};

const _get = curry((_path, node) => {
Expand Down
3 changes: 2 additions & 1 deletion src/bin/watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import minimist from 'minimist';

import makeEtcdClient from './etcd';
import createWatcher$ from '../watch';
import {stringify} from '../parse';

const argz = minimist(process.argv.slice(2));

Expand All @@ -14,5 +15,5 @@ const client = makeEtcdClient(argz);

const watcher = client.watcher(pathETCD, null, {recursive: true});
createWatcher$(watcher).do(action =>
process.stdout.write(`${JSON.stringify(action, null, 4)}\n`)
process.stdout.write(`${stringify(action)}\n`)
).toPromise();
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const createSquirrel = options => {
save(node$),
indexBuilder
);
const api = createAPI(store, client);
const api = createAPI(store, client, options);

return {
...api,
Expand Down
38 changes: 23 additions & 15 deletions src/parse.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,35 @@
import {set, reduce, has} from 'lodash/fp';
import {get, set, reduce, has, pipe} from 'lodash/fp';

const parseNode = node => {
if (!has('key', node))
node = set('key', '/', node);
if (node.dir)
return set('nodes', (node.nodes || []).map(parseNode), node);
export const stringify = value => JSON.stringify(value, null, 2);

export const parseValue = value => {
try {
return set('value', JSON.parse(node.value), node);
return JSON.parse(value);
} catch (err) {
return node;
return value;
}
};

const parseAction = action => {
export const parseNode = pipe(
node => { // Root node does't has 'key' key
if (has('key', node)) return node;
return set('key', '/', node);
},
node => { // Parse children of dir node
if (!node.dir) return node;
return set('nodes', (node.nodes || []).map(parseNode), node);
},
node => { // Parse value of file node
if (!has('value', node)) return node;
return set('value', parseValue(get('value', node)), node);
}
);

export const parseAction = action => {
return reduce((action, key) => {
if (has(key, action)) {
return set(key, parseNode(action[key]), action);
return set(key, parseNode(get(key, action)), action);
}
return action;
}, action, ['node', 'prevNode']);
};

export {
parseNode,
parseAction
};
3 changes: 1 addition & 2 deletions src/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ import {writeFile} from 'fs';
import {Observable} from 'rxjs';
import {isString} from 'lodash/fp';
import createDebug from 'debug';
import {stringify} from './parse';
const debug = createDebug('squirrel:save');

const writeFile$ = Observable.bindNodeCallback(writeFile);

const stringify = obj => JSON.stringify(obj, null, 2);

const createSave = filePath => event$ => {
if (!isString(filePath)) return event$;

Expand Down
15 changes: 13 additions & 2 deletions src/test/api.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import test from 'ava';
import createEtcdMock from '../util/test/helpers/etcd';
import {stringify} from '../parse';

import createAPI from '../api';
const node = {
Expand Down Expand Up @@ -176,14 +177,24 @@ test('should get null if any node matches this path', async t => {
);
});

const createAction = (type, node) => ({
type,
node
});
const createNode = (key, value) => ({
key,
value: stringify(value),
prevNode: null
});

test('should set value if value setted', async t => {
const client = createEtcdMock({
set: [{
assert: (key, value) => {
t.deepEqual(key, '/foo');
t.deepEqual(value, {foo: 'baz'});
t.deepEqual(value, stringify({foo: 'baz'}));
},
values: [null, {foo: 'baz'}, null]
values: [null, createAction('set', createNode('/foo', {foo: 'baz'})), null]
}]
});
const api = createAPI(getStore, client);
Expand Down
22 changes: 17 additions & 5 deletions src/test/parse.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
import test from 'ava';
import {parseAction, parseNode} from '../parse';
import {stringify, parseValue, parseAction, parseNode} from '../parse';

test('should parse value as JSON', t => {
const expected = {
foo: 'foo'
};
t.deepEqual(parseValue(stringify(expected)), expected);
});

test('shouldn\'t parse value if ', t => {
const expected = 'foo';
t.deepEqual(parseValue(expected), expected);
});

test('should parseNode', t => {
const tests = [{
Expand All @@ -23,7 +35,7 @@ test('should parseNode', t => {
}, {
input: {
key: '/foo',
value: JSON.stringify({
value: stringify({
foo: 'foo'
})
},
Expand All @@ -39,7 +51,7 @@ test('should parseNode', t => {
dir: true,
nodes: [{
key: '/foo/bar',
value: JSON.stringify({
value: stringify({
foo: 'foo'
})
}]
Expand Down Expand Up @@ -81,7 +93,7 @@ test('should parseAction', t => {
action: 'foo',
node: {
key: '/foo',
value: JSON.stringify({
value: stringify({
foo: 'foo'
})
}
Expand All @@ -100,7 +112,7 @@ test('should parseAction', t => {
action: 'foo',
prevNode: {
key: '/foo',
value: JSON.stringify({
value: stringify({
foo: 'foo'
})
}
Expand Down

0 comments on commit 69574fa

Please sign in to comment.