Skip to content

Commit 3a20968

Browse files
committed
Other: Properly remove unnecessary (packed) options from JSON descriptors
1 parent 056ecc3 commit 3a20968

File tree

10 files changed

+66
-95
lines changed

10 files changed

+66
-95
lines changed

cli/targets/proto.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ function buildFieldOptions(field) {
229229
var sb = [];
230230
keys.forEach(function(key) {
231231
var val = field.options[key];
232-
var wireType = types.packed[field.resolvedType instanceof Enum ? "uint32" : field.type];
232+
var wireType = types.packed[field.resolvedType instanceof Enum ? "int32" : field.type];
233233
switch (key) {
234234
case "packed":
235235
val = Boolean(val);

ext/descriptor/index.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -234,16 +234,16 @@ Type.prototype.toDescriptor = function toDescriptor(syntax) {
234234
if (this._fieldsArray[i] instanceof MapField) { // map fields are repeated FieldNameEntry
235235
var keyType = toDescriptorType(this._fieldsArray[i].keyType, this._fieldsArray[i].resolvedKeyType),
236236
valueType = toDescriptorType(this._fieldsArray[i].type, this._fieldsArray[i].resolvedType),
237-
valueTypeName = valueType === 11 || valueType === 14
237+
valueTypeName = valueType === /* type */ 11 || valueType === /* enum */ 14
238238
? this._fieldsArray[i].resolvedType && this._fieldsArray[i].resolvedType.fullName || this._fieldsArray[i].type
239239
: undefined;
240240
descriptor.nestedType.push(exports.DescriptorProto.create({
241-
name: descriptor.field[descriptor.field.length - 1].type_name,
241+
name: descriptor.field[descriptor.field.length - 1].typeName,
242242
field: [
243-
exports.FieldDescriptorProto.create({ name: "key", number: 1, label: 1, type: keyType }), // can't be message
243+
exports.FieldDescriptorProto.create({ name: "key", number: 1, label: 1, type: keyType }), // can't reference a type or enum
244244
exports.FieldDescriptorProto.create({ name: "value", number: 2, label: 1, type: valueType, typeName: valueTypeName })
245245
],
246-
options: exports.MessageOptions({ mapEntry: true })
246+
options: exports.MessageOptions.create({ mapEntry: true })
247247
}));
248248
}
249249
}
@@ -482,7 +482,7 @@ Field.prototype.toDescriptor = function toDescriptor(syntax) {
482482
case 10: // group
483483
case 11: // type
484484
case 14: // enum
485-
descriptor.typeName = this.resolvedType ? this.resolvedType.fullName : this.type;
485+
descriptor.typeName = this.resolvedType ? this.resolvedType.fullName : this.type; // TODO: shorten
486486
break;
487487
}
488488

@@ -567,7 +567,8 @@ Enum.fromDescriptor = function fromDescriptor(descriptor) {
567567

568568
return new Enum(
569569
descriptor.name && descriptor.name.length ? descriptor.name : "Enum" + unnamedEnumIndex++,
570-
values
570+
values,
571+
descriptor.options && descriptor.options.allowAlias ? { allowAlias: true } : undefined
571572
);
572573
};
573574

ext/descriptor/test.js

+11-4
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,22 @@ var proto = require("../../google/protobuf/descriptor.json")/*{
2727
}
2828
}*/;
2929

30-
var root = protobuf.Root.fromJSON(proto);
30+
var root = protobuf.Root.fromJSON(proto).resolveAll();
3131

32-
console.log("Original proto", JSON.stringify(root, null, 2));
32+
// console.log("Original proto", JSON.stringify(root, null, 2));
3333

3434
var msg = root.toDescriptor();
3535

36-
console.log("\nDescriptor", JSON.stringify(msg.toObject(), null, 2));
36+
// console.log("\nDescriptor", JSON.stringify(msg.toObject(), null, 2));
3737

3838
var buf = descriptor.FileDescriptorSet.encode(msg).finish();
3939
var root2 = protobuf.Root.fromDescriptor(buf, "proto2");
4040

41-
console.log("\nDecoded proto", JSON.stringify(root2, null, 2));
41+
// console.log("\nDecoded proto", JSON.stringify(root2, null, 2));
42+
43+
require("deep-diff").diff(root.toJSON(), root2.toJSON()).forEach(function(diff) {
44+
console.log(diff.kind + " @ " + diff.path.join("."));
45+
console.log("lhs:", diff.lhs);
46+
console.log("rhs:", diff.rhs);
47+
console.log();
48+
});

google/protobuf/descriptor.json

+31-74
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
"file": {
1010
"rule": "repeated",
1111
"type": "FileDescriptorProto",
12-
"id": 1,
13-
"options": {}
12+
"id": 1
1413
}
1514
}
1615
},
@@ -27,10 +26,7 @@
2726
"dependency": {
2827
"rule": "repeated",
2928
"type": "string",
30-
"id": 3,
31-
"options": {
32-
"packed": false
33-
}
29+
"id": 3
3430
},
3531
"publicDependency": {
3632
"rule": "repeated",
@@ -51,26 +47,22 @@
5147
"messageType": {
5248
"rule": "repeated",
5349
"type": "DescriptorProto",
54-
"id": 4,
55-
"options": {}
50+
"id": 4
5651
},
5752
"enumType": {
5853
"rule": "repeated",
5954
"type": "EnumDescriptorProto",
60-
"id": 5,
61-
"options": {}
55+
"id": 5
6256
},
6357
"service": {
6458
"rule": "repeated",
6559
"type": "ServiceDescriptorProto",
66-
"id": 6,
67-
"options": {}
60+
"id": 6
6861
},
6962
"extension": {
7063
"rule": "repeated",
7164
"type": "FieldDescriptorProto",
72-
"id": 7,
73-
"options": {}
65+
"id": 7
7466
},
7567
"options": {
7668
"type": "FileOptions",
@@ -95,38 +87,32 @@
9587
"field": {
9688
"rule": "repeated",
9789
"type": "FieldDescriptorProto",
98-
"id": 2,
99-
"options": {}
90+
"id": 2
10091
},
10192
"extension": {
10293
"rule": "repeated",
10394
"type": "FieldDescriptorProto",
104-
"id": 6,
105-
"options": {}
95+
"id": 6
10696
},
10797
"nestedType": {
10898
"rule": "repeated",
10999
"type": "DescriptorProto",
110-
"id": 3,
111-
"options": {}
100+
"id": 3
112101
},
113102
"enumType": {
114103
"rule": "repeated",
115104
"type": "EnumDescriptorProto",
116-
"id": 4,
117-
"options": {}
105+
"id": 4
118106
},
119107
"extensionRange": {
120108
"rule": "repeated",
121109
"type": "ExtensionRange",
122-
"id": 5,
123-
"options": {}
110+
"id": 5
124111
},
125112
"oneofDecl": {
126113
"rule": "repeated",
127114
"type": "OneofDescriptorProto",
128-
"id": 8,
129-
"options": {}
115+
"id": 8
130116
},
131117
"options": {
132118
"type": "MessageOptions",
@@ -135,16 +121,12 @@
135121
"reservedRange": {
136122
"rule": "repeated",
137123
"type": "ReservedRange",
138-
"id": 9,
139-
"options": {}
124+
"id": 9
140125
},
141126
"reservedName": {
142127
"rule": "repeated",
143128
"type": "string",
144-
"id": 10,
145-
"options": {
146-
"packed": false
147-
}
129+
"id": 10
148130
}
149131
},
150132
"nested": {
@@ -270,8 +252,7 @@
270252
"value": {
271253
"rule": "repeated",
272254
"type": "EnumValueDescriptorProto",
273-
"id": 2,
274-
"options": {}
255+
"id": 2
275256
},
276257
"options": {
277258
"type": "EnumOptions",
@@ -304,8 +285,7 @@
304285
"method": {
305286
"rule": "repeated",
306287
"type": "MethodDescriptorProto",
307-
"id": 2,
308-
"options": {}
288+
"id": 2
309289
},
310290
"options": {
311291
"type": "ServiceOptions",
@@ -408,8 +388,7 @@
408388
"uninterpretedOption": {
409389
"rule": "repeated",
410390
"type": "UninterpretedOption",
411-
"id": 999,
412-
"options": {}
391+
"id": 999
413392
}
414393
},
415394
"extensions": [
@@ -455,8 +434,7 @@
455434
"uninterpretedOption": {
456435
"rule": "repeated",
457436
"type": "UninterpretedOption",
458-
"id": 999,
459-
"options": {}
437+
"id": 999
460438
}
461439
},
462440
"extensions": [
@@ -507,8 +485,7 @@
507485
"uninterpretedOption": {
508486
"rule": "repeated",
509487
"type": "UninterpretedOption",
510-
"id": 999,
511-
"options": {}
488+
"id": 999
512489
}
513490
},
514491
"extensions": [
@@ -545,8 +522,7 @@
545522
"uninterpretedOption": {
546523
"rule": "repeated",
547524
"type": "UninterpretedOption",
548-
"id": 999,
549-
"options": {}
525+
"id": 999
550526
}
551527
},
552528
"extensions": [
@@ -569,8 +545,7 @@
569545
"uninterpretedOption": {
570546
"rule": "repeated",
571547
"type": "UninterpretedOption",
572-
"id": 999,
573-
"options": {}
548+
"id": 999
574549
}
575550
},
576551
"extensions": [
@@ -589,8 +564,7 @@
589564
"uninterpretedOption": {
590565
"rule": "repeated",
591566
"type": "UninterpretedOption",
592-
"id": 999,
593-
"options": {}
567+
"id": 999
594568
}
595569
},
596570
"extensions": [
@@ -609,8 +583,7 @@
609583
"uninterpretedOption": {
610584
"rule": "repeated",
611585
"type": "UninterpretedOption",
612-
"id": 999,
613-
"options": {}
586+
"id": 999
614587
}
615588
},
616589
"extensions": [
@@ -629,8 +602,7 @@
629602
"uninterpretedOption": {
630603
"rule": "repeated",
631604
"type": "UninterpretedOption",
632-
"id": 999,
633-
"options": {}
605+
"id": 999
634606
}
635607
},
636608
"extensions": [
@@ -645,8 +617,7 @@
645617
"name": {
646618
"rule": "repeated",
647619
"type": "NamePart",
648-
"id": 2,
649-
"options": {}
620+
"id": 2
650621
},
651622
"identifierValue": {
652623
"type": "string",
@@ -695,8 +666,7 @@
695666
"location": {
696667
"rule": "repeated",
697668
"type": "Location",
698-
"id": 1,
699-
"options": {}
669+
"id": 1
700670
}
701671
},
702672
"nested": {
@@ -705,18 +675,12 @@
705675
"path": {
706676
"rule": "repeated",
707677
"type": "int32",
708-
"id": 1,
709-
"options": {
710-
"packed": true
711-
}
678+
"id": 1
712679
},
713680
"span": {
714681
"rule": "repeated",
715682
"type": "int32",
716-
"id": 2,
717-
"options": {
718-
"packed": true
719-
}
683+
"id": 2
720684
},
721685
"leadingComments": {
722686
"type": "string",
@@ -729,10 +693,7 @@
729693
"leadingDetachedComments": {
730694
"rule": "repeated",
731695
"type": "string",
732-
"id": 6,
733-
"options": {
734-
"packed": false
735-
}
696+
"id": 6
736697
}
737698
}
738699
}
@@ -743,8 +704,7 @@
743704
"annotation": {
744705
"rule": "repeated",
745706
"type": "Annotation",
746-
"id": 1,
747-
"options": {}
707+
"id": 1
748708
}
749709
},
750710
"nested": {
@@ -753,10 +713,7 @@
753713
"path": {
754714
"rule": "repeated",
755715
"type": "int32",
756-
"id": 1,
757-
"options": {
758-
"packed": true
759-
}
716+
"id": 1
760717
},
761718
"sourceFile": {
762719
"type": "string",

src/decoder.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ function decoder(mtype) {
3131
var i = 0;
3232
for (; i < /* initializes */ mtype.fieldsArray.length; ++i) {
3333
var field = mtype._fieldsArray[i].resolve(),
34-
type = field.resolvedType instanceof Enum ? "uint32" : field.type,
34+
type = field.resolvedType instanceof Enum ? "int32" : field.type,
3535
ref = "m" + util.safeProp(field.name); gen
3636
("case %d:", field.id);
3737

src/encoder.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ function encoder(mtype) {
3939
for (var i = 0; i < fields.length; ++i) {
4040
var field = fields[i].resolve(),
4141
index = mtype._fieldsArray.indexOf(field),
42-
type = field.resolvedType instanceof Enum ? "uint32" : field.type,
42+
type = field.resolvedType instanceof Enum ? "int32" : field.type,
4343
wireType = types.basic[type];
4444
ref = "m" + util.safeProp(field.name);
4545

0 commit comments

Comments
 (0)