Skip to content

Commit 094cd5c

Browse files
committed
Add support for dot notation
1 parent 932aa1d commit 094cd5c

2 files changed

Lines changed: 110 additions & 2 deletions

File tree

sdk-extensions/incubator/src/main/java/io/opentelemetry/sdk/extension/incubator/fileconfig/ExtendedConfigProperties.java

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.util.List;
1616
import java.util.Map;
1717
import java.util.StringJoiner;
18+
import java.util.function.BiFunction;
1819
import javax.annotation.Nullable;
1920

2021
/**
@@ -110,6 +111,9 @@ private static boolean isMap(Object object) {
110111
@Override
111112
public String getString(String name) {
112113
Object value = simpleEntries.get(name);
114+
if (value == null && hasDotNotation(name)) {
115+
return applyToChild(name, ExtendedConfigProperties::getString);
116+
}
113117
if (value instanceof String) {
114118
return (String) value;
115119
}
@@ -120,6 +124,9 @@ public String getString(String name) {
120124
@Override
121125
public Boolean getBoolean(String name) {
122126
Object value = simpleEntries.get(name);
127+
if (value == null && hasDotNotation(name)) {
128+
return applyToChild(name, ExtendedConfigProperties::getBoolean);
129+
}
123130
if (value instanceof Boolean) {
124131
return (Boolean) value;
125132
}
@@ -130,6 +137,9 @@ public Boolean getBoolean(String name) {
130137
@Override
131138
public Integer getInt(String name) {
132139
Object value = simpleEntries.get(name);
140+
if (value == null && hasDotNotation(name)) {
141+
return applyToChild(name, ExtendedConfigProperties::getInt);
142+
}
133143
if (value instanceof Integer) {
134144
return (Integer) value;
135145
}
@@ -140,6 +150,9 @@ public Integer getInt(String name) {
140150
@Override
141151
public Long getLong(String name) {
142152
Object value = simpleEntries.get(name);
153+
if (value == null && hasDotNotation(name)) {
154+
return applyToChild(name, ExtendedConfigProperties::getLong);
155+
}
143156
if (value instanceof Integer) {
144157
return ((Integer) value).longValue();
145158
}
@@ -153,6 +166,9 @@ public Long getLong(String name) {
153166
@Override
154167
public Double getDouble(String name) {
155168
Object value = simpleEntries.get(name);
169+
if (value == null && hasDotNotation(name)) {
170+
return applyToChild(name, ExtendedConfigProperties::getDouble);
171+
}
156172
if (value instanceof Float) {
157173
return ((Float) value).doubleValue();
158174
}
@@ -170,9 +186,16 @@ public Duration getDuration(String name) {
170186
}
171187

172188
@Override
173-
@SuppressWarnings("unchecked")
189+
@SuppressWarnings({"unchecked", "NullAway"})
174190
public List<String> getList(String name) {
175191
Object value = simpleEntries.get(name);
192+
if (value == null && hasDotNotation(name)) {
193+
List<String> result = applyToChild(name, ExtendedConfigProperties::getList);
194+
if (result != null) {
195+
return result;
196+
}
197+
return Collections.emptyList();
198+
}
176199
if (value instanceof List) {
177200
return (List<String>) value;
178201
}
@@ -184,14 +207,47 @@ public Map<String, String> getMap(String name) {
184207
throw new UnsupportedOperationException("Use getConfigProperties(String) instead");
185208
}
186209

210+
private static boolean hasDotNotation(String name) {
211+
return name.contains(".");
212+
}
213+
214+
/** Should only be called after {@link #hasDotNotation(String)} returns {@code true}. */
215+
@Nullable
216+
private <T> T applyToChild(
217+
String name, BiFunction<ExtendedConfigProperties, String, T> function) {
218+
String[] parts = name.split("\\.", 2);
219+
assert parts.length == 2;
220+
if (parts.length != 2) {
221+
throw new IllegalStateException("Error!");
222+
}
223+
ExtendedConfigProperties childProps = getConfigPropertiesInternal(parts[0]);
224+
if (childProps == null) {
225+
return null;
226+
}
227+
return function.apply(childProps, parts[1]);
228+
}
229+
187230
@Nullable
188231
public ExtendedConfigProperties getConfigProperties(String name) {
232+
ExtendedConfigProperties value = getConfigPropertiesInternal(name);
233+
if (value == null && hasDotNotation(name)) {
234+
return applyToChild(name, ExtendedConfigProperties::getConfigProperties);
235+
}
236+
return value;
237+
}
238+
239+
@Nullable
240+
private ExtendedConfigProperties getConfigPropertiesInternal(String name) {
189241
return mapEntries.get(name);
190242
}
191243

192244
@Nullable
193245
public List<ExtendedConfigProperties> getListConfigProperties(String name) {
194-
return listEntries.get(name);
246+
List<ExtendedConfigProperties> value = listEntries.get(name);
247+
if (value == null && hasDotNotation(name)) {
248+
return applyToChild(name, ExtendedConfigProperties::getListConfigProperties);
249+
}
250+
return value;
195251
}
196252

197253
@Override

