Skip to content

Commit a6a9acb

Browse files
authored
Add getStructured default method, add empty StructuredConfigProperties (#6759)
1 parent 361f035 commit a6a9acb

File tree

3 files changed

+150
-0
lines changed

3 files changed

+150
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.sdk.autoconfigure.spi.internal;
7+
8+
import java.util.Collections;
9+
import java.util.List;
10+
import java.util.Set;
11+
import javax.annotation.Nullable;
12+
13+
/** Empty instance of {@link StructuredConfigProperties}. */
14+
final class EmptyStructuredConfigProperties implements StructuredConfigProperties {
15+
16+
private static final EmptyStructuredConfigProperties INSTANCE =
17+
new EmptyStructuredConfigProperties();
18+
19+
private EmptyStructuredConfigProperties() {}
20+
21+
static EmptyStructuredConfigProperties getInstance() {
22+
return INSTANCE;
23+
}
24+
25+
@Nullable
26+
@Override
27+
public String getString(String name) {
28+
return null;
29+
}
30+
31+
@Nullable
32+
@Override
33+
public Boolean getBoolean(String name) {
34+
return null;
35+
}
36+
37+
@Nullable
38+
@Override
39+
public Integer getInt(String name) {
40+
return null;
41+
}
42+
43+
@Nullable
44+
@Override
45+
public Long getLong(String name) {
46+
return null;
47+
}
48+
49+
@Nullable
50+
@Override
51+
public Double getDouble(String name) {
52+
return null;
53+
}
54+
55+
@Nullable
56+
@Override
57+
public <T> List<T> getScalarList(String name, Class<T> scalarType) {
58+
return null;
59+
}
60+
61+
@Nullable
62+
@Override
63+
public StructuredConfigProperties getStructured(String name) {
64+
return null;
65+
}
66+
67+
@Nullable
68+
@Override
69+
public List<StructuredConfigProperties> getStructuredList(String name) {
70+
return null;
71+
}
72+
73+
@Override
74+
public Set<String> getPropertyKeys() {
75+
return Collections.emptySet();
76+
}
77+
}

sdk-extensions/autoconfigure-spi/src/main/java/io/opentelemetry/sdk/autoconfigure/spi/internal/StructuredConfigProperties.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,17 @@
2424
*/
2525
public interface StructuredConfigProperties {
2626

27+
/**
28+
* Return an empty {@link StructuredConfigProperties} instance.
29+
*
30+
* <p>Useful for walking the tree without checking for null. For example, to access a string key
31+
* nested at .foo.bar.baz, call: {@code config.getStructured("foo", empty()).getStructured("bar",
32+
* empty()).getString("baz")}.
33+
*/
34+
static StructuredConfigProperties empty() {
35+
return EmptyStructuredConfigProperties.getInstance();
36+
}
37+
2738
/**
2839
* Returns a {@link String} configuration property.
2940
*
@@ -168,6 +179,18 @@ default <T> List<T> getScalarList(String name, Class<T> scalarType, List<T> defa
168179
@Nullable
169180
StructuredConfigProperties getStructured(String name);
170181

182+
/**
183+
* Returns a {@link StructuredConfigProperties} configuration property.
184+
*
185+
* @return a map-valued configuration property, or {@code defaultValue} if {@code name} has not
186+
* been configured
187+
* @throws ConfigurationException if the property is not a mapping
188+
*/
189+
default StructuredConfigProperties getStructured(
190+
String name, StructuredConfigProperties defaultValue) {
191+
return defaultIfNull(getStructured(name), defaultValue);
192+
}
193+
171194
/**
172195
* Returns a list of {@link StructuredConfigProperties} configuration property.
173196
*
@@ -178,6 +201,18 @@ default <T> List<T> getScalarList(String name, Class<T> scalarType, List<T> defa
178201
@Nullable
179202
List<StructuredConfigProperties> getStructuredList(String name);
180203

204+
/**
205+
* Returns a list of {@link StructuredConfigProperties} configuration property.
206+
*
207+
* @return a list of map-valued configuration property, or {@code defaultValue} if {@code name}
208+
* has not been configured
209+
* @throws ConfigurationException if the property is not a sequence of mappings
210+
*/
211+
default List<StructuredConfigProperties> getStructuredList(
212+
String name, List<StructuredConfigProperties> defaultValue) {
213+
return defaultIfNull(getStructuredList(name), defaultValue);
214+
}
215+
181216
/**
182217
* Returns a set of all configuration property keys.
183218
*

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
package io.opentelemetry.sdk.extension.incubator.fileconfig;
77

8+
import static io.opentelemetry.sdk.autoconfigure.spi.internal.StructuredConfigProperties.empty;
89
import static org.assertj.core.api.Assertions.assertThat;
910

1011
import com.google.common.collect.ImmutableSet;
@@ -170,6 +171,18 @@ void additionalProperties() {
170171
assertThat(listKeyProps2.getInt("int_key1")).isEqualTo(2);
171172
}
172173

174+
@Test
175+
void treeWalking() {
176+
// Validate common pattern of walking down tree path which is not defined
177+
// Access string at .foo.bar.baz without null checking and without exception.
178+
assertThat(
179+
structuredConfigProps
180+
.getStructured("foo", empty())
181+
.getStructured("bar", empty())
182+
.getString("baz"))
183+
.isNull();
184+
}
185+
173186
@Test
174187
void defaults() {
175188
assertThat(structuredConfigProps.getString("foo", "bar")).isEqualTo("bar");
@@ -181,6 +194,9 @@ void defaults() {
181194
structuredConfigProps.getScalarList(
182195
"foo", String.class, Collections.singletonList("bar")))
183196
.isEqualTo(Collections.singletonList("bar"));
197+
assertThat(structuredConfigProps.getStructured("foo", empty())).isEqualTo(empty());
198+
assertThat(structuredConfigProps.getStructuredList("foo", Collections.emptyList()))
199+
.isEqualTo(Collections.emptyList());
184200
}
185201

186202
@Test
@@ -209,4 +225,26 @@ void wrongType() {
209225
assertThat(otherProps.getStructured("str_key")).isNull();
210226
assertThat(otherProps.getStructuredList("str_key")).isNull();
211227
}
228+
229+
@Test
230+
void emptyProperties() {
231+
assertThat(empty().getString("foo")).isNull();
232+
assertThat(empty().getInt("foo")).isNull();
233+
assertThat(empty().getLong("foo")).isNull();
234+
assertThat(empty().getDouble("foo")).isNull();
235+
assertThat(empty().getBoolean("foo")).isNull();
236+
assertThat(empty().getScalarList("foo", String.class)).isNull();
237+
assertThat(empty().getStructured("foo")).isNull();
238+
assertThat(empty().getStructuredList("foo")).isNull();
239+
assertThat(empty().getString("foo", "bar")).isEqualTo("bar");
240+
assertThat(empty().getInt("foo", 1)).isEqualTo(1);
241+
assertThat(empty().getLong("foo", 1)).isEqualTo(1);
242+
assertThat(empty().getDouble("foo", 1.1)).isEqualTo(1.1);
243+
assertThat(empty().getBoolean("foo", true)).isTrue();
244+
assertThat(empty().getScalarList("foo", String.class, Collections.singletonList("bar")))
245+
.isEqualTo(Collections.singletonList("bar"));
246+
assertThat(empty().getStructured("foo", empty())).isEqualTo(empty());
247+
assertThat(empty().getStructuredList("foo", Collections.emptyList()))
248+
.isEqualTo(Collections.emptyList());
249+
}
212250
}

0 commit comments

Comments
 (0)