json.js
7.3 KB
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*
Copyright 2013 Daniel Wirtz <dcode@dcode.io>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
var description = "Plain JSON descriptor";
var ProtoBuf = require(__dirname+"/../../../index.js"),
util = require("../util.js");
/**
* pbjs target: Plain JSON descriptor
* @exports pbjs/targets/json
* @function
* @param {!ProtoBuf.Builder} builder Builder
* @param {!Object.<string,*>=} options Options
* @returns {string}
*/
var json = module.exports = function(builder, options) {
options = options || {};
builder.resolveAll();
// Set the pointer to the lowest common namespace (with options)
var ptr = builder.ns;
while (ptr.children.length === 1 && Object.keys(ptr.options).length === 0 && ptr.children[0].className === "Namespace")
ptr = ptr.children[0];
// Start by building the package namespace
var pkg = ptr.fqn().substring(1),
out = {
"package": pkg !== "" ? pkg : null
};
buildNamespace(ptr, out);
return JSON.stringify(out, null, options.min ? 0 : 4);
};
/**
* Module description.
* @type {string}
*/
json.description = description;
/**
* Builds all structures in a namespace.
* @param {!ProtoBuf.Reflect.Namespace} ns Namespace to build
* @param {!Object.<string,*>} out Extended output object
*/
function buildNamespace(ns, out) {
var messages, enums, services;
util.extend(out, {
"options" : out.options || {},
"messages" : messages = [],
"enums" : enums = [],
"services" : services = []
});
util.extend(out["options"], buildOptions(ns.options));
ns.getChildren(ProtoBuf.Reflect.Enum).forEach(function(enm) {
enums.push(buildEnum(enm));
});
if (enums.length === 0)
delete out["enums"];
ns.getChildren(ProtoBuf.Reflect.Message).forEach(function(msg) {
messages.push(buildMessage(msg));
});
ns.getChildren(ProtoBuf.Reflect.Service).forEach(function(svc) {
services.push(buildService(svc));
});
if (services.length === 0)
delete out["services"];
Array.prototype.push.apply(messages, buildExtensions(ns));
ns.getChildren(ProtoBuf.Reflect.Namespace).forEach(function(innerNs) {
if (innerNs.className !== "Namespace")
return;
var emptyMessage = {
"name": innerNs.name,
"fields": []
};
buildNamespace(innerNs, emptyMessage);
messages.push(emptyMessage);
});
if (messages.length === 0)
delete out["messages"];
if (Object.keys(out["options"]).length === 0)
delete out["options"];
}
/**
* Builds extensions declared in the specified namespace.
* @param {!ProtoBuf.Reflect.Namespace} ns Namespace
* @returns {!Array.<!*>}
*/
function buildExtensions(ns) {
var exts = util.groupExtensions(ns);
if (exts === null)
return [];
var messages = [];
Object.keys(exts).forEach(function(extFqn) {
var extMsg = ns.resolve(extFqn),
extFields = exts[extFqn];
var fields, ext = {
"ref" : ns.qn(extMsg),
"fields" : fields = []
};
extFields.forEach(function(extField) {
fields.push(buildMessageField(extField));
});
messages.push(ext);
});
return messages;
}
/**
* Builds block-level options.
* @param {!Object.<string,*>} options Options
* @returns {!Object.<string,*>}
*/
function buildOptions(options) {
Object.keys(options = options || {}).forEach(function(key) {
var val = options[key];
switch (typeof val) {
case 'string':
case 'number':
case 'boolean':
break;
default:
throw Error("Illegal option type: "+typeof(val));
}
});
return options;
}
/**
* Builds a message.
* @param {!ProtoBuf.Reflect.Message} msg Message
* @returns {!*}
*/
function buildMessage(msg) {
var fields, oneofs;
var out = {
"name" : msg.name,
"options" : {},
"fields" : fields = [],
"oneofs" : oneofs = {}
};
msg.getChildren(ProtoBuf.Reflect.Message.Field).forEach(function(fld) {
if (fld instanceof ProtoBuf.Reflect.Message.ExtensionField)
return;
fields.push(buildMessageField(fld));
});
msg.getChildren(ProtoBuf.Reflect.Message.OneOf).forEach(function(oneof) {
oneofs[oneof.name] = buildMessageOneof(oneof);
});
if (msg.extensions)
out["extensions"] = msg.extensions;
if (Object.keys(oneofs).length === 0)
delete out["oneofs"];
buildNamespace(msg, out);
return out;
}
/**
* Builds a message field.
* @param {!ProtoBuf.Reflect.Message.Field} fld Message field
* @returns {!*}
*/
function buildMessageField(fld) {
return {
"rule" : fld.map ? "map" : (fld.repeated ? "repeated" : (fld.required ? "required" : "optional")),
"type" : fld.resolvedType ? fld.parent.qn(fld.resolvedType) : fld.type['name'],
"keytype" : (typeof(fld.keyType) === 'string') ? fld.keyType : (fld.keyType !== null ? fld.keyType.name : undefined),
"name" : fld instanceof ProtoBuf.Reflect.Message.ExtensionField ? fld.name.substring(fld.name.lastIndexOf(".")+1): fld.name,
"id" : fld.id,
"options" : Object.keys(fld.options).length > 0 ? buildOptions(fld.options) : undefined,
"oneof" : fld.oneof ? fld.oneof.name : undefined
};
}
/**
* Builds a message oneof.
* @param {!ProtoBuf.Reflect.message.OneOf} oneof Message oneof
* @returns {!Array.<!*>}
*/
function buildMessageOneof(oneof) {
var out = [];
oneof.fields.forEach(function(fld) {
out.push(fld.id);
});
return out;
}
/**
* Builds an enum.
* @param {!ProtoBuf.Reflect.Enum} enm Enum
* @returns {!*}
*/
function buildEnum(enm) {
var values;
var out = {
"name" : enm.name,
"values" : values = []
};
enm.getChildren(ProtoBuf.Reflect.Enum.Value).forEach(function(val) {
values.push(buildEnumValue(val));
});
if (Object.keys(enm.options).length > 0)
out["options"] = buildOptions(enm.options);
return out;
}
/**
* Builds an enum value.
* @param {!ProtoBuf.Reflect.Enum.Value} val Enum value
* @returns {!*}
*/
function buildEnumValue(val) {
return {
"name" : val.name,
"id" : val.id
};
}
/**
* Builds a service.
* @param {!ProtoBuf.Reflect.Service} svc Service
* @returns {!*}
*/
function buildService(svc) {
var rpc;
var out = {
"name": svc.name,
"options": buildOptions(svc.options),
"rpc": rpc = {}
};
svc.getChildren(ProtoBuf.Reflect.Service.RPCMethod).forEach(function(mtd) {
rpc[mtd.name] = {
"request": svc.qn(mtd.resolvedRequestType),
"response": svc.qn(mtd.resolvedResponseType),
"options": buildOptions(mtd.options)
};
});
return out;
}