Skip to content

Commit

Permalink
fix: 🐛 works in real life
Browse files Browse the repository at this point in the history
  • Loading branch information
touv committed Feb 28, 2020
1 parent 154f1f3 commit b5c1fbf
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 30 deletions.
38 changes: 38 additions & 0 deletions packages/transformers/examples/convert-LODEX-model-to-ezs.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#
# Tranforme un model lodex en script ezs
# permet de récupérer les transformations
# pour les rejouer à sans lodex
#
# La transformatino est brute, cad
# qu'une foi le fichier généré il est necessaire
# - de supprimer la ligne contenant AUTGENERATE_URI (qui n'a de sens que dans lodex)
# - d'ajouter l'instruction :
# [exchange]
# value = omit('$origin')
# en fin de script poiur éviter d'avoir le champ technique $origin dans son fichier résulat
# 
# Exemple d'usage complet
# Etape 1: on convertit le model en fichier ezs
#  ezs ./convert-LODEX-model-to-ezs.ini < ./modele-lodex.json > ./transformers.ini
#
# Etape 2 : on converit un fichier zip (dl.istex.fr) en json (lodex)
# ezs ./zip.ini ./transformers.ini ./finalize.ini < istex-subset-2020-01-09.zip > output.json

[use]
plugin = basics
plugin = analytics

[JSONParse]

[exploding]
id = name
value = transformers

[replace]
path = get('value.operation').split('').unshift('[transformers:$').push(']').join('')
value = get('value.args').reduce((o, v) => (o[v.name] = String('fix(').concat(JSON.stringify(v.value)).concat(')') , o), {})

path = get('value.operation').split('').unshift('[transformers:$').push('].field').join('')
value = get('id')

[INIString]
8 changes: 8 additions & 0 deletions packages/transformers/examples/finalize.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#
# Script pratique à utiliser apres un fichier généré par ./convert-LODEX-model-to-ezs.ini
#
[exchange]
value = omit('$origin')

[dump]
indent = true
2 changes: 1 addition & 1 deletion packages/transformers/src/dollar-column.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ import dollar from './dollar';
* @returns {Object}
*/
export default function $COLUMN(data, feed) {
return dollar(this, data, feed, transformer, data);
return dollar(this, data, feed, transformer, true);
}
50 changes: 50 additions & 0 deletions packages/transformers/src/dollar-column.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,54 @@ describe('$COLUMN', () => {
done();
});
});

test('with valid parameter', (done) => {
const script = `
[$COLUMN]
field = d
column = b
[$COLUMN]
field = e
column = a
[exchange]
value = omit('$origin')
`;
const input = [
{ a: '1', b: 'un', c: true },
{ a: '2', b: 'deux', c: true },
{ a: '3', b: 'trois', c: false },
{ a: '4', b: 'quatre', c: true },
];
const output = [];
from(input)
.pipe(ezs('delegate', { script }))
.pipe(ezs.catch())
.on('error', done)
.on('data', (chunk) => {
expect(chunk).toEqual(expect.any(Object));
output.push(chunk);
})
.on('end', () => {
expect(output.length).toBe(4);
expect(output[0].d).toEqual('un');
expect(output[1].d).toEqual('deux');
expect(output[2].d).toEqual('trois');
expect(output[3].d).toEqual('quatre');
expect(output[0].e).toEqual('1');
expect(output[1].e).toEqual('2');
expect(output[2].e).toEqual('3');
expect(output[3].e).toEqual('4');
expect(output[0]).not.toHaveProperty('a');
expect(output[0]).not.toHaveProperty('b');
expect(output[1]).not.toHaveProperty('a');
expect(output[1]).not.toHaveProperty('b');
expect(output[2]).not.toHaveProperty('a');
expect(output[2]).not.toHaveProperty('b');
expect(output[3]).not.toHaveProperty('a');
expect(output[3]).not.toHaveProperty('b');
done();
});
});
});
2 changes: 1 addition & 1 deletion packages/transformers/src/dollar-concat-uri.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ import dollar from './dollar';
* @returns {Object}
*/
export default function $CONCAT_URI(data, feed) {
return dollar(this, data, feed, transformer, data);
return dollar(this, data, feed, transformer, true);
}
2 changes: 1 addition & 1 deletion packages/transformers/src/dollar-concat.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ import dollar from './dollar';
* @returns {Object}
*/
export default function $CONCAT(data, feed) {
return dollar(this, data, feed, transformer, data);
return dollar(this, data, feed, transformer, true);
}
54 changes: 28 additions & 26 deletions packages/transformers/src/dollar.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,42 @@
import _ from 'lodash';

export default async function dollar(statement, data, feed, transformer, doc) {
export default async function dollar(statement, data, feed, transformer, useAllfields) {
if (statement.isLast()) {
return feed.close();
}
const fields = statement.getParam('field');
const field = Array.isArray(fields) ? fields.shift() : fields;
const { args } = transformer.getMetas();
let index = -1;
const values = args.map((arg) => {
const vals = statement.getParam(arg.name);
if (Array.isArray(vals)) {
index += 1;
if (!statement.values) {
const { args } = transformer.getMetas();
let index = -1;
statement.values = args.map((arg) => {
const vals = statement.getParam(arg.name);
if (Array.isArray(vals)) {
index += 1;
return {
...arg,
value: vals[index],
};
}
return {
...arg,
value: vals[index],
value: vals,
};
}
return {
...arg,
value: vals,
};
});
let newData;
if (data.$origin) {
newData = { ...data };
} else {
newData = {
$origin: { ...data },
};
});
}
const input = doc || _.get(data, field, null);

const output = await transformer(input, values)(input);
const source = data.$origin || data;
const input = (useAllfields && source) || _.get(source, field) || _.get(data, field, null);
const output = await transformer(input, statement.values)(input);

_.set(newData, field, output);

return feed.send(newData);
if (!data.$origin) {
const target = {};
_.set(target, '$origin', source);
_.unset(target.$origin, field);
_.set(target, field, output);
return feed.send(target);
}
_.unset(data.$origin, field);
_.set(data, field, output);
return feed.send(data);
}
2 changes: 1 addition & 1 deletion packages/transformers/src/dollar.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import statements from '.';
ezs.use(statements);

describe('chaining transformer', () => {
test('with valid parameter', (done) => {
test('with to transformers', (done) => {
const script = `
[$PREFIX]
field = b
Expand Down

0 comments on commit b5c1fbf

Please sign in to comment.