File tree Expand file tree Collapse file tree
main/java/io/opentelemetry/contrib/dynamic/policy/source
test/java/io/opentelemetry/contrib/dynamic/policy/source Expand file tree Collapse file tree Original file line number Diff line number Diff line change 77
88import com .google .errorprone .annotations .Immutable ;
99import java .util .List ;
10+ import java .util .Locale ;
1011import java .util .Objects ;
12+ import javax .annotation .Nonnull ;
1113import 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 *
Original file line number Diff line number Diff 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}" );
You can’t perform that action at this time.
0 commit comments