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

Support for builder pattern to construct objects of custom classes #57

Open
kravemir opened this issue Oct 4, 2018 · 3 comments
Open

Comments

@kravemir
Copy link

kravemir commented Oct 4, 2018

I would like to use TOML (toml4j) for (de)serialization of model classes for my project. I strongly prefer value based approach to model classes, and use immutable classes with builder pattern for construction.

I found support for custom classes in README.md, but there's no mention of builder pattern support, therefore I assume it's not implemented. However, I would be very glad to have such feature.

Also, as my project uses models defined in another project. I would like to use builder based construction without need of annotations (ie. annotation based configuration is optional).

For example:

TomlTypeRegistry registry = new TomlTypeRegistry();
registry.add(Address.class, "builder", "build");
registry.add(User.class, "newBuilder", "create");

User user = new Toml().read(tomlFile).to(User.class);

Assume for example:

class Address {
    // ....

    static class Builder {
        // ... setProperty(Type value) methods ...

        Address build();
    }

    static Builder builder();
}

class User {
    // ....

    static class Builder {
        // ... setProperty(Type value) methods ...

        User create();
    }

    static Builder newBuilder();
}
@mwanji
Copy link
Owner

mwanji commented Oct 6, 2018

Thanks for the interest! I'm not sure I understand what toml4j would pass to the builder, or why built-in support is needed. Is this builder auto-generated or hand-written?

@kravemir
Copy link
Author

kravemir commented Oct 6, 2018

I'm not sure I understand what toml4j would pass to the builder

The same thing as to custom classes. When populating custom classes, (I think) the process is following:

  • create new instance custom class (new CustomClass()),
  • deserialize properties (recusive process, if property is also custom class),
  • set properties using setter methods (instance.setName(value), instance.setOther(value), ... ),
  • return populated instance of custom class.

When deserializing to custom classes instantiated via builder, process is following:

  • create new instance of builder (CustomClass.newBuilder()),
  • deserialize properties (recusive process, if property is also custom class),
  • set properties using setter methods (builder.setName(value), builder.setOther(value), ... ),
  • build instance using builder ( instance = builder.build() ),
  • return built instance.

or why built-in support is needed

Not needed, and could be created on top of toml4j. But, it would be useful for projects using value-based approach to model classes.

Is this builder auto-generated or hand-written?

Usually auto-generated. Either by IDE's extensions, or via annotation processing (lombok, immutables, google-auto,...), or from contract definition (Google Protocol Buffers, ...).

@kravemir
Copy link
Author

kravemir commented Oct 6, 2018

@mwanji actually, I'm much more concerned about legal stuff mentioned in #56, that this feature. Legal stuff is blocker,... Feature I can write on my own, and also contribute as PR to this project (if you would welcome it),... But, legal stuff has to be solved by library's author.

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

2 participants