-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJSON-camelCase-comment.json.groovy
94 lines (87 loc) · 3.47 KB
/
JSON-camelCase-comment.json.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
* Available settings:
* PRINT_COMMENT { true, false } controls whether to print comment
* PRINT_ALL_COMMENTS { true, false } whether to print all comments
* INDENT space indentation size, default to 2 spaces
*
* Note:
* PRINT_COMMENT feature is not available for multi-table selects
*/
import static com.intellij.openapi.util.text.StringUtil.escapeStringCharacters as escapeStr
import com.intellij.database.util.Case
PRINT_COMMENT = true
PRINT_ALL_COMMENTS = false
NEWLINE = System.getProperty("line.separator")
INDENT = " "
COL_COMMENTS = new LinkedHashMap<String, String>()
if (TABLE != null) {
TABLE.getDasChildren(com.intellij.database.model.ObjectKind.COLUMN).each { column ->
COL_COMMENTS.put(column.getName(), column.getComment())
}
} else {
PRINT_COMMENT = false
OUT.append("`print comment` feature is not available for multi-table selects due to Intellij's SDK limitations!$NEWLINE$NEWLINE")
}
if (PRINT_ALL_COMMENTS && TABLE != null) {
COL_COMMENTS.entrySet().each { column ->
OUT.append(javaName(column.getKey(), false) + ": " + column.getValue() + NEWLINE)
}
OUT.append(NEWLINE)
}
def printJSON(level, col, o) {
switch (o) {
case null: OUT.append("null"); break
case Number: OUT.append("$o"); break
case Boolean: OUT.append("$o"); break
case String: OUT.append("\"${escapeStr(o)}\""); break
case Tuple: printJSON(level, o[0], o[1]); break
case Map:
OUT.append("{${NEWLINE}")
def es = o.entrySet()
def esSize = es.size()
es.eachWithIndex { entry, i ->
OUT.append("${INDENT * (level + 1)}")
OUT.append("\"${escapeStr(javaName(entry.getKey().toString(), false))}\"")
OUT.append(": ")
printJSON(level + 1, null, entry.getValue())
OUT.append("${i < esSize - 1 ? "," : ""}")
if (PRINT_COMMENT && COL_COMMENTS[entry.getKey().toString()]) {
OUT.append(" // ${entry.getKey().toString()}: ${COL_COMMENTS[entry.getKey().toString()]}")
}
OUT.append(NEWLINE)
}
OUT.append("${INDENT * level}}")
break
case Object[]:
case Iterable:
OUT.append("[")
def plain = true
o.eachWithIndex { item, i ->
plain = item == null || item instanceof Number || item instanceof Boolean || item instanceof String
if (plain) OUT.append(i > 0 ? ", " : "")
else OUT.append("${i > 0 ? "," : ""}$NEWLINE${INDENT * (level + 1)}")
printJSON(level + 1, null, item)
}
if (plain) OUT.append("]") else OUT.append("$NEWLINE${INDENT * level}]")
break
default:
if (col != null) printJSON(level, null, FORMATTER.formatValue(o, col))
else OUT.append("$o")
break
}
}
printJSON(0, null, ROWS.transform { row ->
def map = new LinkedHashMap<String, String>()
COLUMNS.each { col ->
def val = row.value(col)
map.put(col.name(), new Tuple(col, val))
}
map
})
def javaName(str, capitalize) {
def s = com.intellij.psi.codeStyle.NameUtil.splitNameIntoWords(str)
.collect { Case.LOWER.apply(it).capitalize() }
.join("")
.replaceAll(/[^\p{javaJavaIdentifierPart}[_]]/, "_")
capitalize || s.length() == 1 ? s : Case.LOWER.apply(s[0]) + s[1..-1]
}