[go: nahoru, domu]

Skip to content

Commit

Permalink
场景串联用例:PRE_DATA 等提取值函数支持省略路径,其中数组下标支持 -1 这种负数来倒序查找
Browse files Browse the repository at this point in the history
  • Loading branch information
TommyLemon committed May 2, 2024
1 parent e556051 commit df4e8fb
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 21 deletions.
31 changes: 25 additions & 6 deletions apijson/JSONResponse.js
Original file line number Diff line number Diff line change
Expand Up @@ -1581,6 +1581,7 @@ var JSONResponse = {
if (k == null) {
return null;
}
k = decodeURI(k)

if (tgt instanceof Object) {
if (k == '') {
Expand All @@ -1594,11 +1595,26 @@ var JSONResponse = {
}
}
}
else {
k = decodeURI(k)
if (tgt instanceof Array) {
try {
var n = Number.parseInt(k);
if (Number.isSafeInteger(n)) {
k = n > 0 ? n : n + tgt.length;
}
} catch (e) {
}
}
}

tgt = tgt[k];

continue;
}
else {
throw new Error('getValByPath 语法错误,' + k + ': value 中 value 类型应该是 Object 或 Array !');
}

return null;
}
Expand Down Expand Up @@ -1638,12 +1654,15 @@ var JSONResponse = {
k = 0;
}
else {
try {
var n = Number.parseInt(k);
if (Number.isSafeInteger(n)) {
k = 0;
}
} catch (e) {
k = decodeURI(k)
if (tgt instanceof Array) {
try {
var n = Number.parseInt(k);
if (Number.isSafeInteger(n)) {
k = n > 0 ? n : n + tgt.length;
}
} catch (e) {
}
}
}

Expand Down
31 changes: 16 additions & 15 deletions js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -623,11 +623,12 @@ https://github.com/Tencent/APIJSON/issues
var CUR_ARG = 'CUR_ARG' // CUR_REQ('User/id')
var CUR_PUT = 'CUR_PUT' // CUR_PUT('key', val)

function get4Path(obj, path) {
if (StringUtil.isEmpty(path, false)) {
return obj
function get4Path(obj, path, nullable) {
var val = JSONResponse.getValByPath(obj, StringUtil.split(path, '/'), true)
if (val == null && nullable != true) {
throw new Error('找不到 ' + path + ' 对应在 obj 中的非 null 值!')
}
return JSONResponse.getValByPath(obj, StringUtil.split(path, '/'), true)
return val
}

var RANDOM_DB = 'RANDOM_DB'
Expand Down Expand Up @@ -4307,7 +4308,7 @@ https://github.com/Tencent/APIJSON/issues
return
}

var group = this.chainGroups[this.currentChainGroupIndex]
var group = (this.chainGroups[this.currentChainGroupIndex] || {}).Chain
if (group == null) {
var index = this.chainPaths.length - 1
group = this.chainPaths[index]
Expand Down Expand Up @@ -4388,7 +4389,7 @@ https://github.com/Tencent/APIJSON/issues
if (group == null) {
if (index == null) {
index = this.casePaths.length - 1
group = (this.casePaths[index] || {}).Chain
group = this.casePaths[index]
} else {
this.casePaths = []
}
Expand Down Expand Up @@ -9194,7 +9195,7 @@ Content-Type: ` + contentType) + (StringUtil.isEmpty(headerStr, true) ? '' : hea
const index = line.indexOf(': '); //APIJSON Table:alias 前面不会有空格 //致后面就接 { 'a': 1} 报错 Unexpected token ':' lastIndexOf(': '); // indexOf(': '); 可能会有 Comment:to
const p_k = line.substring(0, index);
const bi = -1; //没必要支持,用 before: undefined, after: .. 同样支持替换,反而这样导致不兼容包含空格的 key p_k.indexOf(' ');
const path = bi < 0 ? p_k : p_k.substring(0, bi); // User/id
const path = decodeURI(bi < 0 ? p_k : p_k.substring(0, bi)); // User/id

const pathKeys = path.split('/')
if (pathKeys == null || pathKeys.length <= 0) {
Expand Down Expand Up @@ -9449,28 +9450,28 @@ Content-Type: ` + contentType) + (StringUtil.isEmpty(headerStr, true) ? '' : hea
}
else { //随机函数
if (fun == PRE_REQ) {
toEval = 'get4Path(((ctx || {}).pre || {}).req, ' + value.substring(start + 1);
toEval = 'get4Path(((ctx || {}).pre || {}).req, ' + (value == ')' ? JSON.stringify(path) : '') + value.substring(start + 1);
}
else if (fun == PRE_ARG) {
toEval = 'get4Path(((ctx || {}).pre || {}).arg, ' + value.substring(start + 1);
toEval = 'get4Path(((ctx || {}).pre || {}).arg, ' + (value == ')' ? JSON.stringify(path) : '') + value.substring(start + 1);
}
else if (fun == PRE_RES) {
toEval = 'get4Path(((ctx || {}).pre || {}).res, ' + value.substring(start + 1);
toEval = 'get4Path(((ctx || {}).pre || {}).res, ' + (value == ')' ? JSON.stringify(path) : '') + value.substring(start + 1);
}
else if (fun == PRE_DATA) {
toEval = 'get4Path(((ctx || {}).pre || {}).data, ' + value.substring(start + 1);
toEval = 'get4Path(((ctx || {}).pre || {}).data, ' + (value == ')' ? JSON.stringify(path) : '') + value.substring(start + 1);
}
else if (fun == PRE_EXT) {
toEval = 'get4Path(((ctx || {}).pre || {}).ext, ' + value.substring(start + 1);
toEval = 'get4Path(((ctx || {}).pre || {}).ext, ' + (value == ')' ? JSON.stringify(path) : '') + value.substring(start + 1);
}
else if (fun == CUR_REQ) {
toEval = 'get4Path(((ctx || {}).cur || {}).req, ' + value.substring(start + 1);
toEval = 'get4Path(((ctx || {}).cur || {}).req, ' + (value == ')' ? JSON.stringify(path) : '') + value.substring(start + 1);
}
else if (fun == CUR_ARG) {
toEval = 'get4Path(((ctx || {}).cur || {}).arg, ' + value.substring(start + 1);
toEval = 'get4Path(((ctx || {}).cur || {}).arg, ' + (value == ')' ? JSON.stringify(path) : '') + value.substring(start + 1);
}
else if (fun == CUR_PUT) {
toEval = 'put4Path(((ctx || {}).cur || {}).ctx, ' + value.substring(start + 1);
toEval = 'put4Path(((ctx || {}).cur || {}).ctx, ' + (value == ')' ? JSON.stringify(path) : '') + value.substring(start + 1);
}
else {
fun = funWithOrder; //还原,其它函数不支持 升降序和跨步!
Expand Down

0 comments on commit df4e8fb

Please sign in to comment.