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

[Java] safer method for decoding repeating groups #891

Open
spoerri opened this issue Feb 27, 2022 · 0 comments
Open

[Java] safer method for decoding repeating groups #891

spoerri opened this issue Feb 27, 2022 · 0 comments

Comments

@spoerri
Copy link
Contributor

spoerri commented Feb 27, 2022

Would you accept a PR to generate an additional method to make it safer to decode repeating groups?

By safer, I mostly mean preventing accidentally not completely decoding a group, or otherwise messing up the required order of invocation.

For CarDecoder it might be used like:

carDecoder.decodeGroups(
    (count) -> newEncoder.newMpgEncoder(count),
    (newMpgEncoder, fuelFiguresDecoder) -> newMpgEncoder.next().mpg(fuelFiguresDecoder.mpg()),
    IGNORE_A_COUNT,
    IGNORE_A_GROUP
);

And the generated code would be like:

public <R> void decodeGroups(
    IntFunction<R> fuelFiguresInit,
    BiConsumer<R, FuelFiguresDecoder> fuelFiguresConsumer,
    IntFunction<R> performanceFiguresInit,
    BiConsumer<R, PerformanceFiguresDecoder> performanceFiguresConsumer)
{
        FuelFiguresDecoder fuelFigures = fuelFigures();
        BiConsumer<R, FuelFiguresDecoder> fuelFiguresConsumer = fuelFiguresInit.apply(fuelFigures.count());
        while (fuelFigures.hasNext())
        {
            fuelFiguresConsumer.accept(consumer, fuelFigures.next());
        }

        PerformanceFiguresDecoder performanceFigures = performanceFigures();
        BiConsumer<R, PerformanceFiguresDecoder> performanceFiguresConsumer = performanceFiguresInit.apply(performanceFigures.count());
        while (performanceFigures.hasNext())
        {
            performanceFiguresConsumer.accept(performanceFiguresConsumer, performanceFigures.next());
        }
}

//of course next bit need not be generated for each decoder:
public static final IntFunction IGNORE_A_COUNT = i -> null;
public static final BiConsumer IGNORE_A_GROUP = (x,y) -> {};

I understand this would involve breaking compilation if a new group is added to the schema without an accompanying code change. If that's undesirable, we could instead use a "fluent" approach, in which each method returns something that only has the next appropriate method. The disadvantage of that approach would be needing an extra interface (or class) for each repeating group. If there's any interest, I could give more detail about how that might look...

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

No branches or pull requests

1 participant