forked from mafintosh/protocol-buffers-schema
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stringify.js
86 lines (67 loc) · 1.89 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
var onfield = function(f, result) {
var prefix = f.repeated ? 'repeated' : f.required ? 'required' : 'optional'
var opts = Object.keys(f.options || {}).map(function(key) {
return key+' = '+f.options[key]
}).join(',')
if (opts) opts = ' ['+opts+']'
result.push(prefix+' '+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, []))
})
if (!m.fields) m.fields = []
m.fields.forEach(function(f) {
result.push(onfield(f, []))
})
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 (typeof v === 'string' && option !== 'optimize_for') v = '"'+v+'"'
result.push('option '+option+' = '+v+';')
})
if (keys.length > 0) {
result.push('')
}
}
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 = []
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)
})
return result.map(indent('')).join('\n')
}