From 17b597513cb8c7aab777cd1ea5fe7228cd15c166 Mon Sep 17 00:00:00 2001 From: Andreas Reich Date: Thu, 28 Jul 2016 00:18:59 +0200 Subject: [PATCH] Set packed=true for all repeated packable types Workaround for https://github.com/dcodeIO/protobuf.js/issues/432 --- pogo-protos.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/pogo-protos.js b/pogo-protos.js index acff4f4..8ae0e9e 100644 --- a/pogo-protos.js +++ b/pogo-protos.js @@ -8,4 +8,20 @@ fs.readdirSync(protoDir) .filter(filename => filename.match(/\.proto$/)) .forEach(filename => protobuf.loadProtoFile(protoDir + '/' + filename, builder)); +// Recursively add the packed=true to all packable repeated fields. +// Repeated fields are packed by default in proto3 but protobuf.js incorrectly does not set the option. +// See also: https://github.com/dcodeIO/protobuf.js/issues/432 +function addPackedOption(ns) { + if (ns instanceof protobuf.Reflect.Message) { + ns.getChildren(protobuf.Reflect.Field).forEach(field => { + if (field.repeated && protobuf.PACKABLE_WIRE_TYPES.indexOf(field.type.wireType) != -1) { + field.options.packed = true; + } + }); + } else if (ns instanceof protobuf.Reflect.Namespace) { + ns.children.forEach(addPackedOption); + } +} +addPackedOption(builder.lookup('POGOProtos')); + module.exports = builder.build("POGOProtos"); \ No newline at end of file