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

ClassCastException if Context isn't a ContextImpl #18

Closed
bckfnn opened this issue Jun 27, 2012 · 5 comments
Closed

ClassCastException if Context isn't a ContextImpl #18

bckfnn opened this issue Jun 27, 2012 · 5 comments

Comments

@bckfnn
Copy link

bckfnn commented Jun 27, 2012

Congrats on the 0.1.0 release!

I wanted to try the new Context interface for an "advanced" use of adding support for public fields and for public static fields.

When one of my helpers want to render some other template I get an exception

    java.lang.ClassCastException: handlebars.Templating$9 cannot be cast to com.github.edgarespina.handlebars.ContextFactory$ContextImpl
    at com.github.edgarespina.handlebars.ContextFactory$ContextImpl.<init>(ContextFactory.java:92)
    at com.github.edgarespina.handlebars.ContextFactory$ContextImpl.<init>(ContextFactory.java:25)
    at com.github.edgarespina.handlebars.ContextFactory.wrapInternal(ContextFactory.java:321)
    at com.github.edgarespina.handlebars.ContextFactory.wrap(ContextFactory.java:306)
    at com.github.edgarespina.handlebars.internal.DefaultOptions.apply(DefaultOptions.java:136)

because my implementation of Context can not be cast to a ContextImpl. Replacing the cast with

    this.storage = parent.storage();

works for me.

@jknack
Copy link
Owner

jknack commented Jun 27, 2012

I see it.

Issues #17 solve one part of the problem, but not what you're trying to do. A have one question for you:

  • In your custom Context, are you dealing with the context stack? what about the rules behind the context stack? or are you just resolving the value from a field?

From my perspective, a user should never never need to create a custom Context (that is why #17 was created).

A different/safe approach will be the extend the context API with the ability to let client to resolve value and reuse all the context stack rules. This also means:

  • Context will be a final class
  • You can provide custom "lookup" or value resolver (method, fields, map, etc.)

What do you think?

Thanks

@bckfnn
Copy link
Author

bckfnn commented Jun 28, 2012

It sounds good. My context implementation had way too much code copied from your ContextImpl anyway.

@jknack
Copy link
Owner

jknack commented Jun 29, 2012

fixed in 0.2.0. The jar is in maven central. I'll write the release notes later today

Oh, have a look to the README, specially the Advanced usage section

@jknack jknack closed this as completed Jun 29, 2012
@bckfnn
Copy link
Author

bckfnn commented Jun 29, 2012

It's perfect...

except :-) for the Context.newContext() call in DefaultOptions.apply() where I can't find a way to add the FieldValueResolver that I want. So

   {{#each users}} {{.name}} {{/each}}

works only if the User class have a getName(), not if 'name' is a field.

@jknack
Copy link
Owner

jknack commented Jun 29, 2012

good catch! All the issues are more or less related to making public "Context", I have to review Options#apply and Template#apply.

wait for the next release, everything will be working as expected!

jknack added a commit that referenced this issue Jun 30, 2012
ClassCastException if Context isn't a ContextImpl #18
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