Skip to content

Commit 4724d44

Browse files
committed
解决tag,version,query,role等关键词注释错误
1 parent 1fb6d05 commit 4724d44

File tree

1 file changed

+53
-50
lines changed

1 file changed

+53
-50
lines changed

apijson/CodeUtil.js

Lines changed: 53 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ var CodeUtil = {
2828
if (StringUtil.isEmpty(reqStr)) {
2929
return '';
3030
}
31-
// maxLineLength = maxLineLength || 0;
3231

3332
var lines = reqStr.split('\n');
3433
var line;
@@ -40,46 +39,38 @@ var CodeUtil = {
4039
var key;
4140
var value;
4241

43-
var maxLength;
4442
var comment;
4543
for (var i = 0; i < lines.length; i ++) {
4644
line = lines[i].trim();
4745

4846
//每一种都要提取:左边的key
4947
index = line == null ? -1 : line.indexOf(': '); //可能是 ' 或 ",所以不好用 ': , ": 判断
50-
if (index < 0) {
51-
continue;
52-
}
53-
if (index == 0) { // ":user": value 不合法
54-
// throw new Error('不允许将 : 作为key的第一个字符!');
55-
comment = ' ! 不允许将 : 作为key的第一个字符!';
48+
key = index < 0 ? '' : line.substring(1, index - 1);
49+
50+
if (line.endsWith('{')) { //对象,判断是不是Table,再加对应的注释
51+
depth ++;
52+
names[depth] = key;
53+
comment = CodeUtil.getComment4Request(tableList, null, key, null);
5654
}
5755
else {
58-
key = line.substring(1, index - 1);
59-
60-
if (line.endsWith('{')) { //对象,判断是不是Table,再加对应的注释
61-
depth ++;
62-
names[depth] = key;
63-
comment = CodeUtil.getComment4Request(tableList, names[depth], key, null);
56+
if (line.endsWith(',')) {
57+
line = line.substring(0, line.length - 1);
6458
}
65-
else if (line.endsWith('[')) { //数组,判断是不是 key{}
66-
depth ++;
67-
names[depth] = key;
68-
comment = CodeUtil.getComment4Request(tableList, names[depth], key, null);
69-
}
70-
else if (line.endsWith('}') || line.endsWith(']')) {
59+
line = line.trim();
60+
if (line.endsWith('}')) {
7161
depth --;
62+
continue;
63+
}
64+
else if (key == '') { //[ 1, \n 2, \n 3] 跳过
65+
continue;
7266
}
7367
else { //其它,直接在后面加上注释
74-
value = line.substring(index + 2);
75-
comment = CodeUtil.getComment4Request(tableList, names[depth], key, value);
68+
var isArray = line.endsWith('[');
69+
alert('depth = ' + depth + '; line = ' + line + '; isArray = ' + isArray);
70+
comment = value == 'null' ? ' ! null无效' : CodeUtil.getComment4Request(tableList, names[depth], key, isArray ? '' : line.substring(index + 2).trim());
7671
}
7772
}
7873

79-
//maxLength = maxLineLength - lines[i].length;
80-
// if (maxLength >= 0 && comment.length > maxLength) {
81-
// comment = comment.substring(0, maxLength);
82-
// }
8374
lines[i] += comment;
8475
}
8576

@@ -742,17 +733,14 @@ var CodeUtil = {
742733

743734

744735
QUERY_TYPES: ['数据', '数量', '全部'],
745-
REQUEST_ROLES: {
736+
QUERY_TYPE_KEYS: [0, 1, 2],
737+
REQUEST_ROLE_KEYS: ['UNKNOWN', 'LOGIN', 'CONTACT', 'CIRCLE', 'OWNER', 'ADMIN'],
738+
REQUEST_ROLE: {
746739
UNKNOWN: '未登录',
747-
748740
LOGIN: '已登录',
749-
750741
CONTACT: '联系人',
751-
752742
CIRCLE: '圈子成员',
753-
754743
OWNER: '拥有者',
755-
756744
ADMIN: '管理员'
757745
},
758746

@@ -763,6 +751,8 @@ var CodeUtil = {
763751
* @param value
764752
*/
765753
getComment4Request: function (tableList, name, key, value) {
754+
alert('name = ' + name + '; key = ' + key + '; value = ' + value);
755+
766756
if (key == null) {
767757
return '';
768758
}
@@ -783,50 +773,59 @@ var CodeUtil = {
783773
if (JSONObject.isArrayKey(name)) {
784774
switch (key) {
785775
case 'count':
786-
return CodeUtil.getComment('最多数量', false, ' ');
776+
return CodeUtil.getType4Request(value) != 'number' ? ' ! value必须是Number类型!' : CodeUtil.getComment('最多数量', false, ' ');
787777
case 'page':
788-
return CodeUtil.getComment('分页页码', false, ' ');
789-
case 'query':
790-
value = Number(value)
791-
if (value < 0 || value > 2) {
792-
value = 0;
778+
if (CodeUtil.getType4Request(value) != 'number') {
779+
return ' ! value必须是Number类型!';
793780
}
794-
return CodeUtil.getComment('查询内容:' + (CodeUtil.QUERY_TYPES[value]), false, ' ');
781+
return value < 0 ? ' ! 必须 >= 0 !' : CodeUtil.getComment('分页页码', false, ' ');
782+
case 'query':
783+
var query = CodeUtil.QUERY_TYPES[value];
784+
return StringUtil.isEmpty(query) ? ' ! value必须是[' + CodeUtil.QUERY_TYPE_KEYS.join() + ']中的一种!' : CodeUtil.getComment('查询内容:' + query, false, ' ');
795785
}
796-
return CodeUtil.getComment('自定义关键词', false, ' ');
786+
return '';
797787
}
798788

799789
if (JSONObject.isTableKey(name)) {
800790
if (key.startsWith('@')) {
801791
switch (key) {
802792
case '@column':
803-
return CodeUtil.getComment('返回字段', false, ' ');
793+
return CodeUtil.getType4Request(value) != 'string' ? ' ! value必须是String类型!' : CodeUtil.getComment('返回字段', false, ' ');
804794
case '@order':
805-
return CodeUtil.getComment('排序方式,+升序,-降序', false, ' ');
795+
return CodeUtil.getType4Request(value) != 'string' ? ' ! value必须是String类型!' : CodeUtil.getComment('排序方式,+升序,-降序', false, ' ');
806796
case '@group':
807-
return CodeUtil.getComment('分组方式', false, ' ');
797+
return CodeUtil.getType4Request(value) != 'string' ? ' ! value必须是String类型!' : CodeUtil.getComment('分组方式', false, ' ');
808798
case '@having':
809-
return CodeUtil.getComment('SQL函数', false, ' ');
799+
return CodeUtil.getType4Request(value) != 'string' ? ' ! value必须是String类型!' : CodeUtil.getComment('SQL函数', false, ' ');
810800
case '@schema':
811-
return CodeUtil.getComment('数据库', false, ' ');
801+
return CodeUtil.getType4Request(value) != 'string' ? ' ! value必须是String类型!' : CodeUtil.getComment('数据库', false, ' ');
812802
case '@correct':
813-
return CodeUtil.getComment('字段校正', false, ' ');
803+
return value != null ? ' ! value必须是Object类型!' : CodeUtil.getComment('字段校正', false, ' ');
814804
case '@role':
815-
return CodeUtil.getComment('登录角色:' + CodeUtil.REQUEST_ROLES[value.toLowerCase()], false, ' ');
805+
try {
806+
value = value.substring(1, value.length - 1).toUpperCase();
807+
} catch (e) {}
808+
var role = CodeUtil.REQUEST_ROLE[value];
809+
return StringUtil.isEmpty(role) ? ' ! value必须是[' + CodeUtil.REQUEST_ROLE_KEYS.join() + ']中的一种!' : CodeUtil.getComment('登录角色:' + role, false, ' ');
816810
}
817811
return '';
818812
}
819813
return CodeUtil.getComment(CodeUtil.getCommentFromDoc(tableList, name, key), false, ' ');
820814
}
821815

816+
alert('name = ' + name + '; key = ' + key);
822817
if (StringUtil.isEmpty(name)) {
823818
switch (key) {
824819
case 'tag':
825-
return '请求密钥';
820+
return CodeUtil.getType4Request(value) != 'string' ? ' ! value必须是String类型!' : CodeUtil.getComment('请求密钥', false, ' ');
826821
case 'version':
827-
return '版本号';
822+
return CodeUtil.getType4Request(value) != 'number' ? ' ! value必须是Number类型!' : CodeUtil.getComment('版本号', false, ' ');
828823
case '@role':
829-
return '默认角色:' + CodeUtil.REQUEST_ROLES[value.toLowerCase()];
824+
try {
825+
value = value.substring(1, value.length - 1).toUpperCase();
826+
} catch (e) {}
827+
var role = CodeUtil.REQUEST_ROLE[value];
828+
return StringUtil.isEmpty(role) ? ' ! value必须是[' + CodeUtil.REQUEST_ROLE_KEYS.join() + ']中的一种!' : CodeUtil.getComment('默认角色:' + role, false, ' ');
830829
}
831830
}
832831

@@ -885,6 +884,10 @@ var CodeUtil = {
885884
}
886885

887886
return '';
887+
},
888+
889+
getType4Request: function (value) {
890+
return typeof JSON.parse(value);
888891
}
889892

890893
}

0 commit comments

Comments
 (0)