@@ -28,7 +28,6 @@ var CodeUtil = {
28
28
if ( StringUtil . isEmpty ( reqStr ) ) {
29
29
return '' ;
30
30
}
31
- // maxLineLength = maxLineLength || 0;
32
31
33
32
var lines = reqStr . split ( '\n' ) ;
34
33
var line ;
@@ -40,46 +39,38 @@ var CodeUtil = {
40
39
var key ;
41
40
var value ;
42
41
43
- var maxLength ;
44
42
var comment ;
45
43
for ( var i = 0 ; i < lines . length ; i ++ ) {
46
44
line = lines [ i ] . trim ( ) ;
47
45
48
46
//每一种都要提取:左边的key
49
47
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 ) ;
56
54
}
57
55
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 ) ;
64
58
}
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 ( '}' ) ) {
71
61
depth -- ;
62
+ continue ;
63
+ }
64
+ else if ( key == '' ) { //[ 1, \n 2, \n 3] 跳过
65
+ continue ;
72
66
}
73
67
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 ( ) ) ;
76
71
}
77
72
}
78
73
79
- //maxLength = maxLineLength - lines[i].length;
80
- // if (maxLength >= 0 && comment.length > maxLength) {
81
- // comment = comment.substring(0, maxLength);
82
- // }
83
74
lines [ i ] += comment ;
84
75
}
85
76
@@ -742,17 +733,14 @@ var CodeUtil = {
742
733
743
734
744
735
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 : {
746
739
UNKNOWN : '未登录' ,
747
-
748
740
LOGIN : '已登录' ,
749
-
750
741
CONTACT : '联系人' ,
751
-
752
742
CIRCLE : '圈子成员' ,
753
-
754
743
OWNER : '拥有者' ,
755
-
756
744
ADMIN : '管理员'
757
745
} ,
758
746
@@ -763,6 +751,8 @@ var CodeUtil = {
763
751
* @param value
764
752
*/
765
753
getComment4Request : function ( tableList , name , key , value ) {
754
+ alert ( 'name = ' + name + '; key = ' + key + '; value = ' + value ) ;
755
+
766
756
if ( key == null ) {
767
757
return '' ;
768
758
}
@@ -783,50 +773,59 @@ var CodeUtil = {
783
773
if ( JSONObject . isArrayKey ( name ) ) {
784
774
switch ( key ) {
785
775
case 'count' :
786
- return CodeUtil . getComment ( '最多数量' , false , ' ' ) ;
776
+ return CodeUtil . getType4Request ( value ) != 'number' ? ' ! value必须是Number类型!' : CodeUtil . getComment ( '最多数量' , false , ' ' ) ;
787
777
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类型!' ;
793
780
}
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 , ' ' ) ;
795
785
}
796
- return CodeUtil . getComment ( '自定义关键词' , false , ' ' ) ;
786
+ return '' ;
797
787
}
798
788
799
789
if ( JSONObject . isTableKey ( name ) ) {
800
790
if ( key . startsWith ( '@' ) ) {
801
791
switch ( key ) {
802
792
case '@column' :
803
- return CodeUtil . getComment ( '返回字段' , false , ' ' ) ;
793
+ return CodeUtil . getType4Request ( value ) != 'string' ? ' ! value必须是String类型!' : CodeUtil . getComment ( '返回字段' , false , ' ' ) ;
804
794
case '@order' :
805
- return CodeUtil . getComment ( '排序方式,+升序,-降序' , false , ' ' ) ;
795
+ return CodeUtil . getType4Request ( value ) != 'string' ? ' ! value必须是String类型!' : CodeUtil . getComment ( '排序方式,+升序,-降序' , false , ' ' ) ;
806
796
case '@group' :
807
- return CodeUtil . getComment ( '分组方式' , false , ' ' ) ;
797
+ return CodeUtil . getType4Request ( value ) != 'string' ? ' ! value必须是String类型!' : CodeUtil . getComment ( '分组方式' , false , ' ' ) ;
808
798
case '@having' :
809
- return CodeUtil . getComment ( 'SQL函数' , false , ' ' ) ;
799
+ return CodeUtil . getType4Request ( value ) != 'string' ? ' ! value必须是String类型!' : CodeUtil . getComment ( 'SQL函数' , false , ' ' ) ;
810
800
case '@schema' :
811
- return CodeUtil . getComment ( '数据库' , false , ' ' ) ;
801
+ return CodeUtil . getType4Request ( value ) != 'string' ? ' ! value必须是String类型!' : CodeUtil . getComment ( '数据库' , false , ' ' ) ;
812
802
case '@correct' :
813
- return CodeUtil . getComment ( '字段校正' , false , ' ' ) ;
803
+ return value != null ? ' ! value必须是Object类型!' : CodeUtil . getComment ( '字段校正' , false , ' ' ) ;
814
804
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 , ' ' ) ;
816
810
}
817
811
return '' ;
818
812
}
819
813
return CodeUtil . getComment ( CodeUtil . getCommentFromDoc ( tableList , name , key ) , false , ' ' ) ;
820
814
}
821
815
816
+ alert ( 'name = ' + name + '; key = ' + key ) ;
822
817
if ( StringUtil . isEmpty ( name ) ) {
823
818
switch ( key ) {
824
819
case 'tag' :
825
- return ' 请求密钥';
820
+ return CodeUtil . getType4Request ( value ) != 'string' ? ' ! value必须是String类型!' : CodeUtil . getComment ( ' 请求密钥', false , ' ' ) ;
826
821
case 'version' :
827
- return ' 版本号';
822
+ return CodeUtil . getType4Request ( value ) != 'number' ? ' ! value必须是Number类型!' : CodeUtil . getComment ( ' 版本号', false , ' ' ) ;
828
823
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 , ' ' ) ;
830
829
}
831
830
}
832
831
@@ -885,6 +884,10 @@ var CodeUtil = {
885
884
}
886
885
887
886
return '' ;
887
+ } ,
888
+
889
+ getType4Request : function ( value ) {
890
+ return typeof JSON . parse ( value ) ;
888
891
}
889
892
890
893
}
0 commit comments