Skip to content

Commit 6eed41c

Browse files
committed
Update DeclarativeConfigProperties to return null on type mismatch
1 parent df9a72b commit 6eed41c

File tree

2 files changed

+28
-34
lines changed

2 files changed

+28
-34
lines changed

api/incubator/src/main/java/io/opentelemetry/api/incubator/config/DeclarativeConfigProperties.java

Lines changed: 23 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ static Map<String, Object> toMap(DeclarativeConfigProperties declarativeConfigPr
4646
/**
4747
* Returns a {@link String} configuration property.
4848
*
49-
* @return null if the property has not been configured
50-
* @throws DeclarativeConfigException if the property is not a valid scalar string
49+
* @return null if the property has not been configured or if the property is not a valid scalar
50+
* string
5151
*/
5252
@Nullable
5353
String getString(String name);
@@ -56,8 +56,7 @@ static Map<String, Object> toMap(DeclarativeConfigProperties declarativeConfigPr
5656
* Returns a {@link String} configuration property.
5757
*
5858
* @return a {@link String} configuration property or {@code defaultValue} if a property with
59-
* {@code name} has not been configured
60-
* @throws DeclarativeConfigException if the property is not a valid scalar string
59+
* {@code name} has not been configured or is not a valid scalar string
6160
*/
6261
default String getString(String name, String defaultValue) {
6362
return defaultIfNull(getString(name), defaultValue);
@@ -67,8 +66,8 @@ default String getString(String name, String defaultValue) {
6766
* Returns a {@link Boolean} configuration property. Implementations should use the same rules as
6867
* {@link Boolean#parseBoolean(String)} for handling the values.
6968
*
70-
* @return null if the property has not been configured
71-
* @throws DeclarativeConfigException if the property is not a valid scalar boolean
69+
* @return null if the property has not been configured or if the property is not a valid scalar
70+
* boolean
7271
*/
7372
@Nullable
7473
Boolean getBoolean(String name);
@@ -77,8 +76,7 @@ default String getString(String name, String defaultValue) {
7776
* Returns a {@link Boolean} configuration property.
7877
*
7978
* @return a {@link Boolean} configuration property or {@code defaultValue} if a property with
80-
* {@code name} has not been configured
81-
* @throws DeclarativeConfigException if the property is not a valid scalar boolean
79+
* {@code name} has not been configured or is not a valid scalar boolean
8280
*/
8381
default boolean getBoolean(String name, boolean defaultValue) {
8482
return defaultIfNull(getBoolean(name), defaultValue);
@@ -90,8 +88,8 @@ default boolean getBoolean(String name, boolean defaultValue) {
9088
* <p>If the underlying config property is {@link Long}, it is converted to {@link Integer} with
9189
* {@link Long#intValue()} which may result in loss of precision.
9290
*
93-
* @return null if the property has not been configured
94-
* @throws DeclarativeConfigException if the property is not a valid scalar integer
91+
* @return null if the property has not been configured or if the property is not a valid scalar
92+
* integer
9593
*/
9694
@Nullable
9795
Integer getInt(String name);
@@ -103,8 +101,7 @@ default boolean getBoolean(String name, boolean defaultValue) {
103101
* {@link Long#intValue()} which may result in loss of precision.
104102
*
105103
* @return a {@link Integer} configuration property or {@code defaultValue} if a property with
106-
* {@code name} has not been configured
107-
* @throws DeclarativeConfigException if the property is not a valid scalar integer
104+
* {@code name} has not been configured or is not a valid scalar integer
108105
*/
109106
default int getInt(String name, int defaultValue) {
110107
return defaultIfNull(getInt(name), defaultValue);
@@ -113,8 +110,8 @@ default int getInt(String name, int defaultValue) {
113110
/**
114111
* Returns a {@link Long} configuration property.
115112
*
116-
* @return null if the property has not been configured
117-
* @throws DeclarativeConfigException if the property is not a valid scalar long
113+
* @return null if the property has not been configured or if the property is not a valid scalar
114+
* long
118115
*/
119116
@Nullable
120117
Long getLong(String name);
@@ -123,8 +120,7 @@ default int getInt(String name, int defaultValue) {
123120
* Returns a {@link Long} configuration property.
124121
*
125122
* @return a {@link Long} configuration property or {@code defaultValue} if a property with {@code
126-
* name} has not been configured
127-
* @throws DeclarativeConfigException if the property is not a valid scalar long
123+
* name} has not been configured or is not a valid scalar long
128124
*/
129125
default long getLong(String name, long defaultValue) {
130126
return defaultIfNull(getLong(name), defaultValue);
@@ -133,8 +129,8 @@ default long getLong(String name, long defaultValue) {
133129
/**
134130
* Returns a {@link Double} configuration property.
135131
*
136-
* @return null if the property has not been configured
137-
* @throws DeclarativeConfigException if the property is not a valid scalar double
132+
* @return null if the property has not been configured or if the property is not a valid scalar
133+
* double
138134
*/
139135
@Nullable
140136
Double getDouble(String name);
@@ -143,8 +139,7 @@ default long getLong(String name, long defaultValue) {
143139
* Returns a {@link Double} configuration property.
144140
*
145141
* @return a {@link Double} configuration property or {@code defaultValue} if a property with
146-
* {@code name} has not been configured
147-
* @throws DeclarativeConfigException if the property is not a valid scalar double
142+
* {@code name} has not been configured or is not a valid scalar double
148143
*/
149144
default double getDouble(String name, double defaultValue) {
150145
return defaultIfNull(getDouble(name), defaultValue);
@@ -158,8 +153,8 @@ default double getDouble(String name, double defaultValue) {
158153
* @param scalarType the scalar type, one of {@link String}, {@link Boolean}, {@link Long} or
159154
* {@link Double}
160155
* @return a {@link List} configuration property, or null if the property has not been configured
161-
* @throws DeclarativeConfigException if the property is not a valid sequence of scalars, or if
162-
* {@code scalarType} is not supported
156+
* or if the property is not a valid sequence of scalars
157+
* @throws DeclarativeConfigException if {@code scalarType} is not supported
163158
*/
164159
@Nullable
165160
<T> List<T> getScalarList(String name, Class<T> scalarType);
@@ -172,8 +167,7 @@ default double getDouble(String name, double defaultValue) {
172167
* @param scalarType the scalar type, one of {@link String}, {@link Boolean}, {@link Long} or
173168
* {@link Double}
174169
* @return a {@link List} configuration property or {@code defaultValue} if a property with {@code
175-
* name} has not been configured
176-
* @throws DeclarativeConfigException if the property is not a valid sequence of scalars
170+
* name} has not been configured or is not a valid sequence of scalars
177171
*/
178172
default <T> List<T> getScalarList(String name, Class<T> scalarType, List<T> defaultValue) {
179173
return defaultIfNull(getScalarList(name, scalarType), defaultValue);
@@ -183,8 +177,7 @@ default <T> List<T> getScalarList(String name, Class<T> scalarType, List<T> defa
183177
* Returns a {@link DeclarativeConfigProperties} configuration property.
184178
*
185179
* @return a map-valued configuration property, or {@code null} if {@code name} has not been
186-
* configured
187-
* @throws DeclarativeConfigException if the property is not a mapping
180+
* configured or is not a mapping
188181
*/
189182
@Nullable
190183
DeclarativeConfigProperties getStructured(String name);
@@ -193,8 +186,7 @@ default <T> List<T> getScalarList(String name, Class<T> scalarType, List<T> defa
193186
* Returns a list of {@link DeclarativeConfigProperties} configuration property.
194187
*
195188
* @return a map-valued configuration property, or {@code defaultValue} if {@code name} has not
196-
* been configured
197-
* @throws DeclarativeConfigException if the property is not a mapping
189+
* been configured or is not a mapping
198190
*/
199191
default DeclarativeConfigProperties getStructured(
200192
String name, DeclarativeConfigProperties defaultValue) {
@@ -210,8 +202,7 @@ default DeclarativeConfigProperties getStructured(
210202
* but empty vs. not set, use {@link #getStructured(String)}.
211203
*
212204
* @return a map-valued configuration property, or an empty {@link DeclarativeConfigProperties}
213-
* instance if {@code name} has not been configured
214-
* @throws DeclarativeConfigException if the property is not a mapping
205+
* instance if {@code name} has not been configured or is not a mapping
215206
*/
216207
default DeclarativeConfigProperties get(String name) {
217208
return getStructured(name, empty());
@@ -221,8 +212,7 @@ default DeclarativeConfigProperties get(String name) {
221212
* Returns a list of {@link DeclarativeConfigProperties} configuration property.
222213
*
223214
* @return a list of map-valued configuration property, or {@code null} if {@code name} has not
224-
* been configured
225-
* @throws DeclarativeConfigException if the property is not a sequence of mappings
215+
* been configured or is not a sequence of mappings
226216
*/
227217
@Nullable
228218
List<DeclarativeConfigProperties> getStructuredList(String name);
@@ -231,8 +221,7 @@ default DeclarativeConfigProperties get(String name) {
231221
* Returns a list of {@link DeclarativeConfigProperties} configuration property.
232222
*
233223
* @return a list of map-valued configuration property, or {@code defaultValue} if {@code name}
234-
* has not been configured
235-
* @throws DeclarativeConfigException if the property is not a sequence of mappings
224+
* has not been configured or is not a sequence of mappings
236225
*/
237226
default List<DeclarativeConfigProperties> getStructuredList(
238227
String name, List<DeclarativeConfigProperties> defaultValue) {

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,11 @@ void wrongType() {
230230
assertThat(otherProps.getDouble("str_key")).isNull();
231231
assertThat(otherProps.getBoolean("str_key")).isNull();
232232
assertThat(otherProps.getScalarList("str_key", String.class)).isNull();
233+
assertThat(otherProps.getScalarList("str_list_key", Long.class)).isNull();
233234
assertThat(otherProps.getStructured("str_key")).isNull();
234235
assertThat(otherProps.getStructuredList("str_key")).isNull();
236+
assertThat(otherProps.getStructured("str_list_key")).isNull();
237+
assertThat(otherProps.getStructuredList("map_key")).isNull();
235238

236239
assertWarning("Ignoring value for key [int_key] because it is Integer instead of String: 1");
237240
assertWarning(
@@ -240,6 +243,8 @@ void wrongType() {
240243
"Ignoring value for key [str_key] because it is String instead of Double: str_value");
241244
assertWarning(
242245
"Ignoring value for key [str_key] because it is String instead of Boolean: str_value");
246+
assertWarning(
247+
"Ignoring value for key [str_list_key] because it is String instead of Long: val1");
243248
}
244249

245250
private void assertWarning(String message) {

0 commit comments

Comments
 (0)