Use uppercase letters for environment variable naming.#809
Conversation
| .replace('_', '.'); | ||
| // Here we need to convert these characters back. | ||
| // Additionally, as the env variables are in uppercase, we need to convert them to lowercase. | ||
| .replace('_', '.').toLowerCase(Locale.ENGLISH); |
There was a problem hiding this comment.
Use toLowerCase to convert environment variable values from upper case to lower case may be not always correct.
Currently, any configuration class can be deserialized from external configuration source like file or environment variables, these classes may contain fields that are using camel style naming conventions.
For example, in org.bithon.agent.observability.tracing.config.TraceConfig, there's a field naming as traceResponseHeader. Also existing test case does not cover such scenario.
So to support the uppcase of the environment variables is a little bit tricky now. I have not come up with any idea to support it.
There was a problem hiding this comment.
After reading the relevant code, I had a general understanding that the scope involved exceeded my previous estimation.
There was a problem hiding this comment.
Can add a property like camelNamingStyleProp to the TestProp class in the test file and improve the test case test_Environment to verify the deserialization for such field.
This makes sure new changes in this PR is compatible with such case. The comment below may be one way to resolve such problem.
|
jackson supports to deserialize objects in case insensitive way. Maybe this is the simplest way. Here's the sample code //
// Illustration of deserialization from uppercase 'Age' to lower case property 'age'
//
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
public class Main {
public static void main(String[] args) throws Exception {
ObjectMapper mapper = new ObjectMapper();
// Enable case-insensitive property matching
mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
String json = "{\"name\": \"John\", \"Age\": 30}";
Person person = mapper.readValue(json, Person.class);
System.out.println(person);
}
}
class Person {
private String name;
private int age;
// getters and setters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person{name='" + name + "', age=" + age + '}';
}
}//
// Illustration of same properties in different cases
//
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
public class Main {
public static void main(String[] args) throws Exception {
ObjectMapper mapper = new ObjectMapper();
// Enable case-insensitive property matching
mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
String json = "{\"name\": \"John\", \"Name\": \"Doe\"}";
Person person = mapper.readValue(json, Person.class);
System.out.println(person);
}
}
class Person {
private String name;
// getters and setters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Person{name='" + name + "'}";
}
} |
| ExternalSource.build(), | ||
| CommandLineArgsSource.build("bithon."), | ||
| EnvironmentSource.build("bithon_")); | ||
| EnvironmentSource.build("BITHON_")); |
There was a problem hiding this comment.
We can keep the lower case for compatibility.
There was a problem hiding this comment.
I agree that the case of environment variables should not be strictly enforced. It is only necessary to ensure that the environment variables can be recognized, regardless of whether they are in uppercase or lowercase
Use uppercase letters for environment variable naming.
#808