Skip to content

Commit 30e9f8a

Browse files
authored
feat: Allow DS for service program return value (#217)
1 parent f0fcc6f commit 30e9f8a

File tree

3 files changed

+50
-29
lines changed

3 files changed

+50
-29
lines changed

lib/Deprecated.js

+15-8
Original file line numberDiff line numberDiff line change
@@ -109,18 +109,25 @@ class iPgm {
109109

110110
/**
111111
* @description adds a return element to the program XML
112-
* @param {string} data
112+
* @param {string} value
113113
* @param {string} type
114114
* @param {object} [options]
115115
*/
116-
addReturn(data, type = '1024a', options) {
117-
if (!type) {
118-
iPgmDeprecate('defaulting return type to 1024a has been deprecated. You should specify a type instead.');
119-
// eslint-disable-next-line no-param-reassign
120-
type = '1024a';
116+
addReturn(value, type = '1024a', options) {
117+
const data = {};
118+
data.type = type;
119+
data.value = value;
120+
if (!data.type) {
121+
iPgmDeprecate('defaulting return data type to 1024a has been deprecated. You should specify a type instead.');
122+
data.type = '1024a';
123+
}
124+
125+
if (typeof options === 'object') { // <data> options
126+
Object.keys(options).forEach((key) => {
127+
data[key] = options[key];
128+
});
121129
}
122-
iPgmDeprecate('As of v1.0, \'iPgm.addReturn()\' is deprecated. Please use \'ProgramCall.addParam()\' instead.');
123-
return this.ProgramCall.addReturn(data, type, options);
130+
return this.ProgramCall.addReturn(data);
124131
}
125132

126133
/**

lib/ProgramCall.js

+10-16
Original file line numberDiff line numberDiff line change
@@ -92,25 +92,19 @@ class ProgramCall {
9292

9393
/**
9494
* @description adds a return element to the program XML
95-
* @param {string} data
96-
* @param {string} type
97-
* @param {object} [options]
95+
* @param {object} data
9896
*/
99-
addReturn(data, type, options = null) {
100-
if (!type) {
101-
throw new Error('Specifying the return type is required.');
97+
addReturn(data = {}) {
98+
if (typeof data !== 'object') {
99+
throw new Error('Expected the first parameter to be an object');
102100
}
103-
this.xml += '<return>';
104-
105-
if (options && typeof options === 'object') {
106-
this.xml += `<data type='${type}'`;
107-
Object.keys(options).forEach((key) => {
108-
this.xml += ` ${key}='${options[key]}'`;
109-
});
110-
} else {
111-
this.xml += `<data type='${type}'`;
101+
if (data.type === undefined) {
102+
throw new Error('Expected \'type\' key on the data object');
112103
}
113-
this.xml += `>${data}</data></return>`;
104+
105+
this.xml += '<return>';
106+
this.__addData__(data);
107+
this.xml += '</return>';
114108
}
115109

116110
/**

test/unit/ProgamCallUnit.js

+25-5
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ describe('ProgramCall Class Unit Tests', () => {
139139
const pgm = new ProgramCall('MYPGM', { lib: 'MYLIB', func: 'MY_PROCEDURE' });
140140

141141
pgm.addParam({ value: '', type: '1A', by: 'val' });
142-
pgm.addReturn('', '2A', 'output');
142+
pgm.addReturn({ name: 'output', type: '2A', value: '' });
143143

144144
const lookAtXML = pgm.toXML();
145145
expect(lookAtXML).to.match(/<parm .*by='val'.*>/);
@@ -156,7 +156,7 @@ describe('ProgramCall Class Unit Tests', () => {
156156
pgm.addParam({
157157
name: 'inds', type: 'ds', by: 'val', fields: params,
158158
});
159-
pgm.addReturn('', '2A', { name: 'output' });
159+
pgm.addReturn({ name: 'output', type: '2A', value: '' });
160160

161161
const lookAtXML = pgm.toXML();
162162
expect(lookAtXML).to.match(/<parm .*by='val'.*>/);
@@ -168,7 +168,7 @@ describe('ProgramCall Class Unit Tests', () => {
168168
pgm.addParam({
169169
value: '', type: '1A', by: 'val', io: 'both',
170170
});
171-
pgm.addReturn('', '2A', { name: 'output' });
171+
pgm.addReturn({ name: 'output', type: '2A', value: '' });
172172

173173
const lookAtXML = pgm.toXML();
174174
expect(lookAtXML).to.match(/<parm .*by='val'.*>/);
@@ -186,7 +186,7 @@ describe('ProgramCall Class Unit Tests', () => {
186186
pgm.addParam({
187187
name: 'inds', type: 'ds', by: 'val', io: 'both', fields: params,
188188
});
189-
pgm.addReturn('', '2A', { name: 'output' });
189+
pgm.addReturn({ name: 'output', type: '2A', value: '' });
190190

191191
const lookAtXML = pgm.toXML();
192192
expect(lookAtXML).to.match(/<parm .*by='val'.*>/);
@@ -204,12 +204,32 @@ describe('ProgramCall Class Unit Tests', () => {
204204
error: 'fast',
205205
});
206206

207-
pgm.addReturn('0', '20A');
207+
pgm.addReturn({ type: '20A', value: '0' });
208208

209209
const expectedXML = '<pgm name=\'QTOCNETSTS\' lib=\'QSYS\' func=\'QtoRtvTCPA\' '
210210
+ 'error=\'fast\'><return><data type=\'20A\'>0</data></return></pgm>';
211211

212212
expect(pgm.toXML()).to.equal(expectedXML);
213213
});
214+
215+
it('appends return with ds to pgm xml', () => {
216+
const pgm = new ProgramCall('TEST');
217+
218+
const ds = {
219+
name: 'test_ds',
220+
type: 'ds',
221+
io: 'out',
222+
fields: [
223+
{ type: '10i0', value: 0 },
224+
{ type: '10A', value: '' },
225+
],
226+
};
227+
pgm.addReturn(ds);
228+
229+
const expectedXML = "<pgm name='TEST' error='fast'><return><ds name='test_ds'>"
230+
+ "<data type='10i0'>0</data><data type='10A'></data></ds></return></pgm>";
231+
232+
expect(pgm.toXML()).to.equal(expectedXML);
233+
});
214234
});
215235
});

0 commit comments

Comments
 (0)