config
config copied to clipboard
Case insensitive keys / paths?
I have not been able to figure out how to allow for case-insensitive keys / paths when pulling fields from a configuration. I do not want to require the user (who may write a HOCON config) to match case precisely.
Is there any way to enable this through the API?
all i can think of is to postprocess the config when you load it by lowercasing everything
Also interested in an option to simply ignore sensitivity. I like to "properly" name things in my config spec, like "ThisIsANumber", but also when given a config, "thisisanumber" should properly match.
You get weird problems if just a single case mistake is made and a config value is ignored...
@havocp how can you postprocess a loaded config to lowercase every key?
You’d make a copy while lowercasing each key... there isn’t a nice mapKeys kind of method so would be manual code to write.
Note that the typical way to use the library shares a Config across the process, so to do this you’d have to be sure only your own code will be using the Config instance in question. Other code might assume the key case matters.
@havocp, my question was purely related to the technical "how to". Do you have any hint on how to "make a copy while lowercasing keys"?? It is not a simple task as Config is immutable and Config itself is an interface. The "withValue" method does not really help at all... Thanks!
@havocp I tried to manage this the way you proposed. But it is not feasible at all. I got SOME elements deeper within my structure where I need to preserve the case... So, I really DO need a way to get.... and tell it to ignore the case. I am really wondering why I am the only one with this issue. :(
Most people are requiring a certain case to be used, afaik
I guess an API like config.caseInsensitive.getInt
would be possible but I’m not sure how complex it would get. The case insensitivity should probably only affect the public getters and not e.g. resolving substitutions, I guess, but I’m not certain.
One idea would be a special operator, such as :=
, that creates a case-sensitive map:
DatabaseSchema := {
myTableName = {
indexes := {
field_one = "ascending"
field_two = "ascending"
}
fields := {
field_one = "int"
field_two = "string"
}
}
}
The importance of case sensitivity here is that the keys (field names and table names) are both case sensitive and unique (I don't want to support config for the same key with a different value).
This would be the alternative to the following, which is harder to read.
DatabaseSchema = [{
tableName = "myTableName"
indexes = [{
fieldName = "field_one"
order = "ascending"
}, {
fieldName = "field_two"
order = "descending"
}]
fields = [{
name = "field_one"
type = "int"
}, {
name = "field_two"
type = "str"
}]
}]
Just as an implementation hint, for maps a TreeMap with String.CASE_INSENSITIVE_ORDER
as the Comparator
for keys could be used.