embedded field with converter

10 views
Skip to first unread message

AndyD

unread,
Jun 26, 2013, 1:29:44 PM6/26/13
to twig-p...@googlegroups.com
I'm having a little trouble getting an embedded field with a converter to be stored the way I want.

My model class looks like

class X{
    private Y mY;
}

where Y is:

class Y {
    private long mLongValue;
}

What I want is for the entity corresponding to X to have a property called mY with the value of mLongValue.  I'm trying to accomplish this by telling twig that all fields of type Y should be embedded, and by providing a pair of converters from Y to long and vice versa.  However, what I'm getting is a property called mY.mLongValue -- it seems like twig doing a bottom-up field translation and ignoring my converters.


My overridden Configuration.embed() method looks like:

    public boolean embed(final Field field) {
        if (field.getType().equals(Y.class)
            return true;

        return false;
    }

and my overridden StandardObjectDatastore.createTypeConverter() method looks like:

    protected CompositeTypeConverter createTypeConverter() {
        CompositeTypeConverter converter = super.createTypeConverter();

        // register converters that convert from Y to Long and vice versa
        converter.register(new Converter<Y , Long>() {
            @Override
            public Long convert(final Y y) {
                return y.getLongValue();
            }
        });
        converter.register(new Converter<Long, Y>() {
            @Override
            public Y convert(final Long longValue)
            {
                return new Y(longValue);
            }
        });
        return converter;
    }

Anybody see what I'm doing wrong?

Thanks.
-Andy

AndyD

unread,
Jun 26, 2013, 2:41:58 PM6/26/13
to twig-p...@googlegroups.com
OK, I solved it on my own after some more debugging.  I was missing a piece of the puzzle, namely, I needed to also override Configuration.typeOf() like so:

    public Type typeOf(final Field field) {
        if (field.getType().equals(Y.class))
            return Long.class;

        return super.typeOf(field);
    }
Reply all
Reply to author
Forward
0 new messages