Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handling of nulls in writePackedVarint #123

Closed
rowanwins opened this issue Jun 30, 2020 · 2 comments
Closed

Handling of nulls in writePackedVarint #123

rowanwins opened this issue Jun 30, 2020 · 2 comments
Labels

Comments

@rowanwins
Copy link

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
Copy link
Member

mourner commented Jun 30, 2020

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.

@mourner mourner closed this as completed Jun 30, 2020
@rowanwins
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants