use of org.jnosql.artemis.reflection.ClassRepresentation in project jnosql-artemis by eclipse.
the class ConverterUtilTest method shouldNotConvert.
@Test
public void shouldNotConvert() {
ClassRepresentation representation = representations.get(Person.class);
Object value = 10_000L;
Object id = ConverterUtil.getValue(value, representation, "id", converters);
assertEquals(id, value);
}
use of org.jnosql.artemis.reflection.ClassRepresentation in project jnosql-artemis by eclipse.
the class AbstractKeyValueEntityConverter method toKeyValue.
@Override
public KeyValueEntity<?> toKeyValue(Object entityInstance) {
requireNonNull(entityInstance, "Object is required");
Class<?> clazz = entityInstance.getClass();
ClassRepresentation representation = getClassRepresentations().get(clazz);
FieldRepresentation key = getId(clazz, representation);
Object value = getReflections().getValue(entityInstance, key.getNativeField());
requireNonNull(value, String.format("The key field %s is required", key.getName()));
return KeyValueEntity.of(value, entityInstance);
}
use of org.jnosql.artemis.reflection.ClassRepresentation in project jnosql-artemis by eclipse.
the class ConverterUtilTest method shouldUseAttributeConvert.
@Test
public void shouldUseAttributeConvert() {
ClassRepresentation representation = representations.get(Worker.class);
Object value = new Money("BRL", BigDecimal.TEN);
Object converted = ConverterUtil.getValue(value, representation, "salary", converters);
assertEquals("BRL 10", converted);
}
use of org.jnosql.artemis.reflection.ClassRepresentation in project jnosql-artemis by eclipse.
the class ConverterUtilTest method shouldConvert.
@Test
public void shouldConvert() {
ClassRepresentation representation = representations.get(Person.class);
String value = "100";
Object id = ConverterUtil.getValue(value, representation, "id", converters);
assertEquals(100L, id);
}
use of org.jnosql.artemis.reflection.ClassRepresentation in project jnosql-artemis by eclipse.
the class ConverterUtil method getValue.
public static Object getValue(Object value, ClassRepresentation representation, String name, Converters converters) {
Optional<FieldRepresentation> fieldOptional = representation.getFieldRepresentation(name);
if (fieldOptional.isPresent()) {
FieldRepresentation field = fieldOptional.get();
Field nativeField = field.getNativeField();
if (!nativeField.getType().equals(value.getClass())) {
return field.getConverter().map(converters::get).map(a -> a.convertToDatabaseColumn(value)).orElseGet(() -> Value.of(value).get(nativeField.getType()));
}
return field.getConverter().map(converters::get).map(a -> a.convertToDatabaseColumn(value)).orElse(value);
}
return value;
}
Aggregations