capnproto / capnproto-java

Cap'n Proto in pure Java
Other
391 stars 86 forks source link

Add helpers for serializing collections #95

Open tmr232 opened 3 years ago

tmr232 commented 3 years ago

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());
    }
);
dwrensha commented 3 years ago

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.

tmr232 commented 3 years ago

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?

dwrensha commented 3 years ago

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.