mapbox / pbf

A low-level, lightweight protocol buffers implementation in JavaScript.
BSD 3-Clause "New" or "Revised" License
801 stars 107 forks source link

Handling of nulls in writePackedVarint #123

Closed rowanwins closed 4 years ago

rowanwins commented 4 years ago

I have an array of integers, which also happens to contain some null values

I'm trying to use a writePackedVarint however it's converting my nulls into 0's

const Pbf = require('pbf')

const array = [0, null, 1, null, 2]

// Write the pbf
var pbf = new Pbf();
function writeData(data, pbf) {
    pbf.writePackedVarint(3, data);
}
writeData(array, pbf)
var buffer = pbf.finish();

// Decode the pbf
var data = new Pbf(buffer).readFields(readData, {values: []});
function readData(tag, data, pbf) {
    if (tag === 3) pbf.readPackedVarint(data.values);
}

// What my output looks like
console.log(data.values)
=> [0, 0, 1, 0, 2]

I'm not a protocol buffer guru by any stretch but is this expected behaviour? Is there an alternate way to handle null values?

mourner commented 4 years ago

Hi Rowan, as far as I understand, there's no way to handle that with "packed" fields because they assume a batch of items of the same type (in this case varint). You'd have to either change the proto schema somehow to acommodate arrays of messages that contain different fields, or designate some special integer value (e.g. -1 or Number.MAX_SAFE_INTEGER) to treat as null on the app side.

rowanwins commented 4 years ago

No worries, thanks @mourner - for the time being I'm using the Number.MAX_SAFE_INTEGER approach.