Skip to content

Commit

Permalink
Throw if a non-Instant field is annotated with @TimeColumn (#974)
Browse files Browse the repository at this point in the history
  • Loading branch information
eranl authored Dec 4, 2023
1 parent 5f066d1 commit d697143
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 14 deletions.
34 changes: 20 additions & 14 deletions src/main/java/org/influxdb/dto/Point.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.influxdb.dto;

import org.influxdb.BuilderException;
import org.influxdb.InfluxDBMapperException;
import org.influxdb.annotation.Column;
import org.influxdb.annotation.Exclude;
import org.influxdb.annotation.Measurement;
Expand Down Expand Up @@ -322,20 +323,25 @@ private void addFieldByAttribute(final Object pojo, final Field field, final boo
Object fieldValue = field.get(pojo);

TimeColumn tc = field.getAnnotation(TimeColumn.class);
if (tc != null && Instant.class.isAssignableFrom(field.getType())) {
Optional.ofNullable((Instant) fieldValue).ifPresent(instant -> {
TimeUnit timeUnit = tc.timeUnit();
if (timeUnit == TimeUnit.NANOSECONDS || timeUnit == TimeUnit.MICROSECONDS) {
this.time = BigInteger.valueOf(instant.getEpochSecond())
.multiply(NANOSECONDS_PER_SECOND)
.add(BigInteger.valueOf(instant.getNano()))
.divide(BigInteger.valueOf(TimeUnit.NANOSECONDS.convert(1, timeUnit)));
} else {
this.time = timeUnit.convert(instant.toEpochMilli(), TimeUnit.MILLISECONDS);
}
this.precision = timeUnit;
});
return;
if (tc != null) {
if (Instant.class.isAssignableFrom(field.getType())) {
Optional.ofNullable((Instant) fieldValue).ifPresent(instant -> {
TimeUnit timeUnit = tc.timeUnit();
if (timeUnit == TimeUnit.NANOSECONDS || timeUnit == TimeUnit.MICROSECONDS) {
this.time = BigInteger.valueOf(instant.getEpochSecond())
.multiply(NANOSECONDS_PER_SECOND)
.add(BigInteger.valueOf(instant.getNano()))
.divide(BigInteger.valueOf(TimeUnit.NANOSECONDS.convert(1, timeUnit)));
} else {
this.time = timeUnit.convert(instant.toEpochMilli(), TimeUnit.MILLISECONDS);
}
this.precision = timeUnit;
});
return;
}

throw new InfluxDBMapperException(
"Unsupported type " + field.getType() + " for time: should be of Instant type");
}

if (tag) {
Expand Down
16 changes: 16 additions & 0 deletions src/test/java/org/influxdb/dto/PointTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.influxdb.BuilderException;
import org.influxdb.InfluxDB;
import org.influxdb.InfluxDBMapperException;
import org.influxdb.annotation.Column;
import org.influxdb.annotation.Measurement;
import org.influxdb.annotation.TimeColumn;
Expand Down Expand Up @@ -712,6 +713,13 @@ public void testAddFieldsFromPOJOWithTimeColumnSeconds() throws NoSuchFieldExcep
Assertions.assertEquals(pojo.time.getEpochSecond(), timeField.get(p));
}

@Test
public void testAddFieldsFromPOJOWithBadTimeColumn() {
BadTimeColumnPojo pojo = new BadTimeColumnPojo();
Assertions.assertThrows(InfluxDBMapperException.class,
() -> Point.measurementByPOJO(pojo.getClass()).addFieldsFromPOJO(pojo).build());
}

@Test
public void testAddFieldsFromPOJOWithTimeColumnNull() throws NoSuchFieldException, IllegalAccessException {
TimeColumnPojo pojo = new TimeColumnPojo();
Expand Down Expand Up @@ -939,6 +947,14 @@ static class TimeColumnPojoSec {
Instant time;
}

@Measurement(name = "tcmeasurement", allFields = true)
static class BadTimeColumnPojo {
boolean booleanPrimitive;

@TimeColumn
String time;
}

@Measurement(name = "mymeasurement")
static class Pojo {

Expand Down

0 comments on commit d697143

Please sign in to comment.