19
19
var CodeUtil = {
20
20
TAG : 'CodeUtil' ,
21
21
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
+
22
84
/**解析出 生成iOS-Swift请求JSON 的代码
23
85
* 只需要把所有 对象标识{} 改为数组标识 []
24
86
* @param name
@@ -670,6 +732,128 @@ var CodeUtil = {
670
732
var index = column . lastIndexOf ( '_' ) ; // snake_case
671
733
var id = index < 0 ? column : column . substring ( index + 1 ) ;
672
734
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 '' ;
673
857
}
674
858
675
859
}
0 commit comments