Skip to content

Commit 072f19a

Browse files
committed
feat: Add read-only getters for Point.fields and Point.tags
This makes testing easier.
1 parent 9e0ec0b commit 072f19a

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

client/src/main/java/com/influxdb/client/write/Point.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.util.Map;
3131
import java.util.Set;
3232
import java.util.TreeMap;
33+
import java.util.Collections;
3334
import java.util.concurrent.TimeUnit;
3435
import java.util.stream.Collectors;
3536
import java.util.stream.Stream;
@@ -95,6 +96,24 @@ public static Point measurement(@Nonnull final String measurementName) {
9596
return new Point(measurementName);
9697
}
9798

99+
/**
100+
* Returns a read-only reference to the tags.
101+
* @return The point tags as read-only map.
102+
*/
103+
@Nonnull
104+
public Map<String, String> getTags() {
105+
return Collections.unmodifiableMap(this.tags);
106+
}
107+
108+
/**
109+
* Returns a read-only reference to the fields.
110+
* @return The point fields as read-only map.
111+
*/
112+
@Nonnull
113+
public Map<String, Object> getFields() {
114+
return Collections.unmodifiableMap(this.fields);
115+
}
116+
98117
/**
99118
* Adds or replaces a tag value for this point.
100119
*

client/src/test/java/com/influxdb/client/write/PointTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,17 @@
2424
import java.math.BigDecimal;
2525
import java.math.BigInteger;
2626
import java.time.Instant;
27+
import java.util.Collections;
2728
import java.util.HashMap;
29+
import java.util.Map;
2830

2931
import com.influxdb.client.domain.WritePrecision;
3032

3133
import org.assertj.core.api.Assertions;
3234
import org.junit.jupiter.api.Test;
3335

36+
import static org.junit.jupiter.api.Assertions.assertThrows;
37+
3438
/**
3539
* @author Jakub Bednar (bednar@github) (11/10/2018 12:57)
3640
*/
@@ -438,4 +442,21 @@ void addFields() {
438442

439443
Assertions.assertThat(point.toLineProtocol()).isEqualTo("h2o,location=europe accepted=true,level=2i,power=2.56");
440444
}
445+
446+
@Test
447+
void getFieldsGetTags() {
448+
Point point = Point.measurement("h2 o")
449+
.addTag("location", "europe")
450+
.addField("level", 2);
451+
452+
Map<String, Object> fields = point.getFields();
453+
Map<String, String> tags = point.getTags();
454+
455+
Assertions.assertThat(fields).isEqualTo(Map.of("level",2L));
456+
Assertions.assertThat(tags).isEqualTo(Map.of("location","europe"));
457+
458+
// Assert that returned maps are immutable
459+
assertThrows(UnsupportedOperationException.class, () -> fields.put("test", "value"));
460+
assertThrows(UnsupportedOperationException.class, () -> tags.put("test", "value"));
461+
}
441462
}

0 commit comments

Comments
 (0)