Skip to content
This repository was archived by the owner on Sep 6, 2021. It is now read-only.

Commit 79528b7

Browse files
committed
refactor worker code
1 parent a02eefd commit 79528b7

File tree

2 files changed

+10
-256
lines changed

2 files changed

+10
-256
lines changed

lib/workers/api_body_title.js

Lines changed: 5 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
var semver = require('semver');
2-
var WorkerError = require('../errors/worker_error');
1+
var apiWorker = require('./api_param_title.js');
32

43
// Additional information for error log
54
var _messages = {
@@ -16,36 +15,10 @@ var _messages = {
1615
* @param {Object[]} parsedFiles
1716
* @param {String[]} filenames
1817
* @param {Object} packageInfos
19-
* @param {String} target Target path in preProcess-Object (returned result), where the data should be set.
2018
* @returns {Object}
2119
*/
22-
function preProcess(parsedFiles, filenames, packageInfos, target) {
23-
target = target || 'defineParamTitle';
24-
var source = 'define'; // relative path to the tree (global.), from where the data should be fetched.
25-
26-
var result = {};
27-
result[target] = {};
28-
29-
parsedFiles.forEach(function(parsedFile) {
30-
parsedFile.forEach(function(block) {
31-
if (block.global[source]) {
32-
var name = block.global[source].name;
33-
var version = block.version || packageInfos.defaultVersion;
34-
35-
if ( ! result[target][name])
36-
result[target][name] = {};
37-
38-
// fetch from global
39-
result[target][name][version] = block.global[source];
40-
}
41-
});
42-
});
43-
44-
// remove empty target
45-
if (result[target].length === 0)
46-
delete result[target];
47-
48-
return result;
20+
function preProcess(parsedFiles, filenames, packageInfos) {
21+
return apiWorker.preProcess(parsedFiles, filenames, packageInfos, 'defineBodyTitle');
4922
}
5023

5124
/**
@@ -55,105 +28,9 @@ function preProcess(parsedFiles, filenames, packageInfos, target) {
5528
* @param {String[]} filenames
5629
* @param {Object[]} preProcess
5730
* @param {Object} packageInfos
58-
* @param {String} source Source path in preProcess-Object
59-
* @param {String} target Relative path to the tree (local.), where the data should be modified.
60-
* @param {String} messages
6131
*/
62-
function postProcess(parsedFiles, filenames, preProcess, packageInfos, source, target, messages) {
63-
source = source || 'defineParamTitle';
64-
target = target || 'parameter';
65-
messages = messages || _messages;
66-
67-
parsedFiles.forEach(function(parsedFile, parsedFileIndex) {
68-
parsedFile.forEach(function(block) {
69-
if ( ! block.local[target] || ! block.local[target].fields)
70-
return;
71-
72-
var newFields = {};
73-
var fields = block.local[target].fields;
74-
Object.keys(fields).forEach(function(fieldGroup) {
75-
var params = block.local[target].fields[fieldGroup];
76-
77-
params.forEach(function(definition) {
78-
var name = definition.group;
79-
var version = block.version || packageInfos.defaultVersion;
80-
var matchedData = {};
81-
82-
if ( ! preProcess[source] || ! preProcess[source][name]) {
83-
// TODO: Enable in the next version
84-
// At the moment the (groupname) is optional and must not be defined.
85-
/*
86-
var extra = [
87-
{ 'Groupname': name }
88-
];
89-
throw new WorkerError('Referenced groupname does not exist / it is not defined with @apiDefine.',
90-
filenames[parsedFileIndex],
91-
block.index,
92-
messages.common.element,
93-
messages.common.usage,
94-
messages.common.example,
95-
extra);
96-
*/
97-
// TODO: Remove in the next version
98-
matchedData.name = name;
99-
matchedData.title = name;
100-
101-
}
102-
103-
// TODO: Remove in the next version
104-
else {
105-
106-
if (preProcess[source][name][version]) {
107-
// found the version
108-
matchedData = preProcess[source][name][version];
109-
} else {
110-
// find nearest matching version
111-
var foundIndex = -1;
112-
var lastVersion = packageInfos.defaultVersion;
113-
114-
var versionKeys = Object.keys(preProcess[source][name]);
115-
versionKeys.forEach(function(currentVersion, versionIndex) {
116-
if (semver.gte(version, currentVersion) && semver.gte(currentVersion, lastVersion)) {
117-
lastVersion = currentVersion;
118-
foundIndex = versionIndex;
119-
}
120-
});
121-
122-
if (foundIndex === -1) {
123-
var extra = [
124-
{ 'Groupname': name },
125-
{ 'Version': version },
126-
{ 'Defined versions': versionKeys },
127-
];
128-
throw new WorkerError('Referenced definition has no matching or a higher version. ' +
129-
'Check version number in referenced define block.',
130-
filenames[parsedFileIndex],
131-
block.index,
132-
messages.common.element,
133-
messages.common.usage,
134-
messages.common.example,
135-
extra);
136-
}
137-
138-
var versionName = versionKeys[foundIndex];
139-
matchedData = preProcess[source][name][versionName];
140-
}
141-
142-
// TODO: Remove in the next version
143-
}
144-
145-
if ( ! newFields[matchedData.title])
146-
newFields[matchedData.title] = [];
147-
148-
newFields[matchedData.title].push(definition);
149-
});
150-
});
151-
152-
// replace fields with new field header
153-
// TODO: reduce complexity and remove group
154-
block.local[target].fields = newFields;
155-
});
156-
});
32+
function postProcess(parsedFiles, filenames, preProcess, packageInfos) {
33+
apiWorker.postProcess(parsedFiles, filenames, preProcess, packageInfos, 'defineBodyTitle', 'body', _messages);
15734
}
15835

15936
/**

lib/workers/api_query_title.js

Lines changed: 5 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
var semver = require('semver');
2-
var WorkerError = require('../errors/worker_error');
1+
var apiWorker = require('./api_param_title.js');
32

43
// Additional information for error log
54
var _messages = {
@@ -16,36 +15,10 @@ var _messages = {
1615
* @param {Object[]} parsedFiles
1716
* @param {String[]} filenames
1817
* @param {Object} packageInfos
19-
* @param {String} target Target path in preProcess-Object (returned result), where the data should be set.
2018
* @returns {Object}
2119
*/
22-
function preProcess(parsedFiles, filenames, packageInfos, target) {
23-
target = target || 'defineParamTitle';
24-
var source = 'define'; // relative path to the tree (global.), from where the data should be fetched.
25-
26-
var result = {};
27-
result[target] = {};
28-
29-
parsedFiles.forEach(function(parsedFile) {
30-
parsedFile.forEach(function(block) {
31-
if (block.global[source]) {
32-
var name = block.global[source].name;
33-
var version = block.version || packageInfos.defaultVersion;
34-
35-
if ( ! result[target][name])
36-
result[target][name] = {};
37-
38-
// fetch from global
39-
result[target][name][version] = block.global[source];
40-
}
41-
});
42-
});
43-
44-
// remove empty target
45-
if (result[target].length === 0)
46-
delete result[target];
47-
48-
return result;
20+
function preProcess(parsedFiles, filenames, packageInfos) {
21+
return apiWorker.preProcess(parsedFiles, filenames, packageInfos, 'defineQueryTitle');
4922
}
5023

5124
/**
@@ -55,105 +28,9 @@ function preProcess(parsedFiles, filenames, packageInfos, target) {
5528
* @param {String[]} filenames
5629
* @param {Object[]} preProcess
5730
* @param {Object} packageInfos
58-
* @param {String} source Source path in preProcess-Object
59-
* @param {String} target Relative path to the tree (local.), where the data should be modified.
60-
* @param {String} messages
6131
*/
62-
function postProcess(parsedFiles, filenames, preProcess, packageInfos, source, target, messages) {
63-
source = source || 'defineParamTitle';
64-
target = target || 'parameter';
65-
messages = messages || _messages;
66-
67-
parsedFiles.forEach(function(parsedFile, parsedFileIndex) {
68-
parsedFile.forEach(function(block) {
69-
if ( ! block.local[target] || ! block.local[target].fields)
70-
return;
71-
72-
var newFields = {};
73-
var fields = block.local[target].fields;
74-
Object.keys(fields).forEach(function(fieldGroup) {
75-
var params = block.local[target].fields[fieldGroup];
76-
77-
params.forEach(function(definition) {
78-
var name = definition.group;
79-
var version = block.version || packageInfos.defaultVersion;
80-
var matchedData = {};
81-
82-
if ( ! preProcess[source] || ! preProcess[source][name]) {
83-
// TODO: Enable in the next version
84-
// At the moment the (groupname) is optional and must not be defined.
85-
/*
86-
var extra = [
87-
{ 'Groupname': name }
88-
];
89-
throw new WorkerError('Referenced groupname does not exist / it is not defined with @apiDefine.',
90-
filenames[parsedFileIndex],
91-
block.index,
92-
messages.common.element,
93-
messages.common.usage,
94-
messages.common.example,
95-
extra);
96-
*/
97-
// TODO: Remove in the next version
98-
matchedData.name = name;
99-
matchedData.title = name;
100-
101-
}
102-
103-
// TODO: Remove in the next version
104-
else {
105-
106-
if (preProcess[source][name][version]) {
107-
// found the version
108-
matchedData = preProcess[source][name][version];
109-
} else {
110-
// find nearest matching version
111-
var foundIndex = -1;
112-
var lastVersion = packageInfos.defaultVersion;
113-
114-
var versionKeys = Object.keys(preProcess[source][name]);
115-
versionKeys.forEach(function(currentVersion, versionIndex) {
116-
if (semver.gte(version, currentVersion) && semver.gte(currentVersion, lastVersion)) {
117-
lastVersion = currentVersion;
118-
foundIndex = versionIndex;
119-
}
120-
});
121-
122-
if (foundIndex === -1) {
123-
var extra = [
124-
{ 'Groupname': name },
125-
{ 'Version': version },
126-
{ 'Defined versions': versionKeys },
127-
];
128-
throw new WorkerError('Referenced definition has no matching or a higher version. ' +
129-
'Check version number in referenced define block.',
130-
filenames[parsedFileIndex],
131-
block.index,
132-
messages.common.element,
133-
messages.common.usage,
134-
messages.common.example,
135-
extra);
136-
}
137-
138-
var versionName = versionKeys[foundIndex];
139-
matchedData = preProcess[source][name][versionName];
140-
}
141-
142-
// TODO: Remove in the next version
143-
}
144-
145-
if ( ! newFields[matchedData.title])
146-
newFields[matchedData.title] = [];
147-
148-
newFields[matchedData.title].push(definition);
149-
});
150-
});
151-
152-
// replace fields with new field header
153-
// TODO: reduce complexity and remove group
154-
block.local[target].fields = newFields;
155-
});
156-
});
32+
function postProcess(parsedFiles, filenames, preProcess, packageInfos) {
33+
apiWorker.postProcess(parsedFiles, filenames, preProcess, packageInfos, 'defineQueryTitle', 'query', _messages);
15734
}
15835

15936
/**

0 commit comments

Comments
 (0)