sdk-extensions/incubator/src/test/java/io/opentelemetry/sdk/extension/incubator/fileconfig/ExtendedConfigPropertiesTest.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,35 @@ void additionalProperties() {
119119
assertThat(listKeyProps2.getInt("int_key1")).isEqualTo(2);
120120
}
121121

122+
@Test
123+
void dotNotation() {
124+
assertThat(extendedConfigProps.getString("other.str_key")).isEqualTo("str_value");
125+
assertThat(extendedConfigProps.getInt("other.int_key")).isEqualTo(1);
126+
assertThat(extendedConfigProps.getDouble("other.float_key")).isEqualTo(1.1);
127+
assertThat(extendedConfigProps.getBoolean("other.bool_key")).isEqualTo(true);
128+
assertThat(extendedConfigProps.getList("other.str_list_key"))
129+
.isEqualTo(Arrays.asList("val1", "val2"));
130+
assertThat(extendedConfigProps.getList("other.int_list_key"))
131+
.isEqualTo(Arrays.asList("1", "2"));
132+
assertThat(extendedConfigProps.getList("other.float_list_key"))
133+
.isEqualTo(Arrays.asList("1.1", "2.2"));
134+
assertThat(extendedConfigProps.getList("other.bool_list_key"))
135+
.isEqualTo(Arrays.asList("true", "false"));
136+
assertThat(extendedConfigProps.getString("other.map_key.str_key1")).isEqualTo("str_value1");
137+
assertThat(extendedConfigProps.getInt("other.map_key.int_key1")).isEqualTo(2);
138+
assertThat(extendedConfigProps.getString("other.map_key.map_key1.str_key2"))
139+
.isEqualTo("str_value2");
140+
assertThat(extendedConfigProps.getInt("other.map_key.map_key1.int_key2")).isEqualTo(3);
141+
List<ExtendedConfigProperties> extendedConfigProperties =
142+
extendedConfigProps.getListConfigProperties("other.list_key");
143+
assertThat(extendedConfigProperties).isNotNull().hasSize(2);
144+
ExtendedConfigProperties otherMapKeyProps =
145+
extendedConfigProps.getConfigProperties("other.map_key");
146+
assertThat(otherMapKeyProps.getString("str_key1")).isEqualTo("str_value1");
147+
assertThat(otherMapKeyProps.getInt("int_key1")).isEqualTo(2);
148+
assertThat(otherMapKeyProps.getConfigProperties("map_key1")).isNotNull();
149+
}
150+
122151
@Test
123152
void defaults() {
124153
assertThat(extendedConfigProps.getString("foo", "bar")).isEqualTo("bar");
@@ -128,6 +157,15 @@ void defaults() {
128157
assertThat(extendedConfigProps.getBoolean("foo", true)).isTrue();
129158
assertThat(extendedConfigProps.getList("foo", Collections.singletonList("bar")))
130159
.isEqualTo(Collections.singletonList("bar"));
160+
161+
// Dot notation
162+
assertThat(extendedConfigProps.getString("foo.bar", "baz")).isEqualTo("baz");
163+
assertThat(extendedConfigProps.getInt("foo.bar", 1)).isEqualTo(1);
164+
assertThat(extendedConfigProps.getLong("foo.bar", 1)).isEqualTo(1);
165+
assertThat(extendedConfigProps.getDouble("foo.bar", 1.1)).isEqualTo(1.1);
166+
assertThat(extendedConfigProps.getBoolean("foo.bar", true)).isTrue();
167+
assertThat(extendedConfigProps.getList("foo.bar", Collections.singletonList("baz")))
168+
.isEqualTo(Collections.singletonList("baz"));
131169
}
132170

133171
@Test
@@ -140,6 +178,10 @@ void missingKeys() {
140178
assertThat(extendedConfigProps.getList("foo")).isEmpty();
141179
assertThat(extendedConfigProps.getConfigProperties("foo")).isNull();
142180
assertThat(extendedConfigProps.getListConfigProperties("foo")).isNull();
181+
182+
// Dot notation
183+
assertThat(extendedConfigProps.getString("other.missing_key")).isNull();
184+
assertThat(extendedConfigProps.getString("other.map_key.missing_key")).isNull();
143185
}
144186

145187
@Test
@@ -155,6 +197,16 @@ void wrongType() {
155197
assertThat(otherProps.getList("str_key")).isEmpty();
156198
assertThat(otherProps.getConfigProperties("str_key")).isNull();
157199
assertThat(otherProps.getListConfigProperties("str_key")).isNull();
200+
201+
// Dot notation
202+
assertThat(extendedConfigProps.getString("other.int_key")).isNull();
203+
assertThat(extendedConfigProps.getInt("other.str_key")).isNull();
204+
assertThat(extendedConfigProps.getLong("other.str_key")).isNull();
205+
assertThat(extendedConfigProps.getDouble("other.str_key")).isNull();
206+
assertThat(extendedConfigProps.getBoolean("other.str_key")).isNull();
207+
assertThat(extendedConfigProps.getList("other.str_key")).isEmpty();
208+
assertThat(extendedConfigProps.getConfigProperties("other.str_key")).isNull();
209+
assertThat(extendedConfigProps.getListConfigProperties("other.str_key")).isNull();
158210
}
159211

160212
@Test

0 commit comments

Comments
 (0)