8000 Support for TOML arrays · Issue #1 · creachadair/tomledit · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Support for TOML arrays #1

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

Open
GFSimone opened this issue Aug 7, 2024 · 3 comments
Open

Support for TOML arrays #1

GFSimone opened this issue Aug 7, 2024 · 3 comments
Labels
enhancement New feature or request

Comments

@GFSimone
Copy link
GFSimone commented Aug 7, 2024

As suggested by Cosmos Devs, I'm bringing here a feature request opened for Cosmos Confix.
cosmos/cosmos-sdk#21034

It would be nice if tomledit could parse arrays blocks.

Thanks in advance for any attention to this.

@GFSimone GFSimone changed the title Missing support for TOML arrays Support for TOML arrays Aug 7, 2024
@creachadair
Copy link
Owner

Can you say more specifically what feature you're requesting? The parser handles array-valued blocks already, and it's not clear to me whether this is about the CLI tool or the library, and what kind of API you'd like to see.

It would be helpful if you can give a couple specific examples of what you want to do (ideally with a code snippet, even if that snippet doesn't work with the current API).

@creachadair creachadair added enhancement New feature or request question Further information is requested labels Aug 7, 2024
@julienrbrt
Copy link
julienrbrt commented Aug 7, 2024

Currently, doc.Find needs a key to be present to find a value.

Say you have the following toml:

[[fruit]]
  name = "apple"

  [[fruit.variety]]
    name = "red delicious"

  [[fruit.variety]]
    name = "granny smith"

tomledit.Document{}.Find("fruit","variety") will return a slice of Items.
It would be great if it could range over the array if we specify an index as key: eg.g tomledit.Document{}.Find("fruit","variety","1"). Then it allows us to access the value of that item with tomledit.Document{}.Find("fruit","variety","1","name").

Right now, you don't have an easy way (afaict) to access the toml array values à la jq by simply passing up keys.

@creachadair
Copy link
Owner
creachadair commented Aug 8, 2024

There are a few tricky things to sort out to make a JSONpath-style API work for TOML. Looking just at this example, the TOML is equivalent to the JSON:

{
  "fruit": [
    {
      "name": "apple",
      "variety": [
        {
          "name": "red delicious"
        },
        {
          "name": "granny smith"
        }
      ]
    }
  ]
}

So a JSONpath (or similar) expression like fruit.variety[1] doesn't match anything in the document. (Maybe you meant [fruit] instead of [[fruit]]).

Anyway, taking it as written, you'd want fruit[0].variety[1].name or similar. To do this uniquely, you'll either need to preprocess the syntactic structure of the TOML to combine array elements, or track the current stack on the way through the file.

For example, this TOML:

[a]
  [[a.b]]
    id = 1
  [[a.b]]
    id = 2

[c]

[[a.b]]
  id = 3

is valid, and represents the JSON

{
  "a": {
    "b": [
      {"id": 1},
      {"id": 2},
      {"id": 3}
    ]
  },
  "c": {}
}

To resolve a.b[1].id requires keeping at least a count of the number of elements of (each) array in the traversal. This applies recursively, since arrays and array indices can be nested (cf. fruit[0].variety[1].name above). Plus, in a path API, both the inputs and the outputs are polymorphic—since a.b yields an array of items, vs. a.b[1].id which yields a single value.

All that is of course quite achievable, but it's not a straightforward extension of the existing API—this would need a new API and probably some new types. It's not something I have a lot of time to invest in here—it might even want to be a separate package maybe.

@creachadair creachadair removed the question Further information is requested label Aug 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants
0