-
Notifications
You must be signed in to change notification settings - Fork 967
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
path expression to access array elements directly #30
Comments
What's the cost/benefit ratio of this feature proposal? |
not currently. you could do getConfigList("arr").get(0).getString("ip") or something though. hardcoding an index in a path string seems like it wouldn't be useful most of the time, so I'm not sure it'd be worth it... the existing way to do things doesn't seem that bad. saving a few characters once in a while vs adding a fair amount more code to support the more complex path expression syntax. don't know. |
Also, if someone wants to do it they could always create a bridge over to use XPATH anyway. |
It would be just a convenience function... If you have a complex array with many entries as a kind of default and want to override just one value, you could use this syntax too:
Perhaps this use case makes the proposal more interesting?! |
There's really nothing preventing you from developing such an extension. |
Someone else was asking about this lately; but in the context of using it from inside the config file, not in code. |
If we fix this then #160 could be made to work instead of throwing an exception, if we want. |
I think this feature would be really helpful. I have the same problem that @havocp mentioned (in the context of using it from inside the config file, not in code) . In my case, there exists a base conf file that has an array. I would like my clients to provide their own conf file that would override certain elements of the array in the base conf file. Right now, I have no elegant way to fix it, and that is just too bad. :( Any suggestions? |
The best you can do right now is probably to build the array up from |
Any update on this? It's been 4 years. |
Nobody is employed to go through feature requests here. The ones that have been done are the ones that somebody who wanted the feature chose to work on, or chose to hire someone to work on. If nobody ever wants this enough to work on it or pay for it, then it will never be done. It doesn't matter how many years people have thought it would be nice to have, if none of them thought it was important enough to work on. Not trying to be grumpy, trying to explain how it works. Most open source projects work the same way. |
@havocp, Sorry to say but I mulled over your suggestion to use ${?foo}, but it seemed more like a band-aid and not very elegant, especially given the fact that this can be achieved very nicely if we use JSONPath with JSON files. I was using JSONPath before switching to Typesafe Config, and I had switched for a very strong reason (includes and comments). My final solution right now is to use Typesafe Config to read and parse the conf files, convert them to strings, feed them into JSONPath, and do the necessary array access operations. There are some disadvantages and inconveniences, but this worked better for me overall. Your other suggestion of implementing this feature and contributing a pull request is something I might do in the future. Sorry, but I just do not have enough time for it right now. |
I've run into this same issue when I wanted to check for the existence of a key in a config: My config has a list of objects with a given attribute. I'm then diff'ing two of these configs and trying to check if a given key exists in a third config. Example pseudocode in the style of python/pyhocon: from pyhocon import ConfigFactory
config1 = ConfigFactory.parse_file("file1.conf")
config2 = ConfigFactory.parse_file("file2.conf")
config3 = ConfigFactory.parse_file("file3.conf")
differences = diff(config1, config2) # diff implemented elsewhere. Returns a list of keys that differ between the two configs. Signature: (ConfigTree, ConfigTree) -> [str]
for key in differences:
if config3.get(key, None) is None: # Defaults to None if the key isn't in config3
raise Exception("Value of {0} differs between config1 and config2, but '{0}' doesn't exist in config3!".format(key)) While I understand that no one is working on this right now, but if someone does, this use case might be interesting. Currently, I have to extend the key syntax (I did |
Ho do I use I tried the most natural way you'd think: my.config.array[0].value |
You would need to get the array, manually modify it, and then replace the entire array value |
I've been trying this, with config.processes being an array of objects, I did no changes to ml, yet it doesn't work
but it throws: com.typesafe.config.ConfigException$BugOrBroken: bug in method caller: not valid to create ConfigValue from: Config(SimpleConfigObject({"days":1,"id":"CalendarKlubschule","offsetSec":5,"runEverySec":120,"serviceUrl":"http://localhost:9000/test/klubschule/%s%s%s"}))
|
fromIterable is expecting plain Java objects but it looks like you have ConfigValue objects (or a mix, maybe a plain Java list with ConfigValue in it?) hard to say exactly without the ml-modification code. |
Ok managed to put it together. This replaces a global config I access, code is in Kotlin.
and this is how you use it:
|
I am also looking for a way to reference an array element within config.
In the appliesToZone attribute, I would like to reference a particular element of the allowedZones array |
A simple use case is like: Say we may have an environment variable or not(most likely it was an ipv6 address). When using IPV6 address, the config of akka hostname shold be To aquire a conditional config, I may use this:
If So I think it is worthy of implementing such a function. @viktorklang |
@WayneWang12 Wouldn't you be able to do: akka {
remote {
artery {
canonical {
hostname = "0.0.0.0" # external (logical) hostname
hostname = ${?IPV4}
hostname = ${?IPV6}
}
} |
No, because it fails to build up a connnection. IPV6 need to be a Play's ws client also need it. Details are here: RFC 2732 Url must be like:
|
I face the same issue today. From my point of view HOCON provide this mechanism as "path" for referencing. It should support arrays as well. |
We are facing the same issue today. Our config look like this :
We tried to overwrite
But as said before it does not work in hocon. A similar mechanism do exists for java properties and env vars. |
- Fixes lightbend#30 - Adds and modifies peek and find methods to allow indexing of ConfigList objects - Enables nested array and object access using indices in paths: listConfig.getString("ofObject.0.byteObj.byteVal") listConfig.getString("ofArray.2.2")
- Fixes lightbend#30 - Adds and modifies peek and find methods to allow indexing of ConfigList objects - Enables nested array and object access using indices in paths: listConfig.getString("ofObject.0.byteObj.byteVal") listConfig.getString("ofArray.2.2")
- Fixes lightbend#30 - Adds and modifies peek and find methods to allow indexing of ConfigList objects - Enables nested array and object access using indices in paths: listConfig.getString("ofObject.0.byteObj.byteVal") listConfig.getString("ofArray.2.2")
Is there a way to access an array element directly?
example:
As far as I understand the library I first have to access the array by calling config.getObjectList("arr") and then iterating over the list to access the fields of my objects.
I'd like to have the possibility of calling config.getString("arr[0].ip") or something like that...
Thanks.
The text was updated successfully, but these errors were encountered: