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

Should be possible to enumerate the parsed attributes in a given Section #57

Open
csabatuz-chess opened this issue Nov 6, 2023 · 2 comments
Labels
enhancement New feature or request

Comments

@csabatuz-chess
Copy link
Contributor

csabatuz-chess commented Nov 6, 2023

Assuming

import (
  parser "github.com/haproxytech/config-parser/v5"
)
...
  p := parser.New(content, options)

Proposal
It would be a useful feature, to be able to list all the set attributes in a given section
E.g.

attributes, err := p.AttributesGet(parser.Global, parser.GlobalSectionName) // ([]string, error)

This would allow a piece of code to iterate over the existing attributes.
Currently it's not even possible to list the registered attributes for a given section type, so if some code wanted to enumerate all the attributes e.g. in a loop, it'd need to maintain a list of attributes, while this information is readily available for the parser (both the possible (via ConfiguredParsers), and the actually set attributes (via Parsers))

(Happily) wiling to contribute code if this feature makes sense.

@oktalz
Copy link
Member

oktalz commented Nov 10, 2023

hi @csabatuz-chess,

we have no objections to adding this to the package
there are some hints how this could be achieved in writer function where we go over all sections and attributes

(Happily) wiling to contribute code if this feature makes sense.

we can add it to the list, but if you are willing to contribute, that is most welcomed!

@csabatuz-chess
Copy link
Contributor Author

csabatuz-chess commented Nov 21, 2023

Hi @oktalz.
I created a PR. I tried to not complicate things and keep it simple, might be too simple.

The change is not affecting generated code and (i think) it's not disturbing any parts of the existing interface or behaviour.

Possible discussion points

onlyExisting
I added a flag to the new method, to allow the caller to decide if they are interested in all the possible attributes in that section, or only the ones that are existing in the sense that they are set so will cause lines in the final result.
I see value in both, but I think listing the existing is more important (to our use-case deinitely).
So I can remove the existingOnly flag if you think it clutters the interface.

extra.Section
The extra.Section parsers gave me a bit of a thinking, i'm unsure if there's anything in there that's relevant for attribute listing, but in the end I concluded that they are something special, that are as the name say extra, and strictly not part of the block's definition, not directly associated with lines in the serialised config, they appear more to be information about related blocks.

E.g. the default_backend line seems to cause the backend extra section to contain data about the referenced backend block.

So in the PR I'm just ignoring those.

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

2 participants