Skip to content

Strings with newlines should be encoded as multiline literals #405

Closed
@tejassharma96

Description

This code:

let testYaml = #"""
script:
  - |
    if [[ some test ]]; then
      echo "Hello world!"
    fi
"""#
let loaded = try Yams.load(yaml: testYaml) as? [String: Any]
print(loaded as Any)

prints

Optional(["script": ["if [[ some test ]]; then\n  echo \"Hello world!\"\nfi"]])

But when I try to encode something with newlines:

let dump = try Yams.dump(object: loaded)
print(dump)

prints

script:
- "if [[ some test ]]; then\n  echo \"Hello world!\"\nfi"

instead of using a multiline literal, it instead encodes them as \n. This is confusing, since Yams handles parsing these fine, but is unable to handle encoding them. If you'd like to maintain the current behaviour, I'd suggested adding something like this to Emitter.Options:

enum NewLineHandlingStrategy {
    /// Encode newlines in strings as \n
    case escapeNewlines
    /// Encode newlines in strings with a literal block, ie |
    case literal
    /// Encode newlines in strings with a foided block, ie >
    case folded
}

Here's a reference on these

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions