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

Does dotted attribute notation work when using actual objects instead of JSON? #66

Closed
efenderbosch opened this issue Oct 27, 2017 · 9 comments

Comments

@efenderbosch
Copy link

We're trying to directly put an object into the render context like this:

template.render("some_top_level_name", someComplexObject);

And in the actual template, we can access the root object via {{ some_top_level_name }} but not any of its attributes, like {{ some_top_level_name.snake_case_attribute }} or {{ some_top_level_name.camelCaseAttribute }}

Does this work? Are we doing it wrong?

@bkiers
Copy link
Owner

bkiers commented Oct 27, 2017

Yes, that would be good to have. Check the PR and it's unit tests.

@efenderbosch
Copy link
Author

Maybe allow the ObjectMapper to be passed in or customized? For example, our looks like this:

new ObjectMapper().
  registerModule(new Jdk8Module()).
  registerModule(new JavaTimeModule()).
  disable(FAIL_ON_UNKNOWN_PROPERTIES).
  setSerializationInclusion(NON_ABSENT).
  disable(WRITE_DATES_AS_TIMESTAMPS).
  enable(ALLOW_COMMENTS).
  setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE).
  setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss-Z"));

Not all of that is relevant, but the Jdk8 and JavaTime modules probably are.

@efenderbosch
Copy link
Author

efenderbosch commented Oct 27, 2017

Another question. Will the value present in TemplateContext (like in a custom Tag) be the original object, or a Map? If it is a map, then we'll just be converting the map back to an object instead of JSON back to an object in our custom tags.

@bkiers
Copy link
Owner

bkiers commented Oct 27, 2017

Maybe allow the ObjectMapper to be passed in or customized?

Ah yes, of course. Moved it to ParseSettings so that you can set your own.

Will the value present in TemplateContext

I've not looked into this lib for a while, but I think it is. Give it a try.

@bkiers
Copy link
Owner

bkiers commented Oct 27, 2017

Whoops, I accidentally merged it (and automatically closed this issue). Feel free to re-open if needed.

@efenderbosch
Copy link
Author

I'm getting a LinkedHashMap<String, Object> out of the TemplateContext instead of the instance of someComplexObject. That's not ideal, but not horrible.

@efenderbosch
Copy link
Author

Maybe just add public T get(String key, Class<T> clazz) to TemplateContext?

But then TemplateContext would also need ParseSettings to access the ObjectMapper

@bkiers
Copy link
Owner

bkiers commented Oct 30, 2017

I'm getting a LinkedHashMap<String, Object> out of the TemplateContext instead of the instance of someComplexObject. That's not ideal, but not horrible.

Ah, ok, I misunderstood. Yes, the instance is getting converted into a LinkedHashMap<String, Object>.

Perhaps there is, but I can't imagine a use-case where this would be useful (keeping the instance instead of a LinkedHashMap<String, Object>). Feel free to create a new issue for this, but it wouldn't be high on my list to implement (unless there is a good use-case for it).

@efenderbosch
Copy link
Author

We are already using it in this way in our custom tags. I did a quick and dirty implementation like this:

// horrible name...
abstract class BeanTag extends Tag {

    protected BeanTag(String name) {
        super(name);
    }

    protected <T> T getFromContext(String key, TemplateContext context, Class<T> clazz) {
        Map<String, Object> map = (Map<String, Object>) context.get(key);
        if (map == null || map.isEmpty()) return null;
        return MAPPER.convertValue(map, clazz);
    }
}

And our tags just extend BeanTag instead of Tag

Adding it to TemplateContext would be just a little extra syntactic sugar, but not necessary.

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