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

Can we get node key name from itself? #2

Open
takaaptech opened this issue Jul 3, 2019 · 3 comments
Open

Can we get node key name from itself? #2

takaaptech opened this issue Jul 3, 2019 · 3 comments
Labels
enhancement New feature or request

Comments

@takaaptech
Copy link

takaaptech commented Jul 3, 2019

Hi @dezhidki ,
Thank so much for this TOML parser library! It is awesome!
For now, each time working with TomlNode, I need to get its key name.
For example:

title="TOML file"
[dataconfig]
     name="abc"

I can get TomlTable by:

var table_node = config["dataconfig"]
### Now how can i get "dataconfig" key name from table_node?
### We can get it from parent of couse but i think it is better to have that key name inside node itself.

Thanks!

@akluth
Copy link

akluth commented Oct 15, 2019

You can either use config.Keys() which returns an IEnumerable<string> where you can iterate over and thus get the key names or search for a particular key by using config.HasKey("dataconfig").

@takaaptech
Copy link
Author

takaaptech commented Oct 16, 2019

Hi @alexclooze
Yes, I can get the node name that way, and call some Function that needs the node name like this:
ProcessNode (string nodeName, TomlTable node )

But I think we can have nodeName inside the node instance so that we only need to call:
ProcessNode (TomlTable node )

For example, I use Tommy in my code generation engine so I need to generate class from a node, which class name is the node name. In the runtime of the generation process, it is hard to tell exactly what is the node name is. In some other language like LUA, I can assign nodeName to node like this:
node.nodeName = name
And then call ProcessNode (TomlTable node )
But in C#, It is better to assign it from Tommy
Thank so much!

@dale-roberts
Copy link

dale-roberts commented Oct 20, 2020

I know this is an old thread... First, thanks to @akluth for the great library! It is simple, straightforward, well commented, and easy to use. VERY nice!

In case others look here, I wanted to post an easy solution that works with the library as is. Like @takaaptech I wanted to iterate over a Table, and get the Key and Value together. The quick way I came up with is to just iterate over the underlying Dictionary, which is held in the public RawTable node property for Table type nodes.

// Iterating over the RawTable Dictionary yields a KeyValuePair<string, TomlNode>.
// You can access the name of the node as .Key, and the value (the node itself) as .Value.
foreach (var ITEM in TBL.RawTable)
   Console.WriteLine($"Key: {ITEM.Key},  Value: {ITEM.Value}");

You could pass around this KeyValuePair<> ITEM for further processing. I think the reason the "name" is not part of the node itself is because only nodes that are inside a Table/Dictionary have keys (names), and those "names" are stored only as keys in the parent/container dictionary. Items in an array, for instance, have no names, so having a .Name as part of every node might not make sense.

Thanks again!

@dezhidki dezhidki added the enhancement New feature or request label Nov 29, 2020
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

4 participants