decorators-squad / eo-yaml

YAML for Java 8 and above. A user-friendly OOP library. Previously known as "Camel".
BSD 3-Clause "New" or "Revised" License
261 stars 52 forks source link

reading yaml node without --- or ... #623

Closed DeathGOD7 closed 5 months ago

DeathGOD7 commented 5 months ago
A:
  B: // FYI this is generic and user defined
    booleanss: 'true'
    yamlseq: []
    name: 'Test'

Assume I have loaded the YAML file with YamlMapping ymap= Yaml.createYamlInput(configFile).readYamlMapping(); And if I did, YamlMapping nodeA= ymap.yamlMapping("A"); it gives the yaml mapping of the A node.

I don't know if it is the correct way or not but I loop through the nodes with :

for (YamlNode key : nodeA.keys()) {
   .......
   YamlMapping temp = nodeA.yamlMapping(key);
}

How to get the child nodes of A (eg : B) without any --- or ... (just as simple string value)? Cause when I do key.toString() it gives the string with those --- (start) and ... (end).

Sorry if it's a simple or stupid problem. I recently stumbled upon this lib and was testing it out but couldn't figure it out.

zoeself commented 5 months ago

@DeathGOD7 thank you for reporting this. I'll assign someone to take care of it soon.

zoeself commented 5 months ago

@amihaiemil I couldn't find any assignee for this task. This is either because there are no contributors with role DEV available or because the project does not have enough funds.

Please, make sure there is at least one available contributor with the required role and the project can afford to pay them.

amihaiemil commented 5 months ago

@DeathGOD7 the toString() methods of YamlNode instances are overriden to print nodes as YAML. They are not intended to get their internal values, even if they are simple scalars.

To get the string scalar values of a mapping you use the mapping.string(key) method.

To get the string keys, you iterate over them, as you are already doing, but you are receiving instances of YamlNode, because keys in a mapping can also be complex nodes. Turn them to scalars and get the string value from them: YamlNode.asScalar().value() will get you what you are looking for.

DeathGOD7 commented 5 months ago

Thank you for the quick response. And also when creating a YAML file with a project comment, does it print the --- as shown in wiki?

Cause the yml file I used have the top comment for the first node key instead of the yaml mapping created from file (as stated in the wiki). Just wanted to know if the project lvl comment also prints the start (---) lines.

amihaiemil commented 5 months ago

@DeathGOD7 yes, I am pretty sure it also prints the ---.

In this case, it is necessary, because with comments there always comes the question "to which node does this comment belong/refer?". So, if the first thing in the file is a comment, we assume it refers/belongs to the whole document, not just the first key-value pair. So we have to know/decide always to which node a comment belongs/refers, that is the main problem :)

DeathGOD7 commented 5 months ago

Does it affect the yml file or when loading? Cause yaml file nags with extra one space and indent.

amihaiemil commented 5 months ago

@DeathGOD7 no, it's valid syntax. --- just marks the beginning of a YAML document, especially because you can also have multiple YAML documents in the same file (denoted with start --- and end ...).

It is optional, that's why you do not see it often, but this is perfectly valid:

# some comment
---
a: b
c: d
e: f
amihaiemil commented 5 months ago

I'll close this if all the questions are answered - feel free to open other issues if you encounter more problems.