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

add_multi_constructor equivalent / unknown tag handler #385

Closed
akdor1154 opened this issue Dec 6, 2017 · 3 comments
Closed

add_multi_constructor equivalent / unknown tag handler #385

akdor1154 opened this issue Dec 6, 2017 · 3 comments
Labels

Comments

@akdor1154
Copy link

akdor1154 commented Dec 6, 2017

In PyYAML, I can use the add_multi_constructor feature to allow me to define a constructor for multiple tags. I am using it to handle any unknown tags. For example, I could use this to deserialize the document

thing1: !someTag asdf
thing2: !someOtherTag fdsa

into the JS structure

{
  thing1: UnknownTag { tag: 'someTag', value: 'asdf' },
  thing2: UnknownTag { tag: 'someOtherTag', value: 'fdsa' }
}

and be able to re-serialize that structure back into equivalent yaml.

My use case is to read and modify a tiny part of a big yaml file that has a bunch of domain-specific tags in it. I don't care how the tags are resolved as I'm not reading that part of the file, but currently with js-yaml I can't read it at all because to parse the file in the first place results in an unknown tag error. Even if I set it to skip unknown tags, I can't re-serialize the modified docjument.

This is fine with PyYAML as I can use add_multi_constructor as defined above to handle any unknown tags, but as far as I can tell this behvaiour is impossible to achieve with js-yaml.

@Thom1729
Copy link

This would be extremely useful to me. I'm processing AWS CloudFormation templates with js-yaml. CloudFormation uses a number of tags as macros, e.g. !Ref. These tags need to roundtrip when I load and dump the file.

Currently, in order to do that, I have to hard-code a list of tags used by AWS and define a type for each tag, for each kind of node (because some AWS tags are used for more than one kind of node). This is a pain, it will break if AWS starts supporting additional tags, and it makes otherwise-simple processing code harder to understand. This feature request would drastically simplify this task.

@tomodutch
Copy link

I'm interested in this feature as well. Currently working on deserializing various yaml scripts we get from customers. They're predictable but we could encounter an unknown tag

@rlidwka
Copy link
Member

rlidwka commented Dec 25, 2020

Added in 73ef02c (currently in dev branch).

See an example how to do it here:
https://github.com/nodeca/js-yaml/blob/a0d0caa5aa0f5354fefa9c637cfb7c4c17ef7d02/examples/handle_unknown_types.js

@rlidwka rlidwka closed this as completed Dec 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants