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 instant type format #33

Closed
agongi opened this issue Jul 16, 2021 · 1 comment
Closed

support instant type format #33

agongi opened this issue Jul 16, 2021 · 1 comment

Comments

@agongi
Copy link

agongi commented Jul 16, 2021

as reference mentioned, java instant type can be formatted as following:

https://github.com/lazee/freemarker-java-8#ballot_box_with_check-javatimeinstant

${myinstant.format()}

but It doesnt support format as coded like this:

    @Override
    public TemplateModel getForType(String s) throws TemplateModelException {
        if (METHOD_FORMAT.equals(s)) {
            return new InstantFormatter(getObject());
        }
        throw new TemplateModelException(METHOD_UNKNOWN_MSG + s);
    }


    public class InstantFormatter extends AbstractFormatter<Instant> implements TemplateMethodModelEx {

        public InstantFormatter(Instant obj) {
            super(obj);
        }


        @Override
        public Object exec(List list) throws TemplateModelException {
            return getObject().toString();  // format ignored
        }
    }

generated value is always getObject().toString() so formatting wont work

I've searched other types of type, and find out this:

    public class LocalDateFormatter extends AbstractFormatter<LocalDate> implements TemplateMethodModelEx {

        public LocalDateFormatter(LocalDate obj) {
            super(obj);
        }


        @Override
        public Object exec(List list) throws TemplateModelException {
            return getObject().format(createDateTimeFormatter(list, 0, DateTimeFormatter.ISO_LOCAL_DATE));
        }
    }

in your code-context, instant type also can be formatted.

public class FormattableInstantAdapter extends AbstractAdapter<Instant> implements AdapterTemplateModel,
    TemplateScalarModel, TemplateHashModel {

    public FormattableInstantAdapter(Instant obj, BeansWrapper wrapper) {
        super(obj, wrapper);
    }

    @Override
    public TemplateModel getForType(String s) throws TemplateModelException {
        if (METHOD_FORMAT.equals(s)) {
            return new InstantFormatter(getObject());
        }

        throw new TemplateModelException(METHOD_UNKNOWN_MSG + s);
    }

    public class InstantFormatter extends AbstractFormatter<Instant> implements TemplateMethodModelEx {

        public InstantFormatter(Instant obj) {
            super(obj);
        }

        @Override
        public Object exec(List list) throws TemplateModelException {
            return createDateTimeFormatter(list, 0, DateTimeFormatter.ISO_LOCAL_DATE_TIME)
                .withZone(ZoneId.systemDefault())  // instant use UTC as default
                .format(getObject());
        }
    }
}
@lazee
Copy link
Owner

lazee commented Jun 6, 2022

Fixed in version 2.1.0 that will be released soon

@lazee lazee closed this as completed Jun 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants