Skip to content

Commit

Permalink
fix: @column supports super class inheritance for write measurements (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
bednar authored Apr 2, 2020
1 parent a03cd8d commit 826bb6c
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 7 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
1. [#93](https://github.com/influxdata/influxdb-client-java/issues/93): Add addTags and addFields helper functions to Point
1. [#97](https://github.com/influxdata/influxdb-client-java/pull/97): Add the ability to specify the org and the bucket when creating the client

### Bugs
1. [#98](https://github.com/influxdata/influxdb-client-java/issues/98): @Column supports super class inheritance for write measurements

## 1.6.0 [2020-03-13]

### Features
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,20 @@ private void cacheMeasurementClass(@Nonnull final Class<?>... measurementTypes)
influxColumnAndFieldMap = initialMap;
}

for (Field field : measurementType.getDeclaredFields()) {
Column colAnnotation = field.getAnnotation(Column.class);
if (colAnnotation != null) {
String name = colAnnotation.name();
if (name.isEmpty()) {
name = field.getName();
Class<?> currentMeasurementType = measurementType;
while (currentMeasurementType != null) {
for (Field field : currentMeasurementType.getDeclaredFields()) {
Column colAnnotation = field.getAnnotation(Column.class);
if (colAnnotation != null) {
String name = colAnnotation.name();
if (name.isEmpty()) {
name = field.getName();
}
influxColumnAndFieldMap.put(name, field);
}
influxColumnAndFieldMap.put(name, field);
}

currentMeasurementType = currentMeasurementType.getSuperclass();
}
}
}
Expand Down
42 changes: 42 additions & 0 deletions client/src/test/java/com/influxdb/client/WriteApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import java.util.concurrent.TimeUnit;
import javax.annotation.Nonnull;

import com.influxdb.annotations.Column;
import com.influxdb.annotations.Measurement;
import com.influxdb.client.domain.WritePrecision;
import com.influxdb.client.internal.AbstractInfluxDBClientTest;
import com.influxdb.client.write.Point;
Expand Down Expand Up @@ -163,6 +165,34 @@ void writeMeasurement() throws InterruptedException {
Assertions.assertThat(request.getRequestUrl().queryParameter("precision")).isEqualTo("ns");
}

@Test
void writeMeasurementInheritance() throws InterruptedException {

mockServer.enqueue(new MockResponse());

writeApi = influxDBClient.getWriteApi();

Metric measurement = new Visitor();
//noinspection CastCanBeRemovedNarrowingVariableType
((Visitor) measurement).count = 99;
measurement.source = "metric-source";

// response
writeApi.writeMeasurement("b1", "org1", WritePrecision.S, measurement);

RecordedRequest request = mockServer.takeRequest(10L, TimeUnit.SECONDS);

// value
Assertions.assertThat(request.getBody().readUtf8()).isEqualTo("visitor,source=metric-source count=99i");

// organization
Assertions.assertThat(request.getRequestUrl().queryParameter("org")).isEqualTo("org1");
// bucket
Assertions.assertThat(request.getRequestUrl().queryParameter("bucket")).isEqualTo("b1");
// precision
Assertions.assertThat(request.getRequestUrl().queryParameter("precision")).isEqualTo("s");
}

@Test
void writeMeasurementNull() {

Expand Down Expand Up @@ -843,4 +873,16 @@ private String getRequestBody(@Nonnull final MockWebServer server) {
return recordedRequest.getBody().readUtf8();
}

public abstract class Metric {
@Column(name = "source", tag = true)
private String source;
}


@Measurement(name = "visitor")
public class Visitor extends Metric {
@Column(name = "count")
private long count;
}

}

0 comments on commit 826bb6c

Please sign in to comment.