Reputation: 2385
I'm currently writing a Node.js application that parses a YAML file. Under some situations, I need to be able to retrieve the line number corresponding to a value. For instance, if the YAML was
person:
username: jsmith
password: hunter2
I might want to display the message "Error on line 3: Bad password".
Obviously, this is a trivialized example, but the point is there's nothing wrong with the YAML itself.
I've been using yamljs and was able to add the line number directly to the objects via the Object.defineProperty
method, but this does not work for strings, so if we had the following YAML
passwords:
- aG00dP@ssw0Rd
- hunter2
I wouldn't be able to give a line number for "hunter2".
Some of my thoughts:
{ lineNumber, value }
object. This seems clunky to me, as I would have to call .value
on everything. I would really prefer not to have the line number impact any downstream code that is acting on the object.Is there another way I haven't thought of? Are any of the approaches I mentioned actually feasible, and I've just been doing it wrong?
Upvotes: 3
Views: 1768
Reputation: 2385
For anyone interested, I ended up storing the line number on arrays/objects and foregoing it on strings. It's not ideal, but the costs of trying to solve it in the other ways outweighed the benefits. Plus, my use case will probably see more arrays/objects anyway.
I forked the yamljs repository here: https://github.com/pumlhorse/yaml.js
Edit: I happened upon another question that mentioned adding properties to String
and realized that that was a solution to my problems. I modified the repository above to return String objects, as opposed to primitives, and that provided the functionality I needed.
Upvotes: 1