简体   繁体   English

如何在JavaScript中存储已解析的YAML文件的行号?

[英]How can I store line numbers for a parsed YAML file in JavaScript?

I'm currently writing a Node.js application that parses a YAML file. 我正在编写一个解析YAML文件的Node.js应用程序。 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". 例如,如果YAML是person: username: jsmith password: hunter2我可能想显示消息“第3行错误:密码错误”。

Obviously, this is a trivialized example, but the point is there's nothing wrong with the YAML itself. 显然,这是一个微不足道的例子,但重点是YAML本身没有任何问题。

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". 我一直在使用yamljs,并且能够通过Object.defineProperty方法将行号直接添加到对象,但是这对字符串不起作用,所以如果我们有以下YAML passwords: - aG00dP@ssw0Rd - hunter2我不会不能为“hunter2”提供一个行号。

Some of my thoughts: 我的一些想法:

  • Box all values in a { lineNumber, value } object. { lineNumber, value }对象中输入所有值。 This seems clunky to me, as I would have to call .value on everything. 这对我来说似乎很笨拙,因为我必须在所有事情上调用.value I would really prefer not to have the line number impact any downstream code that is acting on the object. 我真的不希望行号影响任何作用于该对象的下游代码。
  • Store a hash of the values with their line numbers. 存储值的哈希值及其行号。 This wouldn't work, because as far as I know I wouldn't be able to tell the difference between duplicate strings (for example, if "hunter2" was included further on in the document) 这不起作用,因为据我所知,我无法区分重复的字符串(例如,如果文档中还包含“hunter2”)
  • Extend String to store the line number, but still act like a string. 扩展字符串以存储行号,但仍然像字符串一样。 This seems ideal, but I don't know if it's entirely possible. 这看起来很理想,但我不知道它是否完全可能。 I've tried my hand at it and none of the approaches have worked for me yet. 我已经尝试过了,但这些方法对我来说都没有用。

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? 我提到的任何方法是否真的可行,而我一直做错了?

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 我在这里分叉了yamljs存储库: 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. 编辑 :我遇到了另一个问题,提到向String添加属性,并意识到这是我的问题的解决方案。 I modified the repository above to return String objects, as opposed to primitives, and that provided the functionality I needed. 我修改了上面的存储库以返回String对象,而不是原语,并提供了我需要的功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM