DEV Community

Bryan Woolsey
Bryan Woolsey

Posted on

Dealing with long strings in YAML

We have a step in our Puppet CI/CD Pipeline that lints YAML data using adrienverge/yamllint. One of the rules limits the number of characters on a single line. Normally not a huge deal... except when dealing with long URIs or other long strings.

The problem is, I have to change URI Hiera data so infrequently that I always forget exactly what the syntax is. Admittedly, this post is really for me to look back on and reference, but why not share?

And while we're on the topic:

  • YAML Cookbook for Ruby is an awesome resource for YAML syntax in general.
  • Puppet parses YAML using the following function in Ruby:
  YAML.safe_load(File.read(path), [Symbol], [], true)
Enter fullscreen mode Exit fullscreen mode

Combine with Interactive Ruby Shell or ruby -e for a quick and easy way to test any YAML file you create.

Plain Style

"Flow" Styles

No character escaping, or characters matching /[#:]/.

---
key: This is a very
     long string.
Enter fullscreen mode Exit fullscreen mode
{ "key" => "This is a very long string." }
Enter fullscreen mode Exit fullscreen mode

Single-quoted style

No special characters, no escaping. Literal ' must be doubled-up ('').

---
key: 'This isn''t
     a very short string.'
Enter fullscreen mode Exit fullscreen mode
{ "key" => "This isn't a very short string." }
Enter fullscreen mode Exit fullscreen mode

Double-quoted style

Characters matching /[\\"]/ must be escaped by a \ character. Common escape sequences may be used. Line concatenation with a trailing \.

VERY useful for long URIs.

---
key: "http://this.is.my\
     .very.long.string"
Enter fullscreen mode Exit fullscreen mode
{ "key" => "http://this.is.my.very.long.string" }
Enter fullscreen mode Exit fullscreen mode

Block Notation

Literal Style

---
key: |
  This is a very
  long string.
Enter fullscreen mode Exit fullscreen mode
{ "key" => "This is a very\nlong string.\n" }
Enter fullscreen mode Exit fullscreen mode

Folded Style

---
key: >
  This is a very
  long string.
Enter fullscreen mode Exit fullscreen mode
{ "key" => "This is a very long string.\n" }
Enter fullscreen mode Exit fullscreen mode

Block chomping indicator

You may notice that both strings have newlines attached to the end. Want those gone? Use a different chomping indicator:

  • |, >: "clip". Keeps the newline.
  • |+, >+: "keep". Keeps the newline, and also keeps tailing blank lines.
  • |-, >-: "strip". Removes the newline.
---
key0: >
  Do. Or do not.
  There is no try.

key1: >+
  I find your lack of
  faith disturbing.

key2: >-
  The Force will be
  with you. Always.
Enter fullscreen mode Exit fullscreen mode
{
  "key0" => "Do. Or do not. There is no try.\n",
  "key1" => "I find your lack of faith disturbing.\n\n",
  "key2" => "The Force will be with you. Always."
}
Enter fullscreen mode Exit fullscreen mode

Source: YAML v1.2 Specification

Top comments (0)