Skip to content

Commit d8f045c

Browse files
committed
新增自动生成请求JSON的注释
1 parent 74d3b74 commit d8f045c

File tree

4 files changed

+217
-5
lines changed

4 files changed

+217
-5
lines changed

apijson/CodeUtil.js

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,68 @@
1919
var CodeUtil = {
2020
TAG: 'CodeUtil',
2121

22+
/**生成JSON的注释
23+
* @param reqStr //已格式化的JSON String
24+
* @param tableList
25+
* @return parseComment
26+
*/
27+
parseComment: function (reqStr, tableList) { //怎么都获取不到真正的长度,cols不行,默认20不变,maxLineLength不行,默认undefined不变 , maxLineLength) {
28+
if (StringUtil.isEmpty(reqStr)) {
29+
return '';
30+
}
31+
// maxLineLength = maxLineLength || 0;
32+
33+
var lines = reqStr.split('\n');
34+
var line;
35+
36+
var depth = 0;
37+
var names = [];
38+
39+
var index;
40+
var key;
41+
var value;
42+
43+
var maxLength;
44+
var comment;
45+
for (var i = 0; i < lines.length; i ++) {
46+
line = lines[i].trim();
47+
48+
//每一种都要提取:左边的key
49+
index = line.indexOf(': '); //可能是 ' 或 ",所以不好用 ': , ": 判断
50+
if (index < 0) {
51+
continue;
52+
}
53+
54+
key = line.substring(1, index - 1);
55+
56+
if (line.endsWith('{')) { //对象,判断是不是Table,再加对应的注释
57+
depth ++;
58+
names[depth] = key;
59+
comment = CodeUtil.getComment4Request(tableList, names[depth], key, null);
60+
}
61+
else if (line.endsWith('[')) { //数组,判断是不是 key{}
62+
depth ++;
63+
names[depth] = key;
64+
comment = CodeUtil.getComment4Request(tableList, names[depth], key, null);
65+
}
66+
else if (line.endsWith('}') || line.endsWith(']')) {
67+
depth --;
68+
}
69+
else { //其它,直接在后面加上注释
70+
value = line.substring(index + 2);
71+
comment = CodeUtil.getComment4Request(tableList, names[depth], key, value);
72+
}
73+
74+
//maxLength = maxLineLength - lines[i].length;
75+
// if (maxLength >= 0 && comment.length > maxLength) {
76+
// comment = comment.substring(0, maxLength);
77+
// }
78+
lines[i] += comment;
79+
}
80+
81+
return lines.join('\n');
82+
},
83+
2284
/**解析出 生成iOS-Swift请求JSON 的代码
2385
* 只需要把所有 对象标识{} 改为数组标识 []
2486
* @param name
@@ -670,6 +732,128 @@ var CodeUtil = {
670732
var index = column.lastIndexOf('_'); // snake_case
671733
var id = index < 0 ? column : column.substring(index + 1);
672734
return id.toLowerCase() == 'id';
735+
},
736+
737+
QUERY_TYPES: ['数据', '数量', '全部'],
738+
739+
/**获取请求JSON的注释
740+
* @param tableList
741+
* @param name
742+
* @param key
743+
* @param value
744+
*/
745+
getComment4Request: function (tableList, name, key, value) {
746+
if (key == null) {
747+
return '';
748+
}
749+
750+
if (value == null || value instanceof Object) {
751+
if (JSONObject.isArrayKey(key)) {
752+
var arrName = JSONResponse.getSimpleName(key.substring(0, key.lastIndexOf('[]')));
753+
return CodeUtil.getComment('数组' + (JSONObject.isTableKey(arrName) ? ',去除' + arrName + '包装' : ''), false, ' ');
754+
}
755+
if (JSONObject.isTableKey(key)) {
756+
var objName = JSONResponse.getSimpleName(key);
757+
return CodeUtil.getComment(CodeUtil.getCommentFromDoc(tableList, objName, null), false, ' ');
758+
}
759+
760+
return '';
761+
}
762+
763+
if (JSONObject.isArrayKey(name)) {
764+
switch (key) {
765+
case 'count':
766+
return CodeUtil.getComment('最多数量', false, ' ');
767+
case 'page':
768+
return CodeUtil.getComment('分页页码', false, ' ');
769+
case 'query':
770+
value = Number(value)
771+
if (value < 0 || value > 2) {
772+
value = 0;
773+
}
774+
return CodeUtil.getComment('查询内容:' + (CodeUtil.QUERY_TYPES[value]), false, ' ');
775+
}
776+
return CodeUtil.getComment('自定义关键词', false, ' ');
777+
}
778+
779+
if (JSONObject.isTableKey(name)) {
780+
if (key.startsWith('@')) {
781+
switch (key) {
782+
case '@column':
783+
return CodeUtil.getComment('返回字段', false, ' ');
784+
case '@order':
785+
return CodeUtil.getComment('排序方式,+升序,-降序', false, ' ');
786+
case '@group':
787+
return CodeUtil.getComment('分组方式', false, ' ');
788+
case '@having':
789+
return CodeUtil.getComment('SQL函数', false, ' ');
790+
case '@schema':
791+
return CodeUtil.getComment('数据库', false, ' ');
792+
case '@correct':
793+
return CodeUtil.getComment('字段校正', false, ' ');
794+
case '@role':
795+
return CodeUtil.getComment('登录角色', false, ' ');
796+
}
797+
return '';
798+
}
799+
return CodeUtil.getComment(CodeUtil.getCommentFromDoc(tableList, name, key), false, ' ');
800+
}
801+
802+
return '';
803+
},
804+
805+
/**
806+
* @param tableList
807+
* @param tableName
808+
* @param columnName
809+
* @return {*}
810+
*/
811+
getCommentFromDoc: function (tableList, tableName, columnName) {
812+
log('getCommentFromDoc tableName = ' + tableName + '; columnName = ' + columnName + '; tableList = \n' + JSON.stringify(tableList));
813+
814+
if (tableList != null) {
815+
var item;
816+
817+
var table;
818+
var columnList;
819+
var column;
820+
for (var i = 0; i < tableList.length; i++) {
821+
item = tableList[i];
822+
823+
//Table
824+
table = item == null ? null : item.Table;
825+
if (table == null || tableName != CodeUtil.getModelName(table.TABLE_NAME)) {
826+
continue;
827+
}
828+
log('getDoc [] for i=' + i + ': table = \n' + format(JSON.stringify(table)));
829+
830+
if (StringUtil.isEmpty(columnName)) {
831+
return table.TABLE_COMMENT;
832+
}
833+
834+
columnList = item['Column[]'];
835+
if (columnList == null) {
836+
continue;
837+
}
838+
log('getDoc [] for ' + i + ': columnList = \n' + format(JSON.stringify(columnList)));
839+
840+
var name;
841+
for (var j = 0; j < columnList.length; j++) {
842+
column = columnList[j];
843+
name = column == null ? null : column.COLUMN_NAME;
844+
if (name == null || columnName != name) {
845+
continue;
846+
}
847+
848+
return column.COLUMN_COMMENT;
849+
}
850+
851+
break;
852+
}
853+
854+
}
855+
856+
return '';
673857
}
674858

675859
}

css/main.css

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ html,body {
3333
}
3434

3535

36-
textarea {
37-
color: #555;
36+
textarea, input {
37+
color: #000;
3838
}
3939

4040
ul {

index.html

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,26 @@
9090
<a href="javascript:void(0)" @click="restore(item)" > {{(item.version > 0 ? 'V' + item.version : 'V*') + ' ' + item.name + ' ' + item.url}}</a>
9191
</li>
9292
</ul>
93-
<textarea v-show="! isRemoteShow" style="width: 100%;height: 100%;" id="vInput" @keyup="onChange(true)">
93+
<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>
95+
{
96+
"[]": { //数组
97+
"User": { //用户开放信息表
98+
"sex": 1 //性别
99+
}
100+
}
101+
}
102+
</textarea>
103+
<textarea id="vInput" @keyup="onChange(true)"style="width: 100%;height: 100%; position: absolute; z-index: 100; background: #0000;" wrap="off" >
94104
{
95105
"[]":{
96106
"User":{
97107
"sex":1
98108
}
99109
}
100110
}
101-
</textarea>
111+
</textarea>
112+
</div>
102113
</div>
103114
</div>
104115

@@ -355,6 +366,7 @@
355366
<script type="text/javascript" language="JavaScript" charset="UTF-8" >
356367
//TODO 保留,用v-model绑定到App.data会报错,各种undefined
357368
var vInput = document.getElementById("vInput");
369+
var vComment = document.getElementById("vComment");
358370
var vOutput = document.getElementById("vOutput");
359371
var vUrl = document.getElementById("vUrl");
360372
var vTransfer = document.getElementById("vTransfer");
@@ -363,6 +375,15 @@
363375
vUrl.value = new String(URL_GET); //main.js里访问不到,可能是script引用顺序问题
364376

365377
var vMarkdown = document.getElementById('vMarkdown');
378+
379+
380+
//vComment跟随vInput滚动,避免JSON重叠"露馅"
381+
$(vInput).on('scroll', function() {
382+
$(vComment).scrollLeft(this.scrollLeft);
383+
$(vComment).scrollTop(this.scrollTop);
384+
});
385+
386+
366387
function markdownToHTML(md) {
367388
vMarkdown.innerHTML = '';
368389
editormd.markdownToHTML("vMarkdown", {

js/main.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,7 @@
670670
}
671671

672672
App.view = 'output';
673+
vComment.value = '';
673674
vOutput.value = 'resolving...';
674675

675676
//格式化输入代码
@@ -706,6 +707,10 @@
706707

707708
App.showDoc()
708709

710+
// alert('vComment.with = ' + vComment.cols);
711+
// alert('vInput.with = ' + vInput.cols);
712+
vComment.value = CodeUtil.parseComment(before, docObj == null ? null : docObj['[]'])
713+
709714
} catch(e) {
710715
log(e)
711716
vSend.disabled = true
@@ -723,6 +728,8 @@
723728
onChange: function (delay) {
724729
this.setBaseUrl();
725730
inputted = new String(vInput.value);
731+
vComment.value = '';
732+
726733
clearTimeout(handler);
727734

728735
this.isDelayShow = delay;
@@ -864,7 +871,7 @@
864871
*/
865872
getCode: function (rq) {
866873
return '\n\n\n### 请求代码 \n\n#### <= Android-Java: 同名变量需要重命名\n ```java \n'
867-
+ CodeUtil.parseJava(null, JSON.parse(rq))
874+
+ StringUtil.trim(CodeUtil.parseJava(null, JSON.parse(rq)))
868875
+ '\n ``` \n注:用了APIJSON的JSONRequest类。也可使用其它方式,只要JSON有序就行。'
869876
+ '\n\n#### <= iOS-Swift: 所有对象标识{}改为数组标识[]\n ```swift \n'
870877
+ CodeUtil.parseSwift(null, JSON.parse(rq))

0 commit comments

Comments
 (0)