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

Allow for pathlike aliases #380

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Allow for pathlike aliases #380

wants to merge 2 commits into from

Conversation

eemeli
Copy link
Owner

@eemeli eemeli commented Apr 18, 2022

This enables the resolution of YAML looking like this:

- &foo 
  bar:
    - 1
    - 2
    - 42
- *foo/bar/2

to resolve as:

[
  { bar: [1, 2, 42] },
  42
]

In other words, if an alias contains a / character, it is resolved as follows:

  1. If a preceding anchor matches the alias exactly, use that and resolve the value as previously.
  2. Else, find the last longest preceding anchor which matches the start of the alias if it were followed by a / character.
  3. Considering the node of that anchor as a root node, use the rest of the alias as a / separated path to the target node.
  4. Provided that the target node is a scalar or a collection, resolve it as the alias's value.

Put together, this means that all existing valid YAML continues to have the exact same meaning as before, but it's possible for an alias to point at nodes within a structure with an anchor. As with anchors, JS equality is maintained across aliased values, so e.g. this works:

import { parse } from 'yaml'

const src = `
  - &foo { bar: [1, 2, 42] }
  - { alias: *foo/bar }
`
const res = parse(src, { version: 'next' })

Array.isArray(res[0].bar) // true
res[0].bar === res[1].alias // true

In the implementation, this currently requires tracking some additional nodes during the native value composition, but a later optimisation should be able to reduce the impact of that.

This feature is not yet in the YAML spec, and so requires using version: 'next' as an option. Its implementation is also not guaranteed to follow semver.

@eemeli eemeli added the next Issues & PRs on future YAML features label Apr 18, 2022
@ioggstream
Copy link

Even more difficult: 2 might indicate either an integer key or the third array item

- &baz
  baz:
    2: an-integer
    "2": a-string
   ? [1, 2]
   : a-list
   ? [1, "2"]
   : another-list
- *baz/2     # an-integer
- *baz/"2"   # a-string
- *baz/[1,2]  # a-list
- *baz/[1,"2"] # another-list

iiuc libfyaml uses " do discriminate between integers and strings.

Maybe there are other corner cases ..

Copy link

@Leena8686 Leena8686 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merge 2 commits into main from next.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
next Issues & PRs on future YAML features
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants