Skip to content
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

Support for indexed keys #48

Open
vaibhavk81 opened this issue Sep 3, 2013 · 3 comments
Open

Support for indexed keys #48

vaibhavk81 opened this issue Sep 3, 2013 · 3 comments

Comments

@vaibhavk81
Copy link

I would like to request an option to include duplicate keys or keys with index. The config should return list/array for all the values for this key.

e.g.
server=Sometext
server=Othertext

OR
server.0=Sometext
server.1=Othertext

@key("server")
String[] server;

Thanks,

@lviggiano
Copy link
Collaborator

Duplicate keys are not supported by properties files. This means that they would be invalid Java properties files.

Multiline values are supported by properties files out of the box. Example:

you can do something like this:

config.prop=Sometext,\
                  OtherText,\
                  ...

The above will work just fine as:

@Key("config.prop");
public String configProp();     

Another option is to use XML. It will be released in the next version. See XML Support :

<config>
   <prop>
      Sometext
      OtherText
      ...
   </prop>
</config>

Then, for the above xml you can define the newline as separator, or define a SeparatorClass.

@Separator("\n")  //newline
@Key("config.prop");
public String configProp();     

Indexed properties are an option to map legacy configuration files, that are using this notation. Which is quite wrong from my point of view, since properties files do support multiline values out of the box, plus owner adds support for
collections, so the best way to implement this is the first example in this comment, unless there is some good reason I am missing.
Adding indexed properties adds some overhead on properties lookup since we need then to check for multivalues every time a property is requested, then since they are indexed we also need to sort them and return in the proper order, plus... what would be the correct behavior if one index is missing? example:

server.0=...
server.1=...   
server.3=...              # server.2 is missing!
server.4=...

So, I will consider this feature for future development if I get some good understanding what problem the indexed properties are solving that isn't already solved by multiline properties values (supported by default in Java properties files) and owner collections support.

@Ronmck
Copy link

Ronmck commented Oct 26, 2013

Do you support this type of XML structure, I want get elements by value.

env.name this the key for each of my servers.

<servers>
    <env>
        <name>DEV</name>
        <hostname>locahost1</hostname>
        <port>60000</port>
        <user>myuser1</user>
        <password>mypass1</password>
    </env>
    <env>
        <name>SIT</name>
        <hostname>locahost2</hostname>
        <port>60010</port>
        <user>myuser2</user>
        <password>mypass2</password>
    </env>
    <env>
        <name>UAT</name>
        <hostname>locahost3</hostname>
        <port>60020</port>
        <user>myuser3</user>
        <password>mypass3</password>
    </env>
    <env>
        <name>SVP</name>
        <hostname>locahost4</hostname>
        <port>60030</port>
        <user>myuser4</user>
        <password>mypass4</password>
    </env>
</servers>

@lviggiano
Copy link
Collaborator

@Ronmck: No, your xml can't be supported at the moment. You can change it a follows:

<servers>
    <dev>
        <name>DEV</name>
        <hostname>locahost1</hostname>
        <port>60000</port>
        <user>myuser1</user>
        <password>mypass1</password>
    </dev>
    <sit>
        <name>SIT</name>
        <hostname>locahost2</hostname>
        <port>60010</port>
        <user>myuser2</user>
        <password>mypass2</password>
    </sit>
    <uat>
        <name>UAT</name>
        <hostname>locahost3</hostname>
        <port>60020</port>
        <user>myuser3</user>
        <password>mypass3</password>
    </uat>
    <svp>
        <name>SVP</name>
        <hostname>locahost4</hostname>
        <port>60030</port>
        <user>myuser4</user>
        <password>mypass4</password>
    </svp>
</servers>

This one should work fine.

I want to modify OWNER to support something like this:

interface MyConfig extends Config {
    @Key("servers.${env}.name");
    String name();

    @Key("servers.${env}.hostname");
    String hostname();

    @Key("servers.${env}.port");
    Integer port();
}

ConfigFactory.setProperty("env", "dev"); 
MyConfig cfg = ConfigFactory.create(MyConfig.class);

This way you can select ${env} in the ConfigFactory (or from the system properties) and have the above interface map to the appropriate section.

This is not working at the moment, but it is easy to implement and it will be available in the next release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants