config icon indicating copy to clipboard operation
config copied to clipboard

Case insensitive keys / paths?

Open tr8dr opened this issue 7 years ago • 9 comments

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?

tr8dr avatar Dec 21 '17 22:12 tr8dr

all i can think of is to postprocess the config when you load it by lowercasing everything

havocp avatar Dec 21 '17 23:12 havocp

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...

Zordid avatar Jun 13 '19 08:06 Zordid

@havocp how can you postprocess a loaded config to lowercase every key?

Zordid avatar Jun 13 '19 08:06 Zordid

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 avatar Jun 13 '19 11:06 havocp

@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!

Zordid avatar Jun 17 '19 13:06 Zordid

@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. :(

Zordid avatar Jun 19 '19 12:06 Zordid

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.

havocp avatar Jun 19 '19 12:06 havocp

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"
  }]
}]

robert-blankenship avatar Nov 13 '20 00:11 robert-blankenship

Just as an implementation hint, for maps a TreeMap with String.CASE_INSENSITIVE_ORDER as the Comparator for keys could be used.

sschuberth avatar Nov 22 '21 14:11 sschuberth