Skip to content

Commit

Permalink
Integrated types fixes into our tsd-jsdoc fork, now using void as ret…
Browse files Browse the repository at this point in the history
…urn type for undefined, see #527; updated internals and static target to use immutable objects on prototypes
  • Loading branch information
dcodeIO committed Dec 7, 2016
1 parent 9df6a3d commit 1d99442
Show file tree
Hide file tree
Showing 17 changed files with 292 additions and 201 deletions.
31 changes: 13 additions & 18 deletions cli/targets/proto.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@ proto_target.private = true;

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

var Namespace = protobuf.Namespace,
Enum = protobuf.Enum,
Type = protobuf.Type,
Field = protobuf.Field,
OneOf = protobuf.OneOf,
Service = protobuf.Service,
Method = protobuf.Method,
types = protobuf.types,
util = protobuf.util;
var Namespace = protobuf.Namespace,
Enum = protobuf.Enum,
Type = protobuf.Type,
Field = protobuf.Field,
OneOf = protobuf.OneOf,
Service = protobuf.Service,
Method = protobuf.Method,
types = protobuf.types,
util = protobuf.util;
var underScore = protobuf.util.underScore;

var out = [];
var indent = 0;
Expand Down Expand Up @@ -58,12 +59,6 @@ function push(line) {
out.push(ind + line);
}

function under_score(name) {
return name.substring(0,1)
+ name.substring(1)
.replace(/([A-Z])(?=[a-z]|$)/g, function($0, $1) { return "_" + $1.toLowerCase(); });
}

function escape(str) {
return str.replace(/[\\"']/g, '\\$&')
.replace(/\u0000/g, '\\0');
Expand Down Expand Up @@ -186,7 +181,7 @@ function buildField(field, passExtend) {
sb.push(field.required && "required" || "optional", field.type);
else
sb.push(field.type);
sb.push(under_score(field.name), "=", field.id);
sb.push(underScore(field.name), "=", field.id);
var opts = buildFieldOptions(field);
if (opts)
sb.push(opts);
Expand Down Expand Up @@ -252,14 +247,14 @@ function consolidateExtends(nested) {

function buildOneOf(oneof) {
push("");
push("oneof " + under_score(oneof.name) + " {");
push("oneof " + underScore(oneof.name) + " {");
++indent; first = true;
oneof.oneof.forEach(function(fieldName) {
var field = oneof.parent.get(fieldName);
if (first)
push(""), first = false;
var opts = buildFieldOptions(field);
push(field.type + " " + under_score(field.name) + " = " + field.id + (opts ? " " + opts : "") + ";");
push(field.type + " " + underScore(field.name) + " = " + field.id + (opts ? " " + opts : "") + ";");
});
--indent;
push("}");
Expand Down
2 changes: 1 addition & 1 deletion cli/targets/static-module.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
/* Global */ else
global.root = factory(global.protobuf);
})(this, function($runtime) {
})(this, function($protobuf) {
"use strict";
%OUTPUT%
Expand Down
77 changes: 62 additions & 15 deletions cli/targets/static.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@ var Type = protobuf.Type,
Service = protobuf.Service,
Enum = protobuf.Enum,
Namespace = protobuf.Namespace,
encoder = protobuf.encoder,
decoder = protobuf.decoder,
verifier = protobuf.verifier,
util = protobuf.util;
util = protobuf.util,
codegen = protobuf.codegen;

var out = [];
var indent = 0;
Expand Down Expand Up @@ -168,12 +166,61 @@ function buildType(ref, type) {
push("}");
--indent;
push("}");
push("");

// default values
type.fieldsArray.forEach(function(field) {
field.resolve();
if (typeof field.defaultValue === 'object' && field.defaultValue)
return;
push(name(type.name) + ".prototype." + name(field.name) + " = " +JSON.stringify(field.defaultValue) + ";");
var jsType;
switch (field.type) {
case "double":
case "float":
case "int32":
case "uint32":
case "sint32":
case "fixed32":
case "sfixed32":
jsType = "number";
break;
case "int64":
case "uint64":
case "sint64":
case "fixed64":
case "sfixed64":
jsType = "number|Long";
break;
case "bool":
jsType = "boolean";
break;
case "string":
jsType = "string";
break;
case "bytes":
jsType = "Uint8Array";
break;
default:
if (field.resolvedType instanceof Enum) {
jsType = "number";
} else if (field.resolvedType instanceof Type) {
jsType = field.resolvedType.fullName.substring(1);
} else {
jsType = "*"; // should not happen
}
break;
}
if (field.repeated)
jsType = "Array.<" + jsType + ">";
push("");
pushComment([
field.name + ".",
"@name " + fullName + "#" + name(field.name),
"@type {" + jsType + "}"
]);
if (Array.isArray(field.defaultValue)) {
push(name(type.name) + ".prototype[" + JSON.stringify(field.name) + "] = $protobuf.util.emptyArray;");
} else if (util.isObject(field.defaultValue))
push(name(type.name) + ".prototype[" + JSON.stringify(field.name) + "] = $protobuf.util.emptyObject;");
else
push(name(type.name) + ".prototype[" + JSON.stringify(field.name) + "] = " + JSON.stringify(field.defaultValue) + ";");
});

// #encode
Expand All @@ -185,9 +232,9 @@ function buildType(ref, type) {
"@param {Writer} [writer] Writer to encode to",
"@returns {Writer} Writer"
]);
buildFunction(type, "encode", encoder.generate(type), {
Writer : "$runtime.Writer",
util : "$runtime.util"
buildFunction(type, "encode", codegen.encode.generate(type), {
Writer : "$protobuf.Writer",
util : "$protobuf.util"
});

// #encodeDelimited
Expand All @@ -213,9 +260,9 @@ function buildType(ref, type) {
"@param {number} [length] Message length if known beforehand",
"@returns {" + fullName + "} " + type.name
]);
buildFunction(type, "decode", decoder.generate(type), {
Reader : "$runtime.Reader",
util : "$runtime.util"
buildFunction(type, "decode", codegen.decode.generate(type), {
Reader : "$protobuf.Reader",
util : "$protobuf.util"
});

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

function buildService(ref, service) {
Expand Down
Loading

0 comments on commit 1d99442

Please sign in to comment.