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

Refactor tag resolve() API #201

Merged
merged 7 commits into from Oct 5, 2020
Merged

Refactor tag resolve() API #201

merged 7 commits into from Oct 5, 2020

Conversation

eemeli
Copy link
Owner

@eemeli eemeli commented Oct 3, 2020

This is a breaking change for custom tags. The main thrust is to remove any link between the underlying CST implementation and tag implementations. That should make custom tags easier to write and understand (in particular for collections), and will allow for the CST implementation to be changed without further tag API changes.

Previously, custom tags used this sort of interface when parsing values:

import { YAMLSemanticError } from 'yaml/util'

function resolve(doc, node) {
  try {
    return magic(node.strValue)
  } catch (error) {
    doc.errors.push(new YAMLSemanticError(node, error.message))
    return null
  }
}

The new API looks like this:

function resolve(value, onError) {
  try {
    return magic(value)
  } catch (error) {
    onError(error.message)
    return null
  }
}

Here, value is either a string, YAMLMap or YAMLSeq, depending on the shape of its value. A tag should verify that it's getting an instance of its expected type. onError is a callback that takes the error message as its single argument and internally constructs a YAMLSemanticError from it.

Because mappings and sequences are now always initially resolved into their corresponding AST structures, the parseMap and parseSeq exports of 'yaml/util' are also dropped.

@eemeli eemeli added this to the yaml 2 milestone Oct 3, 2020
BREAKING CHANGE: All tag resolvers are now called with two arguments:
- value: string | YAMLMap | YAMLSeq
- onError(message: string): void
with the value being determined solely by the node's shape, rather than
any explicit tag it may have.
BREAKING CHANGE: With the change in the tag resolver API, these
functions are no longer useful when writing custom tags.
@eemeli eemeli merged commit 31d046a into master Oct 5, 2020
@eemeli eemeli deleted the re-resolve branch October 5, 2020 09:10
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

Successfully merging this pull request may close these issues.

None yet

1 participant