Coder Social home page Coder Social logo

Comments (4)

anidotnet avatar anidotnet commented on July 21, 2024 1

Well, now I see what you are actually looking for. You are looking for something like TypeToken in guava or TypeReference in jackson, if I am not mistaken.

The first thing is Nitrite does not support object repository of generic type, so there is no point of having a NitriteMapper which stores generic type information.

While creating object repository, you have to use raw types only. You still can store the type of the generic type parameters in a custom field inside the document and can use it later in your application.

In latest 4.2.0-SNAPSHOT, you can define your object and it's EntityConverter in a following way.

@Data
public class GenericEntry<T> {
    private T data;
    private Long id;
    private Instant lastUpdate;

    public GenericEntry(T data, Long id, Instant lastUpdate) {
        this.data = data;
        this.id = id;
        this.lastUpdate = lastUpdate;
    }

    @SuppressWarnings("rawtypes")
    public static class GenericEntryConverter implements EntityConverter<GenericEntry> {

        @Override
        public Class<GenericEntry> getEntityType() {
            return GenericEntry.class;
        }

        @Override
        public Document toDocument(GenericEntry entity, NitriteMapper nitriteMapper) {
            return Document.createDocument("type", entity.getData().getClass().getName())
                .put("data", nitriteMapper.tryConvert(entity.data, Document.class))
                .put("id", entity.id)
                .put("lastUpdate", entity.lastUpdate);
        }

        @Override
        public GenericEntry fromDocument(Document document, NitriteMapper nitriteMapper) {
            return new GenericEntry<>(
                nitriteMapper.tryConvert(document.get("data"), Object.class),
                document.get("id", Long.class),
                document.get("lastUpdate", Instant.class));
        }
    }
}

from nitrite-java.

lamba92 avatar lamba92 commented on July 21, 2024

This is still an open issue actually. Even if you non Kotlin stuff, an object of type java.lang.Class is not enough to recover information about generics.

My specific use case if a generic container for a document such as:

data class CacheEntry(
    val data: T,
    val `_id`: Long? = null,
    val lastUpdate: Instant = Clock.System.now(),
)

or in java:

import java.time.Clock;
import java.time.Instant;

public class CacheEntry<T> {
    private T data;
    private Long _id;
    private Instant lastUpdate;
    public CacheEntry(T data, Long _id, Instant lastUpdate) {
        this.data = data;
        this._id = _id;
        this.lastUpdate = lastUpdate;
    }
    public CacheEntry(T data) {
        this(data, null, Clock.systemUTC().instant());
    }
    public T getData() {
        return data;
    }
    public void setData(T data) {
        this.data = data;
    }
    public Long get_id() {
        return _id;
    }
    public void set_id(Long _id) {
        this._id = _id;
    }
    public Instant getLastUpdate() {
        return lastUpdate;
    }
    public void setLastUpdate(Instant lastUpdate) {
        this.lastUpdate = lastUpdate;
    }
}

This class would fail to recover the type of T at runtime.

from nitrite-java.

lamba92 avatar lamba92 commented on July 21, 2024

something like TypeToken in guava or TypeReference in jackson

Exactly. Those 2 mechanisms are available since way before java.lang.Type was out there.

Nitrite does not support object repository of generic type

Wdym?

from nitrite-java.

anidotnet avatar anidotnet commented on July 21, 2024

Let's say the tryConvert method in NitriteMapper (in nitrite v4.2) would accept a type parameter instead of a class. I am curious to know what purpose does it serve for you in converting an object to document and vice versa? How would you want to create an object of parameterized type from a document using only Type?

Could you please show me your actual use case with some code explaining what are you trying to achieve? May be I'll be able to help you better.

from nitrite-java.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.