Search in sources :

Example 1 with Serde

use of com.yahoo.elide.core.utils.coerce.converters.Serde in project elide by yahoo.

the class EntityDictionary method getId.

/**
 * Gets id.
 *
 * @param value the value
 * @return the id
 */
public String getId(Object value) {
    if (value == null) {
        return null;
    }
    try {
        AccessibleObject idField = null;
        Type<?> valueClass = getType(value);
        for (; idField == null && valueClass != null; valueClass = valueClass.getSuperclass()) {
            try {
                idField = getEntityBinding(valueClass).getIdField();
            } catch (NullPointerException e) {
                log.warn("Class: {} ID Field: {}", valueClass.getSimpleName(), idField);
            }
        }
        Type<?> idClass;
        Object idValue;
        if (idField instanceof Field) {
            idValue = ((Field) idField).get(value);
            idClass = ((Field) idField).getType();
        } else if (idField instanceof Method) {
            idValue = ((Method) idField).invoke(value, (Object[]) null);
            idClass = ((Method) idField).getReturnType();
        } else {
            return null;
        }
        Serde serde = serdeLookup.apply(((ClassType) idClass).getCls());
        if (serde != null) {
            return String.valueOf(serde.serialize(idValue));
        }
        return String.valueOf(idValue);
    } catch (IllegalArgumentException | IllegalAccessException | InvocationTargetException e) {
        return null;
    }
}
Also used : Serde(com.yahoo.elide.core.utils.coerce.converters.Serde) Field(com.yahoo.elide.core.type.Field) AccessibleObject(com.yahoo.elide.core.type.AccessibleObject) AccessibleObject(com.yahoo.elide.core.type.AccessibleObject) Method(com.yahoo.elide.core.type.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 2 with Serde

use of com.yahoo.elide.core.utils.coerce.converters.Serde in project elide by yahoo.

the class DaySerdeTest method testDateSerialize.

@Test
public void testDateSerialize() {
    LocalDateTime localDate = LocalDateTime.of(2020, java.time.Month.of(01), 01, 00, 00, 00);
    Day expectedDate = new Day(localDate);
    Serde serde = new Day.DaySerde();
    Object actual = serde.serialize(expectedDate);
    assertEquals("2020-01-01", actual);
}
Also used : LocalDateTime(java.time.LocalDateTime) Serde(com.yahoo.elide.core.utils.coerce.converters.Serde) Day(com.yahoo.elide.datastores.aggregation.timegrains.Day) Test(org.junit.jupiter.api.Test)

Example 3 with Serde

use of com.yahoo.elide.core.utils.coerce.converters.Serde in project elide by yahoo.

the class DaySerdeTest method testDateDeserialize.

@Test
public void testDateDeserialize() {
    LocalDateTime localDate = LocalDateTime.of(2020, java.time.Month.of(01), 01, 00, 00, 00);
    Day expectedDate = new Day(localDate);
    Serde serde = new Day.DaySerde();
    Object actualDate = serde.deserialize("2020-01-01");
    assertEquals(expectedDate, actualDate);
}
Also used : LocalDateTime(java.time.LocalDateTime) Serde(com.yahoo.elide.core.utils.coerce.converters.Serde) Day(com.yahoo.elide.datastores.aggregation.timegrains.Day) Test(org.junit.jupiter.api.Test)

Example 4 with Serde

use of com.yahoo.elide.core.utils.coerce.converters.Serde in project elide by yahoo.

the class DaySerdeTest method testDeserializeOffsetDateTime.

@Test
public void testDeserializeOffsetDateTime() {
    LocalDateTime localDate = LocalDateTime.of(2020, java.time.Month.of(01), 01, 00, 00, 00);
    Day expectedDate = new Day(localDate);
    OffsetDateTime dateTime = OffsetDateTime.of(2020, 01, 01, 00, 00, 00, 00, ZoneOffset.UTC);
    Serde serde = new Day.DaySerde();
    Object actualDate = serde.deserialize(dateTime);
    assertEquals(expectedDate, actualDate);
}
Also used : LocalDateTime(java.time.LocalDateTime) Serde(com.yahoo.elide.core.utils.coerce.converters.Serde) OffsetDateTime(java.time.OffsetDateTime) Day(com.yahoo.elide.datastores.aggregation.timegrains.Day) Test(org.junit.jupiter.api.Test)

Example 5 with Serde

use of com.yahoo.elide.core.utils.coerce.converters.Serde in project elide by yahoo.

the class HourSerdeTest method testDeserializeTimestamp.

@Test
public void testDeserializeTimestamp() {
    String dateInString = "2020-01-01T01";
    Hour expectedDate = new Hour(LocalDateTime.from(formatter.parse(dateInString)));
    Timestamp timestamp = new Timestamp(expectedDate.getTime());
    Serde serde = new Hour.HourSerde();
    Object actualDate = serde.deserialize(timestamp);
    assertEquals(expectedDate, actualDate);
}
Also used : Serde(com.yahoo.elide.core.utils.coerce.converters.Serde) Hour(com.yahoo.elide.datastores.aggregation.timegrains.Hour) Timestamp(java.sql.Timestamp) Test(org.junit.jupiter.api.Test)

Aggregations

Serde (com.yahoo.elide.core.utils.coerce.converters.Serde)62 Test (org.junit.jupiter.api.Test)59 LocalDateTime (java.time.LocalDateTime)34 OffsetDateTime (java.time.OffsetDateTime)12 Timestamp (java.sql.Timestamp)10 Time (com.yahoo.elide.datastores.aggregation.timegrains.Time)6 ISOWeek (com.yahoo.elide.datastores.aggregation.timegrains.ISOWeek)5 Quarter (com.yahoo.elide.datastores.aggregation.timegrains.Quarter)5 Week (com.yahoo.elide.datastores.aggregation.timegrains.Week)5 Day (com.yahoo.elide.datastores.aggregation.timegrains.Day)4 Hour (com.yahoo.elide.datastores.aggregation.timegrains.Hour)4 Minute (com.yahoo.elide.datastores.aggregation.timegrains.Minute)4 Month (com.yahoo.elide.datastores.aggregation.timegrains.Month)4 Second (com.yahoo.elide.datastores.aggregation.timegrains.Second)4 Year (com.yahoo.elide.datastores.aggregation.timegrains.Year)4 EntityDictionary (com.yahoo.elide.core.dictionary.EntityDictionary)1 Injector (com.yahoo.elide.core.dictionary.Injector)1 AccessibleObject (com.yahoo.elide.core.type.AccessibleObject)1 Field (com.yahoo.elide.core.type.Field)1 Method (com.yahoo.elide.core.type.Method)1