Skip to content

Commit db61da1

Browse files
committed
Added support for nested inline action schemas in action schemas.
1 parent deb8aa4 commit db61da1

2 files changed

Lines changed: 89 additions & 14 deletions

File tree

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
- fixed `NEWFORK()` method
3131
- fixed callback with `$` in the `ACTION().callback($)`
3232
- fixed returing a default JSON schema object in actions
33+
- added support for nested inline action schemas in action schemas
3334

3435
========================
3536
0.0.15

utils.js

Lines changed: 88 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5949,6 +5949,84 @@ exports.connect = function(opt, callback) {
59495949
meta.socket1.on('clientError', error);
59505950
};
59515951

5952+
function extractnested(str, minDepth = 0) {
5953+
5954+
const parts = [];
5955+
5956+
let out = '';
5957+
let depth = 0;
5958+
let capturing = false;
5959+
let capDepth = 0;
5960+
let buf = '';
5961+
5962+
const isOpen = (c) => c === '[' || c === '{';
5963+
const isClose = (c) => c === ']' || c === '}';
5964+
const matches = (o, c) => (o === '[' && c === ']') || (o === '{' && c === '}');
5965+
const stack = [];
5966+
5967+
for (let i = 0; i < str.length; i++) {
5968+
const ch = str[i];
5969+
5970+
if (isOpen(ch)) {
5971+
5972+
if (!capturing && depth >= minDepth) {
5973+
capturing = true;
5974+
capDepth = depth + 1;
5975+
buf = '';
5976+
}
5977+
5978+
stack.push(ch);
5979+
depth++;
5980+
5981+
if (capturing) buf += ch;
5982+
else out += ch;
5983+
5984+
continue;
5985+
}
5986+
5987+
if (isClose(ch)) {
5988+
5989+
const open = stack[stack.length - 1];
5990+
if (!open || !matches(open, ch))
5991+
throw new Error(`Mismatched/unbalanced brackets at index ${i}`);
5992+
5993+
if (capturing) buf += ch;
5994+
else out += ch;
5995+
5996+
stack.pop();
5997+
depth--;
5998+
5999+
if (capturing && depth < capDepth) {
6000+
6001+
const inner = buf.slice(1, -1);
6002+
const type = buf[0];
6003+
const startsWithRef = inner.trimStart().startsWith('@');
6004+
const hasEnumPipe = type === '{' && inner.includes('|');
6005+
6006+
if (!startsWithRef && !hasEnumPipe) {
6007+
const idx = parts.length;
6008+
out += type + `#${idx}` + (type === '{' ? '}' : ']');
6009+
parts.push(inner);
6010+
} else
6011+
out += buf;
6012+
6013+
capturing = false;
6014+
buf = '';
6015+
}
6016+
6017+
continue;
6018+
}
6019+
6020+
if (capturing) buf += ch;
6021+
else out += ch;
6022+
}
6023+
6024+
if (stack.length)
6025+
throw new Error('Unbalanced opening bracket(s)');
6026+
6027+
return { text: out, parts };
6028+
}
6029+
59526030
SP.toJSONSchema = SP.parseSchema = function(name, url) {
59536031

59546032
let obj = {};
@@ -5963,19 +6041,11 @@ SP.toJSONSchema = SP.parseSchema = function(name, url) {
59636041
obj.properties = {};
59646042

59656043
let str = this;
5966-
let nestedtypes = [];
6044+
let nestedtypes;
59676045

5968-
str = str.replace(/\[.*?\]/g, function(text) {
5969-
if (text.substring(1, 2) === '@')
5970-
return text;
5971-
return '[#' + (nestedtypes.push(text.substring(1, text.length - 1)) - 1) + ']';
5972-
});
5973-
5974-
str = str.replace(/\{.*?\}/g, function(text) {
5975-
if (text.substring(1, 2) === '@')
5976-
return text;
5977-
return '{#' + (nestedtypes.push(text.substring(1, text.length - 1)) - 1) + '}';
5978-
});
6046+
let extracted = extractnested(str);
6047+
nestedtypes = extracted.parts;
6048+
str = extracted.text;
59796049

59806050
let prop = str.split(/,|\n/);
59816051
let required = [];
@@ -6034,8 +6104,12 @@ SP.toJSONSchema = SP.parseSchema = function(name, url) {
60346104
let isenum = type[0] === '{';
60356105

60366106
if (isenum) {
6037-
tmp = type.substring(2, type.length - 1);
6038-
tmp = nestedtypes[+tmp];
6107+
6108+
if (type[1] === '@' || type[1] === '#') {
6109+
tmp = type.substring(2, type.indexOf('}'));
6110+
tmp = nestedtypes[+tmp];
6111+
} else
6112+
tmp = type.substring(1, type.indexOf('}'));
60396113

60406114
// Nested schema
60416115
if ((/[:,\s]/).test(tmp)) {

0 commit comments

Comments
 (0)