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

Suggestion: allow accessing parameter names #122

Closed
pmlopes opened this issue Dec 19, 2019 · 7 comments
Closed

Suggestion: allow accessing parameter names #122

pmlopes opened this issue Dec 19, 2019 · 7 comments

Comments

@pmlopes
Copy link
Contributor

pmlopes commented Dec 19, 2019

It would be nice to have this API made public:

https://github.com/fizzed/rocker/blob/master/rocker-runtime/src/main/java/com/fizzed/rocker/Rocker.java#L72

The use case:

You have a generic template and allow users to pass you a generic map with all possible variables.

If the variables do not match the template arguments you'll get an error. Sometimes variables do not match because the user passed some extra values, imagine a map that gets enriched as it is processed by the user API.

Having a way to know the expected arguments, we could bind the required arguments only ignoring the extras and making the template always to succeed.

@pmlopes
Copy link
Contributor Author

pmlopes commented Dec 19, 2019

Also note that there's a small bug on that method, the logged message refers to a different method name that the one really accessed by reflection.

@jjlauer
Copy link
Member

jjlauer commented Dec 19, 2019

The concept you're referring to already exists. That class you referenced returns a BindableRockerModel which has a bind() method that accepts a map. It compares what you provided against the arguments defined.

String rendered = Rocker.template("views/index.rocker.html")
    .bind(map_is_accepted_here)
    .render()
    .toString();

Finally, the reflection you are talking about is not a bug. We're accessing a method in the Model class, not the Rocker class. If what you mentioned was a bug, there is extensive test coverage of Rocker and BindableRockerModel would catch it. I just think you're a little confused what Rocker is doing vs. BindableRockerModel.

@jjlauer jjlauer closed this as completed Dec 19, 2019
@pmlopes
Copy link
Contributor Author

pmlopes commented Dec 19, 2019

I don't think I explained the problem well enough, my case was, Imagine that in a application lifecycle a Map (session data) is populated with some state and the end of the processing we want to render a template. This state will contain more information that what is needed by the template. Imagine:

This would be the map:

{
  "id": "session-id",
  "name": "Paulo"
}

If my template is:

@args (String name)
Hello @name

Then the render will throw because the number of arguments don't match with a message property id not found.

I'd like either to relax this rule or know in advance what arguments are accepted so I can bind only the correct ones.

The logic is already present the generated code contains:

...
public static String[] getArgumentNames() {
        return new String[]{"name"};
    }
...

It's just a matter of making this accessible.

@pmlopes
Copy link
Contributor Author

pmlopes commented Dec 19, 2019

Regarding the "bug" it was on the log message:

https://github.com/fizzed/rocker/blob/master/rocker-runtime/src/main/java/com/fizzed/rocker/Rocker.java#L72-L78

The method name is getArgumentNames but the log message says Unable to read getModifiedAt which is some other method.

@jjlauer
Copy link
Member

jjlauer commented Dec 20, 2019

@pmlopes Thank you for your new explanation -- I think I get your use case better now. You basically want a relaxed version of setting arguments by supplying a map of values.

I'd be open to a PR that added another method to Rocker.java that builds a model from a map of values. Since Rocker is usually strict with enforcing variable names match templates, I'd suggest the Map of values by default will be strict and check they match what the template accepts. However, let the caller also turn off that functionality, so it just sets what it can.

@jjlauer jjlauer reopened this Dec 20, 2019
@pmlopes
Copy link
Contributor Author

pmlopes commented Dec 20, 2019

I'll make the PR after the Christmas break! Thanks!

jjlauer added a commit that referenced this issue Jan 7, 2020
@jjlauer
Copy link
Member

jjlauer commented Jan 7, 2020

Fixed in latest release.

@jjlauer jjlauer closed this as completed Jan 7, 2020
huw0 added a commit to huw0/rocker that referenced this issue Feb 23, 2020
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