Comments (4)
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.
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.
something like
TypeToken
in guava orTypeReference
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.
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)
- NitriteCollection.update slow when unrelated properties in document are indexed HOT 6
- EntityConverter toDocument HOT 3
- KotlinXSerializationMapper decodes lists to be size 1 always HOT 1
- Invalid repository type for entity where all fields are nullable (with JacksonMapperModule) HOT 3
- Possibiltity to index a field with both text and non-unique/unique filter HOT 3
- Incorrect result when searching indexed column HOT 1
- Support JPMS HOT 6
- Feature request: method for retrieving all document ids in a collection faster HOT 1
- Search for _id always returns an iterator with an element HOT 2
- org.dizitart.no2.common.tuples.Pair cannot be cast to class java.lang.Comparable HOT 4
- @Indices is not neccessary to use, multiple @Index annotations on class are allowed HOT 1
- Repositories initialization logic not compatible with Kotlinx.serialization HOT 8
- bootstrap-3.3.7.min.js: 6 vulnerabilities (highest severity is: 6.1) HOT 1
- jquery.dataTables-1.10.12.min.js: 1 vulnerabilities (highest severity is: 6.1) HOT 1
- Chart-2.1.4.min.js: 1 vulnerabilities (highest severity is: 9.8) HOT 1
- bootstrap-3.3.6.min.js: 6 vulnerabilities (highest severity is: 6.1) HOT 1
- `Chunk not found` exception when trying to drop index HOT 5
- bug: Nitrite#getCollection sometimes fails HOT 3
- bug: migration from 3.x to 4.x with MVStore module fails HOT 2
- Tests fail on systems with non ENGLISH locale
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nitrite-java.