Skip to content

Commit

Permalink
fix pbf code generation for enums
Browse files Browse the repository at this point in the history
  • Loading branch information
mourner committed Jul 3, 2024
1 parent 448056c commit 2e0dfa7
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
2 changes: 1 addition & 1 deletion bench/vector_tile.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'use strict'; // code generated by pbf v2.0.1
'use strict'; // code generated by pbf v3.2.1

// Tile ========================================

Expand Down
13 changes: 11 additions & 2 deletions compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,18 @@ function writeMessage(ctx, options) {
return code;
}

function getEnumValues(ctx) {
var enums = {};
var ids = Object.keys(ctx._proto.values);
for (var i = 0; i < ids.length; i++) {
enums[ids[i]] = ctx._proto.values[ids[i]].value;
}
return enums;
}

function writeEnum(ctx, options) {
return '\n' + compileExport(ctx, options) + ' ' +
JSON.stringify(ctx._proto.values, null, 4) + ';\n';
JSON.stringify(getEnumValues(ctx), null, 4) + ';\n';
}

function compileExport(ctx, options) {
Expand Down Expand Up @@ -373,7 +382,7 @@ function setPackedOption(ctx, field, syntax) {
function setDefaultValue(ctx, field, syntax) {
var options = field.options;
var type = getType(ctx, field);
var enumValues = type && type._proto.values;
var enumValues = type && type._proto.values && getEnumValues(type);

// Proto3 does not support overriding defaults
var explicitDefault = syntax < 3 ? options.default : undefined;
Expand Down
5 changes: 1 addition & 4 deletions test/compile.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,7 @@ test('compiles defaults', function() {

assert.equal(buf.length, 0);
assert.deepEqual(data, {
type: {
options: {},
value: 1
},
type: 1,
name: 'test',
flag: true,
weight: 1.5,
Expand Down

0 comments on commit 2e0dfa7

Please sign in to comment.