ruby / ruby/psych

Psych::Nodes::Scalar::LITERAL ignored in encode_with?

Open
#673 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Ruby
Stars
597
Forks
223
Avg merge
11h 23m
Merged PRs (30d)
3

Description

This could just me, but I really lost and I don't understand why this works this way.

I have a big YAML with various string values. One of them is an encoded file content which is nice for machine consumption but I want to show that as a literal for human beings.

I created a separate class for those values and I added an encode_with method.

                def encode_with( coder )
                        coder.scalar = @str                                                                                                                                 
                        coder.tag = "!file_content"
                        coder.style = Psych::Nodes::Scalar::LITERAL
                end

When the value is short then it works as expected but if it is longer then output is still quoted.

---
hello: world
short: !file_content |
  TEST_DOCKER_IMAGE="foobar/image:1.2.3-r4"
long: !file_content "!\n! %[CONN_NAME]\n!\nneighbor %[REMOTE_ADDRESS] remote-as %[REMOTE_ASN]\nneighbor
  %[REMOTE_ADDRESS] description %[CONN_NAME]\naddress-family ipv6\n\tneighbor %[REMOTE_ADDRESS]
  activate\n\tneighbor %[REMOTE_ADDRESS] soft-reconfiguration inbound\n\tneighbor
  %[REMOTE_ADDRESS] prefix-list ipv6_ranges in\n\tneighbor %[REMOTE_ADDRESS] prefix-list
  ipv6_ranges out\n\tneighbor %[REMOTE_ADDRESS] route-map add_extra_hop in\n\tneighbor
  %[REMOTE_ADDRESS] route-map no_advertise_self out\nexit-address-family\n"

The documentation is quite scarce about this topic. Do I miss something? Or is this a bug in Psych?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the short and long YAML outputs using the custom class's encode_with method, setting coder.scalar, coder.tag, and Psych::Nodes::Scalar::LITERAL. Compare how Psych handles the scalar length and escaped newlines, then determine whether the documented behavior or the resulting output is incorrect. Done means the cause is explained and a focused regression test or documentation change is identified.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.