From 0fcaf94045a9a798eccfc322247e4b0a779d2a98 Mon Sep 17 00:00:00 2001 From: Sergii Shutovskyi Date: Fri, 24 Jun 2016 11:13:05 +0200 Subject: [PATCH 1/2] add support for embedded type references --- bench/vector_tile.proto | 11 +++++++++++ compile.js | 6 ++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/bench/vector_tile.proto b/bench/vector_tile.proto index 13b9b80..74586a5 100644 --- a/bench/vector_tile.proto +++ b/bench/vector_tile.proto @@ -94,7 +94,18 @@ message Tile { extensions 16 to max; } + message Container { + message Inner { + optional string value = 1; + } + + repeated Inner values = 1; + } + repeated Layer layers = 3; extensions 16 to 8191; + + optional Container sub_field = 4; + optional Container.Inner sub_sub_field = 5; } diff --git a/compile.js b/compile.js index 2de8658..ec81ffa 100644 --- a/compile.js +++ b/compile.js @@ -94,7 +94,8 @@ function compileDest(ctx) { } function compileFieldRead(ctx, field) { - var type = ctx[field.type]; + var path = field.type.split('.'); + var type = path.reduce(function(ctx, name) { return ctx && ctx[name]; }, ctx); if (type) { if (type._proto.fields) return type._name + '.read(pbf, pbf.readVarint() + pbf.pos)'; if (type._proto.values) return 'pbf.readVarint()'; @@ -131,7 +132,8 @@ function compileFieldWrite(ctx, field, name) { var postfix = (field.options.packed ? '' : 'Field') + '(' + field.tag + ', obj.' + name + ')'; - var type = ctx[field.type]; + var path = field.type.split('.'); + var type = path.reduce(function(ctx, name) { return ctx && ctx[name]; }, ctx); if (type) { if (type._proto.fields) return prefix + 'Message(' + field.tag + ', ' + type._name + '.write, obj.' + name + ')'; if (type._proto.values) return prefix + 'Varint' + postfix; From d72282e1a01b16611dbe9dff4ff788e220687b3a Mon Sep 17 00:00:00 2001 From: Sergii Shutovskyi Date: Tue, 28 Jun 2016 10:18:53 +0200 Subject: [PATCH 2/2] add separate proto file for embedded type testing, dry compile.js --- bench/vector_tile.proto | 11 ----------- compile.js | 11 +++++++---- test/compile.test.js | 7 +++++++ test/fixtures/embedded_type.proto | 18 ++++++++++++++++++ 4 files changed, 32 insertions(+), 15 deletions(-) create mode 100644 test/fixtures/embedded_type.proto diff --git a/bench/vector_tile.proto b/bench/vector_tile.proto index 74586a5..13b9b80 100644 --- a/bench/vector_tile.proto +++ b/bench/vector_tile.proto @@ -94,18 +94,7 @@ message Tile { extensions 16 to max; } - message Container { - message Inner { - optional string value = 1; - } - - repeated Inner values = 1; - } - repeated Layer layers = 3; extensions 16 to 8191; - - optional Container sub_field = 4; - optional Container.Inner sub_sub_field = 5; } diff --git a/compile.js b/compile.js index ec81ffa..3d20257 100644 --- a/compile.js +++ b/compile.js @@ -93,9 +93,13 @@ function compileDest(ctx) { return '{' + props.join(', ') + '}'; } -function compileFieldRead(ctx, field) { +function getType(ctx, field) { var path = field.type.split('.'); - var type = path.reduce(function(ctx, name) { return ctx && ctx[name]; }, ctx); + return path.reduce(function(ctx, name) { return ctx && ctx[name]; }, ctx); +} + +function compileFieldRead(ctx, field) { + var type = getType(ctx, field); if (type) { if (type._proto.fields) return type._name + '.read(pbf, pbf.readVarint() + pbf.pos)'; if (type._proto.values) return 'pbf.readVarint()'; @@ -132,8 +136,7 @@ function compileFieldWrite(ctx, field, name) { var postfix = (field.options.packed ? '' : 'Field') + '(' + field.tag + ', obj.' + name + ')'; - var path = field.type.split('.'); - var type = path.reduce(function(ctx, name) { return ctx && ctx[name]; }, ctx); + var type = getType(ctx, field); if (type) { if (type._proto.fields) return prefix + 'Message(' + field.tag + ', ' + type._name + '.write, obj.' + name + ')'; if (type._proto.values) return prefix + 'Varint' + postfix; diff --git a/test/compile.test.js b/test/compile.test.js index eb6ea4e..e30d2c4 100644 --- a/test/compile.test.js +++ b/test/compile.test.js @@ -23,3 +23,10 @@ test('compiles vector tile proto', function(t) { t.end(); }); + +test('compiles proto with embedded type reference', function(t) { + var proto = resolve(path.join(__dirname, './fixtures/embedded_type.proto')); + compile(proto); + + t.end(); +}); diff --git a/test/fixtures/embedded_type.proto b/test/fixtures/embedded_type.proto new file mode 100644 index 0000000..6c1e4ec --- /dev/null +++ b/test/fixtures/embedded_type.proto @@ -0,0 +1,18 @@ +// Protocol Version 1 + +package embedded_type; + +option optimize_for = LITE_RUNTIME; + +message EmbeddedType { + message Container { + message Inner { + optional string value = 1; + } + + repeated Inner values = 1; + } + + optional Container sub_field = 4; + optional Container.Inner sub_sub_field = 5; +}