capnproto-java icon indicating copy to clipboard operation
capnproto-java copied to clipboard

Add helpers for serializing collections

Open tmr232 opened this issue 4 years ago • 3 comments

This is a class of helper functions we use when serializing collections. It takes care of the index manipulation and assures the user that this is only serialization - not a generic loop.

Without the helpers:

var thingsBuilder = builder.initThings(items.size());
for (int i = 0; i < items.size(); ++i) {
    var thingBuilder = thingsBuilder.get(i);
    var item = items.get(i);
    thingBuilder.setAttr(item.getAttr());
    thingBuilder.setOther(item.getOther());
}

With the helpers:

serializeCollection(
    items, builder::initThings,
    (item, thingBuilder) -> {
        thingBuilder.setAttr(item.getAttr());
        thingBuilder.setOther(item.getOther());
    }
);

tmr232 avatar Jan 27 '21 18:01 tmr232

Thanks!

I think using the word serialize in the names of these functions clashes somewhat with the rest of capnproto-java, where "serialize" means to go from a MessageBuilder to bytes.

I don't have a good sense for how generally useful these new functions are. It would help if they had some documentation comments and possibly some tests.

dwrensha avatar Feb 01 '21 00:02 dwrensha

What do you think would be a good name, then?

And anything specific you'd like to see in the test, or just a comparison against the regular-loop version to see that it is equivalent?

tmr232 avatar Feb 01 '21 12:02 tmr232

What do you think would be a good name, then?

I don't have any suggestions, because I don't have a good sense for the situations in which these functions are intended to be used.

And anything specific you'd like to see in the test, or just a comparison against the regular-loop version to see that it is equivalent?

Mainly I'm interested in seeing explanations and examples of how these functions are meant to be used.

I'm worried that maybe these functions are too specialized to merit a place in the base library. I was hoping that if you provided more context then I would be able to make a better judgment about that.

dwrensha avatar Feb 09 '21 00:02 dwrensha