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

Type-Safe Placeholders/Templating with ValueSerialiser integration #27

Open
gepron1x opened this issue Jul 30, 2021 · 4 comments
Open
Labels
enhancement New feature or request feedback wanted Feedback especially wanted on this issue
Milestone

Comments

@gepron1x
Copy link

My idea is about utilizing method arguments as ValueSerializer parameters. With them, we can pass some context to the serializer when we are getting the value. Ill give an example:
Lets assume that we have a config with Component message that has sender placeholder:
public interface TestConfig { Component message(@Argument("sender") Component sender); }
And in value serializer, we pass arguments in the map where key is arg name/string defined in @argument, and Value is argument value.
public Component deserialise(FlexibleType value, Map<String, Object> args) throws BadValueException { List<Template> templates = args.entrySet().stream() .map(entry -> Template.of(entry.getKey(), (Component) entry.getValue()) ).toList(); return miniMessage.parse(value.getString(), templates); }
I think this addition would make code much cleaner in some cases, especially if you have placeholders in your message.
But, to be honest, im not sure if its the best solution.

@A248
Copy link
Owner

A248 commented Jul 30, 2021

This feature needs careful consideration. It would very much improve consumer code; in fact, remembering to replace placeholders is now the most error-prone part of my own code. One of this library's most important original goals was to reduce bugs arising from mis-typed configuration keys such as config.get("my-config-option-with-a-typo"). Perhaps the next step in its evolution is to tackle placeholders, which are similarly frequently-used yet liable to mistake.

Nevertheless, a feature like this would be a significant step in this library's purpose. It would transform DazzleConf into more than a type-safe boilerplate-minimal configuration library. This carries associated costs. It introduces additional complexity into a small and easily-maintained library.

For the purpose of discussion, I'll tentatively tack this on to the 2.0.0 milestone. I'd very much like to hear what other users have to say and what extra solutions they employ, if any, with respect to templating.

@A248 A248 added this to the 2.0.0 milestone Jul 30, 2021
@A248 A248 added enhancement New feature or request feedback wanted Feedback especially wanted on this issue labels Jul 30, 2021
@A248 A248 changed the title Suggestion: Placeholders and ValueSerializer parameters. Type-Safe Placeholders/Templating with ValueSerialiser integration Jul 30, 2021
@ghost
Copy link

ghost commented Jul 30, 2021

i like the idea, seems to be suuuper useful! type-safe placeholders is what i need.

@A248
Copy link
Owner

A248 commented Aug 29, 2023

If we implement this feature, I am thinking it will follow this two-step process:

  1. The configuration value is loaded with DazzleConf into its required type. This is what happens normally, without placeholders. It is DazzleConf's main purpose to perform this upfront, fail-fast configuration loading.
  2. Later, when the configuration method is invoked, placeholders are replaced.

This process has implications for the example you posted, @gepron1x . It means that MiniMessage must have a good way to replace templates in an existing Component. The reason is that DazzleConf first needs to load the Component using the standard mechanism in order to provide fail-fast behavior. Later, when the method is called, placeholders are replaced. Such operation is also more efficient than parsing the entire MiniMessage string every time the method is called.

Note that this idea can exist in tandem with #20 , if both are implemented, by using method overloads.

@gepron1x
Copy link
Author

Well, adventure developers are not really a fan of replacing in existing Components. Component is treated as a "compiled" piece of text which ideally shouldnt be changed internally.

In adventure's case i just made an utility class Message(String value, MiniMessage miniMessage) which implements ComponentLike where you can set placeholder first and then construct a component to use.
I do not see any problems

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request feedback wanted Feedback especially wanted on this issue
Projects
None yet
Development

No branches or pull requests

2 participants