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

commentBefore property has no effect #128

Closed
rulatir opened this issue Oct 14, 2019 · 5 comments
Closed

commentBefore property has no effect #128

rulatir opened this issue Oct 14, 2019 · 5 comments
Labels
bug Something isn't working

Comments

@rulatir
Copy link

rulatir commented Oct 14, 2019

test.js:

const YAML = require('yaml');

const input =
"version: 1\n\
task_definition:\n\
  services:\n\
    some_service:\n\
      secrets:\n\
        - name: SOME_NAME\n\
          value_from: SOMEWHERE\n";

const parsed = YAML.parse(input);
const generatedSecretBindings = YAML.createNode([
    {
        name: "ANOTHER_NAME",
        value_from: "ANOTHER_SOURCE"
    }
])
generatedSecretBindings.commentBefore =
    "Don't edit these secret bindings directly. They are generated.";
parsed.task_definition.services.some_service.secrets = generatedSecretBindings;
console.log(YAML.stringify(parsed));

Actual output:

version: 1
task_definition:
  services:
    some_service:
      secrets:
        - name: ANOTHER_NAME
          value_from: ANOTHER_SOURCE

Expected output (more or less):

version: 1
task_definition:
  services:
    some_service:
      # Don't edit these secret bindings directly. They are generated.
      secrets:
        - name: ANOTHER_NAME
          value_from: ANOTHER_SOURCE

Additional information:
node --version: v12.11.1

    "yaml": {
      "version": "1.7.1",
      "resolved": "https://registry.npmjs.org/yaml/-/yaml-1.7.1.tgz",
      "integrity": "sha512-sR0mJ2C3LVBgMST+0zrrrsKSijbw64bfHmTt4nEXLZTZFyIAuUVARqA/LO5kaZav2OVQMaZ+5WRrZv7QjxG3uQ==",
      "requires": {
        "@babel/runtime": "^7.5.5"
      }
    }
@eemeli eemeli added the bug Something isn't working label Oct 14, 2019
@eemeli
Copy link
Owner

eemeli commented Oct 14, 2019

That usage pattern should work, but it clearly doesn't.

What's happening here is that YAML.stringify() is first creating a YAML document from its input, and then stringifying that. During the stringification, the parsed object is recursively worked into a tree of YAML nodes, and that process does not (currently) provide special handling for YAML nodes that already exist in the tree. But it probably should. I'll need to test a few things and work out the cleanest way of making that happen.

As is, here's how you could do what you're trying to do with the current API, with fewer layers of conversion taking place internally:

const doc = YAML.parseDocument(input)
const secrets = doc.getIn(['task_definition', 'services', 'some_service', 'secrets'])
secrets.commentBefore = " Don't edit these secret bindings directly. They are generated."
secrets.set(0, { name: 'ANOTHER_NAME', value_from: 'ANOTHER_SOURCE' })
String(doc)
version: 1
task_definition:
  services:
    some_service:
      secrets:
        # Don't edit these secret bindings directly. They are generated.
        - name: ANOTHER_NAME
          value_from: ANOTHER_SOURCE

@rulatir
Copy link
Author

rulatir commented Oct 14, 2019

Actually I want to completely replace the secrets node, so perhaps I could just createNode from an array and then:

const someService = doc.getIn(['task_definition','services','some_service']);
someService.set('secrets',secrets);

Will that work?

Anyways, the lack of special handling for YAML nodes already in the tree is precisely what I suspected.

@rulatir
Copy link
Author

rulatir commented Oct 14, 2019

Actually if there is no special handling for YAML nodes, then why doesn't output look like this:

version: 1
task_definition:
  services:
    some_service:
      secrets:
        items:
          - key: name
            value: ANOTHER_NAME
          - key: value_from
            value: ANOTHER_SOURCE
        commentBefore: "Don't edit these secret bindings directly. They are generated."

?

@eemeli
Copy link
Owner

eemeli commented Oct 14, 2019

Yeah, that should work. There's also this API (these are documented here):

doc.setIn(['task_definition','services','some_service','secrets'], secrets);

The fix for your original issue looks like it'll be a one-liner. The reason why you're not seeing a commentBefore node in the output is that when stringifying unknown objects we first look for a toJSON() method and call that if it's available. For YAML nodes, that's currently transforming them back into plain JS objects before they're re-parsed into nodes.

@eemeli eemeli closed this as completed in c029266 Oct 15, 2019
@eemeli
Copy link
Owner

eemeli commented Oct 15, 2019

Fixed in 1.7.2.

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

No branches or pull requests

2 participants