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

Breaking changes to Document & createNode APIs #186

Merged
merged 5 commits into from Aug 23, 2020
Merged

Conversation

eemeli
Copy link
Owner

@eemeli eemeli commented Aug 5, 2020

This PR combines a few closely related changes, and is intended to streamline how new documents and nodes are created.

new YAML.Document(value, options?)

The Document constructor gets a new first argument value. This allows for much more ergonomic document creation:

// before
const doc = new YAML.Document()
doc.setSchema()
doc.contents = doc.schema.createNode({ some: 'value' })

// after
const doc = new YAML.Document({ some: 'value' })

The doc.schema is now also left uninitialised only if value === undefined, and is automatically set if createNode() or createPair() are called.

doc.createNode(value, options?)

The methods YAML.createNode() and doc.schema.createNode() are replaced by a single doc.createNode(). Their non-value arguments are also collected into an options object:

// before
const doc = YAML.createDocument({ customTags: ['omap'] })
doc.setSchema()
const seq = YAML.createNode([1, 2, 3])
const omap = doc.schema.createNode([['foo', 13], ['bar', 42]], true, '!!omap')

// after
const doc = YAML.createDocument(null, { customTags: ['omap'] })
const seq = doc.createNode([1, 2, 3])
const omap = doc.createNode([['foo', 13], ['bar', 42]], { tag: '!!omap' })

Unlike its precedents, doc.createNode() is able to support circular references, which were previously only supported by YAML.stringify().

doc.createPair(key, value, options?)

The method doc.schema.createPair() is replaced by doc.createPair(), and also uses an options bag instead of named positional arguments.

// before
const doc = YAML.createDocument()
doc.setSchema()
const pair = doc.schema.createPair('key', 'value')

// after
const doc = YAML.createDocument()
const pair = doc.createPair('key', 'value')

Internally, doc.createPair() is just sugar around two separate doc.createNode() calls and the Pair constructor.

@eemeli eemeli added this to the yaml 2 milestone Aug 5, 2020
Previously, circular refs only worked in YAML.stringify. This adds a
second arg to the Schema constructor, as createNode needs a ref for the
anchors object.

BREAKING CHANGE: If you previously used
    doc.schema.createPair(key, value, { wrapScalars: true })
then you'll need to replace that with
    doc.schema.createPair(key, value, true)
BREAKING CHANGE: `options` is now the second rather than first argument
for `new YAML.Document(value, options)`. If you previously had:
    const doc = new YAML.Document({ version: '1.1' })
    doc.setSchema()
    doc.contents = doc.schema.createNode({ some: 'value' })
you should replace that with:
    const doc = new YAML.Document({ some: 'value' }, { version: '1.1' })

Calling the constructor with no arguments has the exact same behaviour
as previously. Calling it with a defined value will initialise its
`contents` with that value, as well as initialising the `schema`.
BREAKING CHANGE: The createNode & createPair methods are moved from the
Schema instance to the Document instance, and their options arguments
are now collected into an object. So where you may have previously had:
    doc.schema.createNode(value, false, '!!omap')
you should now use:
    doc.createNode(value, { tag: '!!omap', wrapScalars: false })
and similarly for createPair(key, value).
BREAKING CHANGE: Recursively creating nodes now requires a document
instance.
@eemeli
Copy link
Owner Author

eemeli commented Aug 5, 2020

Rebase to account for changes to master.

@eemeli eemeli merged commit 8c997f1 into master Aug 23, 2020
@eemeli eemeli deleted the break-doc-api branch August 23, 2020 08:12
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