Skip to content

Commit 1d99442

Browse files
committed
Integrated types fixes into our tsd-jsdoc fork, now using void as return type for undefined, see #527; updated internals and static target to use immutable objects on prototypes
1 parent 9df6a3d commit 1d99442

17 files changed

+292
-201
lines changed

cli/targets/proto.js

+13-18
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@ proto_target.private = true;
55

66
var protobuf = require("../..");
77

8-
var Namespace = protobuf.Namespace,
9-
Enum = protobuf.Enum,
10-
Type = protobuf.Type,
11-
Field = protobuf.Field,
12-
OneOf = protobuf.OneOf,
13-
Service = protobuf.Service,
14-
Method = protobuf.Method,
15-
types = protobuf.types,
16-
util = protobuf.util;
8+
var Namespace = protobuf.Namespace,
9+
Enum = protobuf.Enum,
10+
Type = protobuf.Type,
11+
Field = protobuf.Field,
12+
OneOf = protobuf.OneOf,
13+
Service = protobuf.Service,
14+
Method = protobuf.Method,
15+
types = protobuf.types,
16+
util = protobuf.util;
17+
var underScore = protobuf.util.underScore;
1718

1819
var out = [];
1920
var indent = 0;
@@ -58,12 +59,6 @@ function push(line) {
5859
out.push(ind + line);
5960
}
6061

61-
function under_score(name) {
62-
return name.substring(0,1)
63-
+ name.substring(1)
64-
.replace(/([A-Z])(?=[a-z]|$)/g, function($0, $1) { return "_" + $1.toLowerCase(); });
65-
}
66-
6762
function escape(str) {
6863
return str.replace(/[\\"']/g, '\\$&')
6964
.replace(/\u0000/g, '\\0');
@@ -186,7 +181,7 @@ function buildField(field, passExtend) {
186181
sb.push(field.required && "required" || "optional", field.type);
187182
else
188183
sb.push(field.type);
189-
sb.push(under_score(field.name), "=", field.id);
184+
sb.push(underScore(field.name), "=", field.id);
190185
var opts = buildFieldOptions(field);
191186
if (opts)
192187
sb.push(opts);
@@ -252,14 +247,14 @@ function consolidateExtends(nested) {
252247

253248
function buildOneOf(oneof) {
254249
push("");
255-
push("oneof " + under_score(oneof.name) + " {");
250+
push("oneof " + underScore(oneof.name) + " {");
256251
++indent; first = true;
257252
oneof.oneof.forEach(function(fieldName) {
258253
var field = oneof.parent.get(fieldName);
259254
if (first)
260255
push(""), first = false;
261256
var opts = buildFieldOptions(field);
262-
push(field.type + " " + under_score(field.name) + " = " + field.id + (opts ? " " + opts : "") + ";");
257+
push(field.type + " " + underScore(field.name) + " = " + field.id + (opts ? " " + opts : "") + ";");
263258
});
264259
--indent;
265260
push("}");

cli/targets/static-module.tpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
/* Global */ else
1010
global.root = factory(global.protobuf);
1111
12-
})(this, function($runtime) {
12+
})(this, function($protobuf) {
1313
"use strict";
1414
1515
%OUTPUT%

cli/targets/static.js

+62-15
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,8 @@ var Type = protobuf.Type,
99
Service = protobuf.Service,
1010
Enum = protobuf.Enum,
1111
Namespace = protobuf.Namespace,
12-
encoder = protobuf.encoder,
13-
decoder = protobuf.decoder,
14-
verifier = protobuf.verifier,
15-
util = protobuf.util;
12+
util = protobuf.util,
13+
codegen = protobuf.codegen;
1614

1715
var out = [];
1816
var indent = 0;
@@ -168,12 +166,61 @@ function buildType(ref, type) {
168166
push("}");
169167
--indent;
170168
push("}");
171-
push("");
169+
170+
// default values
172171
type.fieldsArray.forEach(function(field) {
173172
field.resolve();
174-
if (typeof field.defaultValue === 'object' && field.defaultValue)
175-
return;
176-
push(name(type.name) + ".prototype." + name(field.name) + " = " +JSON.stringify(field.defaultValue) + ";");
173+
var jsType;
174+
switch (field.type) {
175+
case "double":
176+
case "float":
177+
case "int32":
178+
case "uint32":
179+
case "sint32":
180+
case "fixed32":
181+
case "sfixed32":
182+
jsType = "number";
183+
break;
184+
case "int64":
185+
case "uint64":
186+
case "sint64":
187+
case "fixed64":
188+
case "sfixed64":
189+
jsType = "number|Long";
190+
break;
191+
case "bool":
192+
jsType = "boolean";
193+
break;
194+
case "string":
195+
jsType = "string";
196+
break;
197+
case "bytes":
198+
jsType = "Uint8Array";
199+
break;
200+
default:
201+
if (field.resolvedType instanceof Enum) {
202+
jsType = "number";
203+
} else if (field.resolvedType instanceof Type) {
204+
jsType = field.resolvedType.fullName.substring(1);
205+
} else {
206+
jsType = "*"; // should not happen
207+
}
208+
break;
209+
}
210+
if (field.repeated)
211+
jsType = "Array.<" + jsType + ">";
212+
push("");
213+
pushComment([
214+
field.name + ".",
215+
"@name " + fullName + "#" + name(field.name),
216+
"@type {" + jsType + "}"
217+
]);
218+
if (Array.isArray(field.defaultValue)) {
219+
push(name(type.name) + ".prototype[" + JSON.stringify(field.name) + "] = $protobuf.util.emptyArray;");
220+
} else if (util.isObject(field.defaultValue))
221+
push(name(type.name) + ".prototype[" + JSON.stringify(field.name) + "] = $protobuf.util.emptyObject;");
222+
else
223+
push(name(type.name) + ".prototype[" + JSON.stringify(field.name) + "] = " + JSON.stringify(field.defaultValue) + ";");
177224
});
178225

179226
// #encode
@@ -185,9 +232,9 @@ function buildType(ref, type) {
185232
"@param {Writer} [writer] Writer to encode to",
186233
"@returns {Writer} Writer"
187234
]);
188-
buildFunction(type, "encode", encoder.generate(type), {
189-
Writer : "$runtime.Writer",
190-
util : "$runtime.util"
235+
buildFunction(type, "encode", codegen.encode.generate(type), {
236+
Writer : "$protobuf.Writer",
237+
util : "$protobuf.util"
191238
});
192239

193240
// #encodeDelimited
@@ -213,9 +260,9 @@ function buildType(ref, type) {
213260
"@param {number} [length] Message length if known beforehand",
214261
"@returns {" + fullName + "} " + type.name
215262
]);
216-
buildFunction(type, "decode", decoder.generate(type), {
217-
Reader : "$runtime.Reader",
218-
util : "$runtime.util"
263+
buildFunction(type, "decode", codegen.decode.generate(type), {
264+
Reader : "$protobuf.Reader",
265+
util : "$protobuf.util"
219266
});
220267

221268
// #decodeDelimited
@@ -239,7 +286,7 @@ function buildType(ref, type) {
239286
"@param {" + fullName + "|Object} message " + type.name + " or plain object to verify",
240287
"@returns {?string} `null` if valid, otherwise the reason why it is not"
241288
]);
242-
buildFunction(type, "verify", verifier.generate(type), {});
289+
buildFunction(type, "verify", codegen.verify.generate(type), {});
243290
}
244291

245292
function buildService(ref, service) {

0 commit comments

Comments
 (0)