Skip to content

Commit

Permalink
Added legacy groups support to pbjs, see #568
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodeIO committed Dec 18, 2016
1 parent 974a132 commit fed2000
Show file tree
Hide file tree
Showing 13 changed files with 43 additions and 24 deletions.
20 changes: 19 additions & 1 deletion cli/targets/proto.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ function buildRanges(keyword, ranges) {
}

function buildType(type) {
if (type.group)
return; // built with the sister-field
push("");
push("message " + type.name + " {");
++indent;
Expand All @@ -172,12 +174,16 @@ function buildField(field, passExtend) {
return;
if (first)
first = false, push("");
if (field.resolvedType && field.resolvedType.group) {
buildGroup(field);
return;
}
var sb = [];
if (field.map)
sb.push("map<" + field.keyType + ", " + field.type + ">");
else if (field.repeated)
sb.push("repeated", field.type);
else if (syntax === 2)
else if (syntax === 2 || field.parent.group)
sb.push(field.required ? "required" : "optional", field.type);
else
sb.push(field.type);
Expand All @@ -188,6 +194,18 @@ function buildField(field, passExtend) {
push(sb.join(" ") + ";");
}

function buildGroup(field) {
push(field.rule + " group " + field.resolvedType.name + " = " + field.id + " {");
++indent;
buildOptions(field.resolvedType);
first = true;
field.resolvedType.fieldsArray.forEach(function(field) {
buildField(field);
});
--indent;
push("}");
}

function buildFieldOptions(field) {
var keys;
if (!field.options || !(keys = Object.keys(field.options)).length)
Expand Down
4 changes: 2 additions & 2 deletions dist/protobuf.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/protobuf.js.map

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions dist/protobuf.min.js

Large diffs are not rendered by default.

Binary file modified dist/protobuf.min.js.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion dist/protobuf.min.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/runtime/protobuf.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/runtime/protobuf.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified dist/runtime/protobuf.min.js.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion src/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ ServicePrototype.remove = function remove(object) {
ServicePrototype.create = function create(rpcImpl, requestDelimited, responseDelimited) {
var rpcService = new rpc.Service(rpcImpl);
this.getMethodsArray().forEach(function(method) {
rpcService[method.name.substring(0, 1).toLowerCase() + method.name.substring(1)] = function callVirtual(request, /* optional */ callback) {
rpcService[util.lcFirst(method.name)] = function callVirtual(request, /* optional */ callback) {
if (!rpcService.$rpc) // already ended?
return;
if (!request)
Expand Down
1 change: 1 addition & 0 deletions tests/data/groups.proto
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
message Test {
required group MyGroup = 1 {
option foo = "bar";
required uint32 a = 2;
};
}
16 changes: 8 additions & 8 deletions tests/data/mapbox/vector_tile.js
Original file line number Diff line number Diff line change
Expand Up @@ -350,15 +350,15 @@ $root.vector_tile = (function() {
return"invalid value for field .vector_tile.Tile.Value.doubleValue (number expected)"
}
if(m["intValue"]!==undefined){
if(!(util.isInteger(m["intValue"])||m["intValue"]&&util.isInteger(m["intValue"].low)&&util.isInteger(m["intValue"].high)))
if(!util.isInteger(m["intValue"])&&!(m["intValue"]&&util.isInteger(m["intValue"].low)&&util.isInteger(m["intValue"].high)))
return"invalid value for field .vector_tile.Tile.Value.intValue (integer|Long expected)"
}
if(m["uintValue"]!==undefined){
if(!(util.isInteger(m["uintValue"])||m["uintValue"]&&util.isInteger(m["uintValue"].low)&&util.isInteger(m["uintValue"].high)))
if(!util.isInteger(m["uintValue"])&&!(m["uintValue"]&&util.isInteger(m["uintValue"].low)&&util.isInteger(m["uintValue"].high)))
return"invalid value for field .vector_tile.Tile.Value.uintValue (integer|Long expected)"
}
if(m["sintValue"]!==undefined){
if(!(util.isInteger(m["sintValue"])||m["sintValue"]&&util.isInteger(m["sintValue"].low)&&util.isInteger(m["sintValue"].high)))
if(!util.isInteger(m["sintValue"])&&!(m["sintValue"]&&util.isInteger(m["sintValue"].low)&&util.isInteger(m["sintValue"].high)))
return"invalid value for field .vector_tile.Tile.Value.sintValue (integer|Long expected)"
}
if(m["boolValue"]!==undefined){
Expand Down Expand Up @@ -438,18 +438,18 @@ $root.vector_tile = (function() {
if(m["id"]!==undefined&&util.longNe(m["id"],0,0))
w.uint32(8).uint64(m["id"])
if(m["tags"]&&m["tags"].length){
w.fork()
w.uint32(18).fork()
for(var i=0;i<m["tags"].length;++i)
w.uint32(m["tags"][i])
w.ldelim(2)
w.ldelim()
}
if(m["type"]!==undefined&&m["type"]!=="UNKNOWN")
w.uint32(24).uint32(m["type"])
if(m["geometry"]&&m["geometry"].length){
w.fork()
w.uint32(34).fork()
for(var i=0;i<m["geometry"].length;++i)
w.uint32(m["geometry"][i])
w.ldelim(4)
w.ldelim()
}
return w
}
Expand Down Expand Up @@ -539,7 +539,7 @@ $root.vector_tile = (function() {
var types; $lazyTypes.push(types = [null,null,"vector_tile.Tile.GeomType",null]);
return function verify(m) {
if(m["id"]!==undefined){
if(!(util.isInteger(m["id"])||m["id"]&&util.isInteger(m["id"].low)&&util.isInteger(m["id"].high)))
if(!util.isInteger(m["id"])&&!(m["id"]&&util.isInteger(m["id"].low)&&util.isInteger(m["id"].high)))
return"invalid value for field .vector_tile.Tile.Feature.id (integer|Long expected)"
}
if(m["tags"]!==undefined){
Expand Down
10 changes: 5 additions & 5 deletions tests/groups.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ tape.test("legacy groups", function(test) {
});

function verifyEncode(test, buf) {
test.equal(buf.length, 4, "should encode a total of 4 bytes");
test.equal(buf[0], 1 << 3 | 3, "should encode id 1, wireType 3");
test.equal(buf[1], 2 << 3 | 0, "should encode id 2, wireType 0");
test.equal(buf[2], 111, "should encode value 111");
test.equal(buf[3], 1 << 3 | 4, "should encode id 1, wireType 4");
test.equal(buf.length, 4, "a total of 4 bytes");
test.equal(buf[0], 1 << 3 | 3, "id 1, wireType 3");
test.equal(buf[1], 2 << 3 | 0, "id 2, wireType 0");
test.equal(buf[2], 111, "111");
test.equal(buf[3], 1 << 3 | 4, "id 1, wireType 4");
}

0 comments on commit fed2000

Please sign in to comment.