Multiple Level Nesting in Yaml

Multiple level nesting in YAML

That's exactly how I've used nested levels in YAML for configuration files for perl scripts. This YAML Tutorial might be a good reference for you on how to handle the structure you want in Ruby.

I think your problem is trying to mix types. I suggest revising like this:

reporting: 
stored_procs:
-
name: reportingsp
uses:
usedin: breadcrumb
-
name: secondProc
uses:
usedin: something_else

How to replace a value in a multiple level nesting in YAML using ruby

Try this instead:

data["test"]["accounts"][0]["id"] = 2

Remember that accounts is an array of hashes, not a hash.

How to access multiple nested variables in Jekyll YAML config

In your _config.yml, you've defined site.github as a list, and you're trying to access it as an associative array, hence the problem.

If you want to access it as an associative array, you'll need to redefine your variable as such:

github:
repo: 'https://github.com/foo/bar.github.io'

As of writing this, I don't think that the Wikipedia sections I have linked to are super clear, but you can refer to their sample document, which I think showcases YAML's possibilities pretty well.

What is the maximum nesting depth in yaml?

As of YAML 1.2, the only limit imposed by the spec is 1024 characters on implicit keys:

foo: bar                                # okay
[ foo ]: bar # also okay
<<more-than-1024-characters-here>>: bar # not okay

This is forbidden regardless of whether the key is a scalar (first line) or a collection node (second line). Therefore, it practically limits nesting in implicit keys. However, you can simply use explicit keys instead:

? foo
: bar
? [ foo ]
: bar
? <<more-than-1024-characters-here>>
: bar

Apart from this, you do not need to be concerned about nesting as handwritten files are very unlikely to reach limits imposed by an implementation. You could of course test the implementation you use for its limits, e.g. PyYAML:

import yaml, traceback
from yaml.constructor import ConstructorError

for i in range(128, 1024):
open, close = "", ""
for j in range(0, i):
open, close = open + "[", close + "]"

try: yaml.load(open + close)
except:
traceback.print_exc()
print("failed at depth " + str(i))
exit(1)

This yields (after some time):

[…]
RecursionError: maximum recursion depth exceeded
failed at depth 496

A possible depth of 495 levels seems to be plenty.

YAML multiple line on sub level

Yes you need to indent follow up lines for your folded scalars with at least the same indentation as the parent line:

-
test: >
long
test
text

This has nothing to do with this being a sub level or not, this applies to folded (and literal) scalars at any level. For the folding it doesn't make much of a difference, but for literal style the leading spaces are removed with the same indentation as the top line (unless you specify an extra indent offset).

The less indented line ends the folded scalar, and at that point you need to start a new sequence entry (with -), but you have scalar there, test, resulting in an error.

Error with YAML deep nesting

If you want the value as well as the error messages to "belong" to the key, you need to make a list of two items

name:
- Name
- error: Please enter your name.

or as another mapping with two items:

name:
value: Name
error: Please enter your name.

yaml multi nested and python dictionary

I can think of two ways that will save you some typing on the yaml front.

Use the short mapping syntax:

test1:
a:
a1:
a2: {a3: 0, b3: 0, c3: 0}
b2: {a3: 0, b3: 0, c3: 0}
c2: {a3: 0, b3: 0, c3: 0}
b1:
a2: {a3: 0, b3: 0, c3: 0}
b2: {a3: 0, b3: 0, c3: 0}
c2: {a3: 0, b3: 0, c3: 0}
c1:
a2: {a3: 0, b3: 0}

Use aliases:

entry: &aliasentry
a2: {a3: 0, b3: 0, c3: 0}
b2: {a3: 0, b3: 0, c3: 0}
c2: {a3: 0, b3: 0, c3: 0}

test1:
a:
a1: *aliasentry
b1: *aliasentry
c1: {a2: {a3: 0, b3: 0}}


Related Topics



Leave a reply



Submit