Skip to content

Commit ae69564

Browse files
jackshiraziCopilot
andauthored
[dynamic control] add SourceFormat string to enum conversion (#2737)
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 902a5c3 commit ae69564

2 files changed

Lines changed: 45 additions & 0 deletions

File tree

dynamic-control/src/main/java/io/opentelemetry/contrib/dynamic/policy/source/SourceFormat.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77

88
import com.google.errorprone.annotations.Immutable;
99
import java.util.List;
10+
import java.util.Locale;
1011
import java.util.Objects;
12+
import javax.annotation.Nonnull;
1113
import javax.annotation.Nullable;
1214

1315
/** Supported source formats and their parser dispatch. */
@@ -27,6 +29,28 @@ public String configValue() {
2729
return configValue;
2830
}
2931

32+
/**
33+
* Converts string value case insensitively to enum value. Leading and trailing whitespace is
34+
* removed, then the remainder is matched case-insensitively against {@link #configValue()} for
35+
* each format.
36+
*
37+
* @param value the string from config (e.g. {@code "keyvalue"}, {@code "JSONKEYVALUE"})
38+
* @return the matching format
39+
* @throws NullPointerException if value is null
40+
* @throws IllegalArgumentException if no format matches the trimmed value
41+
*/
42+
public static SourceFormat fromConfigValue(@Nonnull String value) {
43+
Objects.requireNonNull(value, "value cannot be null");
44+
String normalized = value.trim().toLowerCase(Locale.ROOT);
45+
for (SourceFormat format : values()) {
46+
if (format.configValue.equals(normalized)) {
47+
return format;
48+
}
49+
}
50+
throw new IllegalArgumentException(
51+
"Unknown source format (normalized): '" + normalized + "' (original: '" + value + "')");
52+
}
53+
3054
/**
3155
* Parses source text into normalized wrappers for this format.
3256
*

dynamic-control/src/test/java/io/opentelemetry/contrib/dynamic/policy/source/SourceFormatTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,27 @@ void configValuesAreStable() {
1919
assertThat(SourceFormat.KEYVALUE.configValue()).isEqualTo("keyvalue");
2020
}
2121

22+
@Test
23+
void fromConfigValueParsesCaseInsensitive() {
24+
assertThat(SourceFormat.fromConfigValue("KEYVALUE")).isEqualTo(SourceFormat.KEYVALUE);
25+
assertThat(SourceFormat.fromConfigValue("JsonKeyValue")).isEqualTo(SourceFormat.JSONKEYVALUE);
26+
}
27+
28+
@Test
29+
void fromConfigValueTrimsWhitespace() {
30+
assertThat(SourceFormat.fromConfigValue(" keyvalue ")).isEqualTo(SourceFormat.KEYVALUE);
31+
}
32+
33+
@Test
34+
void fromConfigValueRejectsNullAndUnknown() {
35+
assertThatThrownBy(() -> SourceFormat.fromConfigValue(null))
36+
.isInstanceOf(NullPointerException.class)
37+
.hasMessage("value cannot be null");
38+
assertThatThrownBy(() -> SourceFormat.fromConfigValue("other"))
39+
.isInstanceOf(IllegalArgumentException.class)
40+
.hasMessage("Unknown source format (normalized): 'other' (original: 'other')");
41+
}
42+
2243
@Test
2344
void parseDelegatesToJsonParser() {
2445
List<SourceWrapper> parsed = SourceFormat.JSONKEYVALUE.parse("{\"trace-sampling\": 0.5}");

0 commit comments

Comments
 (0)