forked from mafintosh/protocol-buffers-schema
-
Notifications
You must be signed in to change notification settings - Fork 1
/
stringify.js
183 lines (142 loc) · 4.08 KB
/
stringify.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
var onfield = function (f, result) {
var prefix = f.repeated ? 'repeated' : f.required ? 'required' : 'optional'
if (f.type === 'map') prefix = 'map<' + f.map.from + ',' + f.map.to + '>'
if (f.oneof) prefix = ''
var opts = Object.keys(f.options || {}).map(function (key) {
return key + ' = ' + f.options[key]
}).join(',')
if (opts) opts = ' [' + opts + ']'
result.push((prefix ? prefix + ' ' : '') + (f.map === 'map' ? '' : f.type + ' ') + f.name + ' = ' + f.tag + opts + ';')
return result
}
var onmessage = function (m, result) {
result.push('message ' + m.name + ' {')
if (!m.enums) m.enums = []
m.enums.forEach(function (e) {
result.push(onenum(e, []))
})
if (!m.messages) m.messages = []
m.messages.forEach(function (m) {
result.push(onmessage(m, []))
})
var oneofs = {}
if (!m.fields) m.fields = []
m.fields.forEach(function (f) {
if (f.oneof) {
if (!oneofs[f.oneof]) oneofs[f.oneof] = []
oneofs[f.oneof].push(onfield(f, []))
} else {
result.push(onfield(f, []))
}
})
Object.keys(oneofs).forEach(function (n) {
oneofs[n].unshift('oneof ' + n + ' {')
oneofs[n].push('}')
result.push(oneofs[n])
})
result.push('}', '')
return result
}
var onenum = function (e, result) {
result.push('enum ' + e.name + ' {')
var vals = Object.keys(e.values).map(function (key) {
return key + ' = ' + e.values[key] + ';'
})
result.push(vals)
result.push('}', '')
return result
}
var onoption = function (o, result) {
var keys = Object.keys(o)
keys.forEach(function (option) {
var v = o[option]
if (~option.indexOf('.')) option = '(' + option + ')'
var type = typeof v
if (type === 'object') {
v = onoptionMap(v, [])
if (v.length) result.push('option ' + option + ' = {', v, '};')
} else {
if (type === 'string' && option !== 'optimize_for') v = '"' + v + '"'
result.push('option ' + option + ' = ' + v + ';')
}
})
if (keys.length > 0) {
result.push('')
}
return result
}
var onoptionMap = function (o, result) {
var keys = Object.keys(o)
keys.forEach(function (k) {
var v = o[k]
var type = typeof v
if (type === 'object') {
if (Array.isArray(v)) {
v.forEach(function (v) {
v = onoptionMap(v, [])
if (v.length) result.push(k + ' {', v, '}')
})
} else {
v = onoptionMap(v, [])
if (v.length) result.push(k + ' {', v, '}')
}
} else {
if (type === 'string') v = '"' + v + '"'
result.push(k + ': ' + v)
}
})
return result
}
var onservices = function (s, result) {
result.push('service ' + s.name + ' {')
if (!s.options) s.options = {}
onoption(s.options, result)
if (!s.methods) s.methods = []
s.methods.forEach(function (m) {
result.push(onrpc(m, []))
})
result.push('}', '')
return result
}
var onrpc = function (rpc, result) {
var def = 'rpc ' + rpc.name + '('
if (rpc.client_streaming) def += 'stream '
def += rpc.input_type + ') returns ('
if (rpc.server_streaming) def += 'stream '
def += rpc.output_type + ')'
if (!rpc.options) rpc.options = {}
var options = onoption(rpc.options, [])
if (options.length > 1) {
result.push(def + ' {', options.slice(0, -1), '}')
} else {
result.push(def + ';')
}
return result
}
var indent = function (lvl) {
return function (line) {
if (Array.isArray(line)) return line.map(indent(lvl + ' ')).join('\n')
return lvl + line
}
}
module.exports = function (schema) {
var result = []
result.push('syntax = "proto' + schema.syntax + '";', '')
if (schema.package) result.push('package ' + schema.package + ';', '')
if (!schema.options) schema.options = {}
onoption(schema.options, result)
if (!schema.enums) schema.enums = []
schema.enums.forEach(function (e) {
onenum(e, result)
})
if (!schema.messages) schema.messages = []
schema.messages.forEach(function (m) {
onmessage(m, result)
})
if (schema.services) {
schema.services.forEach(function (s) {
onservices(s, result)
})
}
return result.map(indent('')).join('\n')
}