Skip to content

Commit 448f428

Browse files
committed
请求JSON注释:新增逻辑符,+-符及与请求方法组合的判断
1 parent 5108239 commit 448f428

File tree

3 files changed

+44
-24
lines changed

3 files changed

+44
-24
lines changed

apijson/CodeUtil.js

Lines changed: 41 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,12 @@ var CodeUtil = {
2424
* @param tableList
2525
* @return parseComment
2626
*/
27-
parseComment: function (reqStr, tableList) { //怎么都获取不到真正的长度,cols不行,默认20不变,maxLineLength不行,默认undefined不变 , maxLineLength) {
27+
parseComment: function (reqStr, tableList, method) { //怎么都获取不到真正的长度,cols不行,默认20不变,maxLineLength不行,默认undefined不变 , maxLineLength) {
2828
if (StringUtil.isEmpty(reqStr)) {
2929
return '';
3030
}
31+
method = method == null ? 'GET' : method.toUpperCase();
32+
3133

3234
var lines = reqStr.split('\n');
3335
var line;
@@ -50,7 +52,7 @@ var CodeUtil = {
5052
if (line.endsWith('{')) { //对象,判断是不是Table,再加对应的注释
5153
depth ++;
5254
names[depth] = key;
53-
comment = CodeUtil.getComment4Request(tableList, null, key, null);
55+
comment = CodeUtil.getComment4Request(tableList, null, key, null, method);
5456
}
5557
else {
5658
if (line.endsWith(',')) {
@@ -67,7 +69,8 @@ var CodeUtil = {
6769
else { //其它,直接在后面加上注释
6870
var isArray = line.endsWith('[');
6971
// alert('depth = ' + depth + '; line = ' + line + '; isArray = ' + isArray);
70-
comment = value == 'null' ? ' ! null无效' : CodeUtil.getComment4Request(tableList, names[depth], key, isArray ? '' : line.substring(index + 2).trim());
72+
comment = value == 'null' ? ' ! null无效' : CodeUtil.getComment4Request(tableList, names[depth], key
73+
, isArray ? '' : line.substring(index + 2).trim(), method);
7174
}
7275
}
7376

@@ -750,21 +753,24 @@ var CodeUtil = {
750753
* @param key
751754
* @param value
752755
*/
753-
getComment4Request: function (tableList, name, key, value) {
754-
// alert('name = ' + name + '; key = ' + key + '; value = ' + value);
756+
getComment4Request: function (tableList, name, key, value, method) {
757+
// alert('name = ' + name + '; key = ' + key + '; value = ' + value + '; method = ' + method);
755758

756759
if (key == null) {
757760
return '';
758761
}
759762

760763
if (value == null || value instanceof Object) {
761764
if (JSONObject.isArrayKey(key)) {
765+
if (method != 'GET') {
766+
return ' ! key[]:{}只支持GET方法!';
767+
}
762768
var arrName = JSONResponse.getSimpleName(key.substring(0, key.lastIndexOf('[]')));
763769
return CodeUtil.getComment('数组' + (JSONObject.isTableKey(arrName) ? ',提取' + arrName : ''), false, ' ');
764770
}
765771
if (JSONObject.isTableKey(key)) {
766772
var objName = JSONResponse.getSimpleName(key);
767-
var c = CodeUtil.getCommentFromDoc(tableList, objName, null);
773+
var c = CodeUtil.getCommentFromDoc(tableList, objName, null, method);
768774
return StringUtil.isEmpty(c) ? ' ! 表不存在!' : CodeUtil.getComment(c, false, ' ');
769775
}
770776

@@ -811,7 +817,7 @@ var CodeUtil = {
811817
}
812818
return '';
813819
}
814-
var c = CodeUtil.getCommentFromDoc(tableList, name, key);
820+
var c = CodeUtil.getCommentFromDoc(tableList, name, key, method);
815821
return StringUtil.isEmpty(c) ? ' ! 字段不存在!' : CodeUtil.getComment(c, false, ' ');
816822
}
817823

@@ -838,10 +844,11 @@ var CodeUtil = {
838844
* @param tableList
839845
* @param tableName
840846
* @param columnName
847+
* @param method
841848
* @return {*}
842849
*/
843-
getCommentFromDoc: function (tableList, tableName, columnName) {
844-
log('getCommentFromDoc tableName = ' + tableName + '; columnName = ' + columnName + '; tableList = \n' + JSON.stringify(tableList));
850+
getCommentFromDoc: function (tableList, tableName, columnName, method) {
851+
log('getCommentFromDoc tableName = ' + tableName + '; columnName = ' + columnName + '; method = ' + method + '; tableList = \n' + JSON.stringify(tableList));
845852

846853
if (tableList == null || tableList.length <= 0) {
847854
return '...';
@@ -899,38 +906,50 @@ var CodeUtil = {
899906
key = columnName.substring(0, columnName.length - 2);
900907
}
901908
else if (columnName.endsWith("+")) {//延长,PUT查询时处理
902-
//if (method == PUT) {//不为PUT就抛异常
903-
fun = '增加 或 扩展 ';
909+
if (method != 'PUT') {//不为PUT就抛异常
910+
return ' ! 功能符 + - 只能用于PUT请求!';
911+
}
912+
fun = '增加/扩展';
904913
key = columnName.substring(0, columnName.length - 1);
905-
//}
906914
}
907915
else if (columnName.endsWith("-")) {//缩减,PUT查询时处理
908-
//if (method == PUT) {//不为PUT就抛异常
909-
fun = '减少 或 去除';
916+
if (method != 'PUT') {//不为PUT就抛异常
917+
return ' ! 功能符 + - 只能用于PUT请求!';
918+
}
919+
fun = '减少/去除';
910920
key = columnName.substring(0, columnName.length - 1);
911-
//}
912921
}
913922
else {
914923
fun = '';
915924
key = new String(columnName);
916925
}
917926

918927

919-
var logic = '';
920-
//if (RequestMethod.isQueryMethod(method)) {//逻辑运算符仅供GET,HEAD方法使用
928+
var logic;
921929
if (key.endsWith("&")) {
930+
if (fun.length <= 0) {
931+
return ' ! 逻辑运算符 & | 后面必须接其它功能符!';
932+
}
922933
logic = '符合全部';
923934
}
924935
else if (key.endsWith("|")) {
936+
if (fun.length <= 0) {
937+
return ' ! 逻辑运算符 & | 后面必须接其它功能符!';
938+
}
925939
logic = '符合任意';
926940
}
927941
else if (key.endsWith("!")) {
928942
logic = '都不符合';
929943
}
930-
else {}
931-
//}
944+
else {
945+
logic = '';
946+
}
947+
932948

933949
if (logic.length > 0) {
950+
if (method != 'GET' && method != 'HEAD' && method != 'GETS' && method != 'HEADS') {//逻辑运算符仅供GET,HEAD方法使用
951+
return ' ! 逻辑运算符 & | ! 只能用于查询(GET,HEAD,GETS,HEADS)请求!';
952+
}
934953
key = key.substring(0, key.length - 1);
935954
}
936955

@@ -955,9 +974,9 @@ var CodeUtil = {
955974
continue;
956975
}
957976

958-
var p = (at.length <= 0 ? '' : at + ' > ')
959-
+ (fun.length <= 0 ? '' : fun + ' > ')
960-
+ (logic.length <= 0 ? '' : logic + ' > ');
977+
var p = (at.length <= 0 ? '' : at + ' < ')
978+
+ (fun.length <= 0 ? '' : fun + ' < ')
979+
+ (logic.length <= 0 ? '' : logic + ' < ');
961980
return (p.length <= 0 ? '' : p + key + ': ') + column.COLUMN_COMMENT;
962981
}
963982

index.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@
9191
</li>
9292
</ul>
9393
<div v-show="! isRemoteShow" style="width: 100%;height: 100%;" >
94-
<textarea id="vComment" style="width: 100%; height: 100%; position: absolute; z-index: 0; color: darkseagreen" wrap="off" disabled>
94+
<textarea id="vComment" style="width: 100%; height: 100%; position: absolute; z-index: 0; color: darkseagreen; background-color: white;" wrap="off" disabled>
9595
</textarea>
9696
<textarea id="vInput" @keyup="onChange(true)"style="width: 100%;height: 100%; position: absolute; z-index: 100; background: #0000;" wrap="off" >
9797
{
@@ -347,6 +347,7 @@
347347
<!-- 必须在main.js前 TODO 可能有冲突,代码写入vue文件? <<<<<<<<<<<<<<<< -->
348348
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
349349
<script type="text/javascript" language="JavaScript" charset="UTF-8" src="apijson/StringUtil.js" ></script>
350+
<script type="text/javascript" language="JavaScript" charset="UTF-8" src="apijson/RequestMethod.js" ></script>
350351
<script type="text/javascript" language="JavaScript" charset="UTF-8" src="apijson/JSONObject.js" ></script>
351352
<script type="text/javascript" language="JavaScript" charset="UTF-8" src="apijson/JSONRequest.js" ></script>
352353
<script type="text/javascript" language="JavaScript" charset="UTF-8" src="apijson/JSONResponse.js" ></script>

js/main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,7 @@
708708
App.showDoc()
709709

710710
try {
711-
vComment.value = isSingle ? '' : CodeUtil.parseComment(before, docObj == null ? null : docObj['[]'])
711+
vComment.value = isSingle ? '' : CodeUtil.parseComment(before, docObj == null ? null : docObj['[]'], App.getMethod())
712712
} catch (e) {
713713
log('onHandle try { vComment.value = CodeUtil.parseComment >> } catch (e) {\n' + e.message);
714714
}

0 commit comments

Comments
 (0)