-
Notifications
You must be signed in to change notification settings - Fork 2
/
kotlin_spring_codegen.js
149 lines (117 loc) · 4.1 KB
/
kotlin_spring_codegen.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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
const parser = require('./geekspec_parser');
const fs = require('fs');
var code = fs.readFileSync('./geekapk_v1b_api.geekspec').toString();
code = code.replace(/#.*/g, '');
console.log(code);
console.log("\n\n\n")
let ast = parser.parse(code);
console.log(JSON.stringify(ast, null, 2));
function walkOption(o) {
return o;
}
function walkOptions(os) {
if (os == null) return "";
return "/* Maybe " + os.map(walkOption).join(" or ") + " */";
}
function walkArg(a, url) {
function translateArgLocation(l, n, optional = false) {
if (l == 'body') {
if (optional) console.log("// WARNING: Body parameters should not be optional, ignoring");
return `@RequestBody`;
}
if (l == 'path') {
if (typeof url != 'undefined' && !url.toString().includes(`{${n}}`))
console.log(`// WARNING: Path parameter ${n} not included in url ${url}`)
if (optional) return `@PathVariable(name = "${n}", required = false)`;
else return `@PathVariable("${n}")`;
}
return l;
}
let match = (a.name.match(/^(?<name_>[a-z]+):(?<type_>[A-Z][a-zA-Z]+)/));
if (match) {
let {groups: {name_, type_}} = match
if (typeof name_ != 'undefined') {
if (!a.required)
return `@RequestParam(name = "${name_}", required = false) ${name_}: ${type_}?${walkOptions(a.options)}`;
else return `@RequestParam("${name_}") ${name_}: ${type_}${walkOptions(a.options)}`;
}
}
let match_ = a.name.match(/^(?<name>[a-z]+)\-(?<location>[a-z]+):(?<type>[A-Z][a-zA-Z]+)/);
if (match_) {
let {groups: {name, location, type}} = match_
if (typeof name != 'undefined') {
if (a.required)
return `${translateArgLocation(location, name)} ${name}: ${type}${walkOptions(a.options)}`;
else return `${translateArgLocation(location, name, true)} ${name}: ${type}?${walkOptions(a.options)}`;
}
}
if (a.required)
return `@RequestParam("${a.name.split(':')[0]}}") ${a.name}${walkOptions(a.options)}`;
else return `@RequestParam(name = "${a.name.split(':')[0]}", required = false) ${a.name}?${walkOptions(a.options)}`;
}
function walkArgs(as, u) {
return as.map(a => walkArg(a, u))
.join(', ')
}
function translatePlainTypeName(name) {
const table = {
string: 'String',
number: 'Int',
boolean: 'Boolean',
datetime: 'Date',
plain: 'String'
};
let result = table[name];
if (typeof result == 'undefined')
return name;
else return result;
}
function translateReturnType(type, name) {
if (type == 'array')
return `List<${translatePlainTypeName(name)}>`;
if (type == 'object') {
if (name.match(/^[A-Z][A-Za-z0-9_].*/))
return name;
else return `Map<String, ${translatePlainTypeName(name)}>`;
}
}
function translateDictReturnType(name, type) {
return `/* ${name}: ${type} */`;
}
function selectPossibleType(dict) {
let d = new Map();
let set = new Set();
dict.forEach(function (e) {
//console.log(e);
for (var name of Object.getOwnPropertyNames(e)) {
d[name] = e[name];
//console.log(`// ${name}: ${e[name]}`);
}
});
for (var i in d) {
if (i == 'type') set.add(d[i]);
}
if (set.size == 1)
return translatePlainTypeName(set.values().next().value);
else console.log(`// Failed to select a possible type for ${JSON.stringify(d)}, expecting single typeId but found ${JSON.stringify(set)}`);
return "Any?";
}
function walkReturn(r) {
if (r == null) return "Unit";
if (typeof r != 'object') return translatePlainTypeName(r);
if (Array.isArray(r)) {
return `Map<String, ${selectPossibleType(r)}> ` + r.map(e => translateDictReturnType(e.name, e.type)).join('')
}
return translateReturnType(r.type, r.of);
}
function translatePathBindingAnnotations(verb, url) {
let prefix = verb[0].toUpperCase() + verb.substring(1, verb.length).toLowerCase()
return `@${prefix}Mapping("${url}")`
}
function walkInterface(i) {
console.log()
var rb = ""
if (i.return) { rb += "@ResponseBody\n" }
console.log(`${translatePathBindingAnnotations(i.method, i.url)}\n${rb}fun ${i.name}(${walkArgs(i.args, i.url)}): ${walkReturn(i.return)} {\n TODO()\n}\n`);
}
ast.forEach(walkInterface);