-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin-golang-const.js
48 lines (45 loc) · 1.16 KB
/
plugin-golang-const.js
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
// check if a const name contains a dash/dashes and replace it with an underline/underlines
function createConstName(d) {
return d.replace(/-/g, '_');
}
// check the value type and put double quotes around if it is a string
function createConstValue(d) {
var value = '';
var theType = typeof d;
if(theType === 'number' || theType === 'boolean') {
value = d;
} else {
value = '"'+d+'"';
}
return value;
}
// create all the constant variables
function createConsts(d) {
if(d.lenght !== 0) {
writeln('const (');
pushLevel();
for(var k in d) {
if(k != 'package' && k != 'PACKAGE') {
writeln(createConstName(k)+' = '+createConstValue(d[k]));
}
}
popLevel();
writeln(')');
}
}
writeln('// plugin-golang-const v0.1.0')
writeln()
if(data !== null) { // || Object.keys(data).length !== 0) {
var packageName = data.package || data.PACKAGE || 'main';
writeln('package '+packageName);
writeln();
if(data.const !== undefined) {
createConsts(data.const);
} else if(data.CONST !== undefined) {
createConsts(data.CONST);
} else {
createConsts(data);
}
} else {
writeln('// the dataset is empty!')
}