Skip to content
This repository has been archived by the owner on Dec 18, 2018. It is now read-only.

Add support for collections/arrays #125

Closed
tillig opened this issue Jan 12, 2015 · 6 comments
Closed

Add support for collections/arrays #125

tillig opened this issue Jan 12, 2015 · 6 comments

Comments

@tillig
Copy link

tillig commented Jan 12, 2015

Using Microsoft.Framework.ConfigurationModel 1.0.0.0-rc1-10693.

Trying to convert the Autofac configuration system over to use the new configuration mechanism. Current Autofac configuration allows for a collection of classes to be registered via config, sometimes with different parameter sets, like this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <modules>
    <module type="ParameterizedModule">
      <parameters>
        <parameter name="message" value="First" />
      </parameters>
    </module>
    <module type="ParameterizedModule">
      <parameters>
        <parameter name="message" value="Second" />
      </parameters>
    </module>
  </modules>
</configuration>

Notice the module type is the same but the parameters provided are different.

This sort of configuration doesn't appear to be possible with the new XML or JSON implementations of configuration.

If I try loading this into the XML provider, I get an exception that the module type is duplicated.

For JSON, I'm not sure how I'd represent this without arrays. I originally thought it'd be something like this...

{
  "modules": [
    {
      "type": "ParameterizedModule",
      "parameters": [{ "name": "message", "value": "First"}]
    },
    {
      "type": "ParameterizedModule",
      "parameters": [{ "name": "message", "value": "Second"}]
    }
  ]
}

But the JSON support doesn't support arrays (i.e., the Load method in JsonConfigurationSource throws if it encounters an array)... and, as mentioned, XML throws on duplicate elements.

I've not seen anywhere in the docs how to handle this and I've not found any roadmap, but this seems like an important use case to me. Am I missing something? Is there a better way to handle this situation?

@tillig
Copy link
Author

tillig commented Jan 25, 2015

Curious if anyone's thought about this at all. I see it overlaps with #115 so I'd be OK with closing this one and just following there. Either way, it'd be nice to know if this is planned or not so I know what to do with my consuming code.

@tillig
Copy link
Author

tillig commented Feb 10, 2015

I see #115 is in planning so I'll close this one.

@tillig tillig closed this as completed Feb 10, 2015
@xferra
Copy link

xferra commented Apr 7, 2016

Unfortunately there is no way to read arrays from XML (FormatException). Arrays are supported in JSON and INI configs.
Does anybody plan to add support for Arrays in XML?

@tillig
Copy link
Author

tillig commented Apr 7, 2016

You can do arrays in XML, you just have to give the XML element a name with the array index, like:

<root>
  <node name="0" value="first array element" />
  <node name="1" value="second array element" />
</root>

@xferra
Copy link

xferra commented Apr 7, 2016

@tillig Thanks for the answer, but what if I need to use "name" attribute for other purposes?

@tillig
Copy link
Author

tillig commented Apr 7, 2016

The name attribute is special. If you need array behavior, this how you do it. There's no "override" or configuration/customization for it.

The whole way arrays work is that the "name" of the element is the array index. Iterating through "arrays" in the configuration source can be done with a foreach, but if you look at each child you're iterating through, you'll see the "key" on the child property is actually the array index as a string. That's done so INI files can "fake" arrays ("ordinal collections"), so JSON array support works... and this is how XML support also works.

If that means you need to nest elements, do that.

<root>
  <node name="0">
    <value name="first" />
  </node>
  <node name="1">
    <value name="second" />
  </node>
</root>

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

No branches or pull requests

2 participants