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

customTags utilizing regex/pattern matching #996

Open
matt1484 opened this issue Nov 6, 2024 · 0 comments
Open

customTags utilizing regex/pattern matching #996

matt1484 opened this issue Nov 6, 2024 · 0 comments

Comments

@matt1484
Copy link

matt1484 commented Nov 6, 2024

Is your enhancement related to a problem? Please describe.

Currently there is no way to provide support for matching tags based on a prefix or regex pattern.
A trivial example being straight from pyyaml:

x: !!python/object:__main__.Hero
  name: Welthyr Syxgon
  hp: 1200
  sp: 0

This will make VSCode always complain about the !!python/object tag simply because its not an exact match. I have tried several values for yaml.customTags including:

"tag:yaml.org,2002:python/object"
"!!python/object"
"tag:yaml.org,2002:python/object*"
"!!python/object*"
"tag:yaml.org,2002:python/object.*"
"!!python/object.*"

but none work due to the CommonTagImpl not utilizing the test field that ScalarTag provides in the yaml module used by this module. Unfortunately CollectionTag doesnt support test, but if we had a custom Composer class that overrides the next generator to look at the token that is passed in and add the value of the tag to our options.customTags if it matches then that would be sufficient I think. A crude and incomplete example being:

class CustomComposer extends Composer {
  regexTags: RegexTag[]
  schema: Schema // yaml.Schema, should be the same schema used in the private option options.schema
  *next(token: Token) {
    // check token tag and if it matches our internal list of regexTags
    // if it matches add it to schema.tags
    super.next(token);
  }
}

Describe the solution you would like

Some way to allow a user to specify that a custom tag is using a prefix/regex. I have looked briefly at the code and it seems that most cases just do simple tag parsing like:

const typeInfo = tag.split(' ');
const tagName = typeInfo[0];
const tagType = (typeInfo[1] && typeInfo[1].toLowerCase()) || 'scalar';

which given the use of .split(' ') could mean that there could be code that checks for options after [1] and does some additional parsing for it. A simple example being:

// assuming a tag of "!!python/object:.* mapping regex"
const typeInfo = tag.split(' ');
const tagName = typeInfo[0];
const tagType = (typeInfo[1] && typeInfo[1].toLowerCase()) || 'scalar';
const options = typeInfo.slice(2);
if (options.includes("regex")) {
  // some code to handle regex tags here
}

Im not saying this is 100% the best solution, but it is backwards compatible with how custom tags have been working. Some alternatives might involve changing the structure of yaml.customTags entirely to accept mappings or similar which is also valid, albeit more work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant