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

Custom tags are printed on newline instead of after the key #399

Closed
avisprince opened this issue Jun 28, 2022 · 3 comments · Fixed by #425
Closed

Custom tags are printed on newline instead of after the key #399

avisprince opened this issue Jun 28, 2022 · 3 comments · Fixed by #425
Labels
enhancement New feature or request

Comments

@avisprince
Copy link

avisprince commented Jun 28, 2022

Describe the bug
I am creating custom tags that handles non scalar values. The way the yaml is formatted though is unexpected. See below:

Expected:

foo:
  bar: !append
    - hello
    - world

Actual

foo:
  bar:
    !append
    - hello
    - world

To Reproduce
Here is my code:

const APPEND_PATCH = {
  tag: '!append',  
  identify: value => value.tag === '!append',
  createNode: (_, item) => {
    const x = new yaml.YAMLSeq();
    x.tag = '!append';
    item.value.forEach(v => x.add(v));
    return x;
  },
}

class AppendPatch {
  tag = '!append';
  value;

  constructor(val) {
    this.value = val;
  }
}

const TEST = {
  foo: {
    bar: new AppendPatch(['hello', 'world'])
  }
}

const result = yaml.stringify(struct, {
    nullStr: '~',
    customTags: [APPEND_PATCH],
  });

console.log(result);
@avisprince avisprince added the bug Something isn't working label Jun 28, 2022
@eemeli
Copy link
Owner

eemeli commented Jun 30, 2022

A PR fixing this to the "expected" result would be most welcome.

@eemeli eemeli added enhancement New feature or request and removed bug Something isn't working labels Aug 1, 2022
@smith-xyz
Copy link

ahh yeah, noticed that too. Just to ask, is the new line actually a technical spec for yaml? Wondering what the convention is there, if any.

Without using custom tags at all:

import YAML from "yaml";

const yaml = `
--- 
:custom: !seq:Custom 
  - :cnt: 1
  - :cnt: 2
`;

const result = YAML.parseDocument(yaml);

console.log(result.toJS());
// { ':custom': [ { ':cnt': 1 }, { ':cnt': 2 } ] }

console.log(result.warnings);
// Unresolved tag: !seq:Custom at line 3, column 10

console.log(result.toString()); 
// ---
// :custom:
//   !seq:Custom
//   - :cnt: 1
//   - :cnt: 2

@eemeli
Copy link
Owner

eemeli commented Aug 3, 2022

Just to ask, is the new line actually a technical spec for yaml? Wondering what the convention is there, if any.

Could you rephrase this question? In general, newlines may be significant in YAML, but specifically the value of a mapping pair may extend from the same line as the : to subsequent lines, as long as the content on those lines is more indented than the parent.

There's no official convention for how a value like this ought to be serialised, though, so it's really whatever feels most appropriate for each implementation.

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

Successfully merging a pull request may close this issue.

3 participants