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 support for "value associated with the merge key is a sequence" #320

Closed
jjkavalam opened this issue Oct 17, 2021 · 3 comments · Fixed by #321
Closed

Add support for "value associated with the merge key is a sequence" #320

jjkavalam opened this issue Oct 17, 2021 · 3 comments · Fixed by #321
Labels
bug Something isn't working

Comments

@jjkavalam
Copy link
Contributor

Describe the bug

If the value associated with the merge key is a sequence (rather than a single mapping node), following error is thrown when attempting doc.toJS() doc.toJSON():

Merge sources must be maps or map aliases

To Reproduce
Steps to reproduce the behaviour.

const YAML = require("yaml");

const sourceError = `
- citrus: &citrus
  - { lemon: yellow }
  - { tangerine: orange }

- fruit-colors:
    <<: *citrus
`;

const sourceNoError = `
- citrus: &citrus
    lemon: yellow
    tangerine: orange

- fruit-colors:
    <<: *citrus
`;

function parseAndConvert(source) {
  console.log("parseAndCovert: >> %s <<", JSON.stringify(source));
  const doc = YAML.parseDocument(source, { merge: true });
  console.log(doc.toString());
  console.log(doc.toJS());
}

parseAndConvert(sourceNoError);
parseAndConvert(sourceError);
parseAndCovert: >> "\n- citrus: &citrus\n    lemon: yellow\n    tangerine: orange\n\n- fruit-colors:\n    <<: *citrus\n" <<
- citrus:
    &citrus
    lemon: yellow
    tangerine: orange

- fruit-colors:
    <<: *citrus

[
  { citrus: { lemon: 'yellow', tangerine: 'orange' } },
  { 'fruit-colors': { lemon: 'yellow', tangerine: 'orange' } }
]
parseAndCovert: >> "\n- citrus: &citrus\n  - { lemon: yellow }\n  - { tangerine: orange }\n\n- fruit-colors:\n    <<: *citrus\n" <<
- citrus:
    &citrus
    - { lemon: yellow }
    - { tangerine: orange }

- fruit-colors:
    <<: *citrus

node_modules/yaml/dist/nodes/addPairToJSMap.js:59
        throw new Error('Merge sources must be maps or map aliases');
        ^

Error: Merge sources must be maps or map aliases
    at mergeToJSMap 

Expected behaviour

When the value associated with the merge key is a sequence, the elements of the sequence must be merged in one by one.

The expected behaviour is described here: https://github.com/eemeli/yaml/blob/master/src/nodes/addPairToJSMap.ts#L54

Versions (please complete the following information):

  • Environment: Node.js v14.14.0
  • yaml: 1.10.0, 2.0.0-8
@jjkavalam jjkavalam added the bug Something isn't working label Oct 17, 2021
jjkavalam added a commit to jjkavalam/yaml that referenced this issue Oct 17, 2021
@jjkavalam jjkavalam mentioned this issue Oct 17, 2021
jjkavalam added a commit to jjkavalam/yaml that referenced this issue Oct 19, 2021
eemeli pushed a commit that referenced this issue Oct 19, 2021
Nathan-Fenner pushed a commit to Nathan-Fenner/yaml that referenced this issue Nov 17, 2021
@sourcevault
Copy link

what is the solution ?

js = yaml.parse(doc.toString(),{merge:true})

js1 = doc.toJS {merge:true}

I am having to use doc.toString(), what is the direct way to do on doc ?

@eemeli
Copy link
Owner

eemeli commented Jun 23, 2022

@sourcevault Please open a new discussion for your query, including a complete example of what you're looking to do.

@sourcevault
Copy link

Hi @eemeli

I found the solution.

doc.setSchema('1.1')
js = doc.toJS()

not sure if this is the expected pattern or workaround.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants