is this valid yaml? - yaml

items:
house:
- bathroom:
- toothbrush
- soap
- bedroom:
- bed:
- pillow
- sheet
- closet:
- clothes:
- underwear
- socks
garden:
- treehouse:
- toys:
- nerfgun
- car
- window
- garage:
- car
- toolbox:
- hammer
- scewdriver
- pliers
- lawnmower
Here is another try at this document, it has no compound list (I guess that's how it's called).
items2:
house:
- bathroom:
- toothbrush
- soap
- bedroom:
- bed:
- pillow
- sheet
- closet:
- clothes:
- underwear
- socks
Which of those two yaml documents are valid ? I'm still wondering if I can use a list of keyed lists like that (nested list ?):
items:
- list1:
-itemA
-itemB
- list2:
-itemC
-itemD

You can use this to check if your yaml is ok: yamlint
It's seems ok.

Yes, it's valid YAML (well, the first two are; in the third, make sure that you have a space after your - in the sequences); but it may not do exactly what you think. In your toy example
items:
- list1:
- itemA
- itemB
- list2:
- itemC
- itemD
the value associated with items is a sequence; and each entry of that sequence is a map with a single key/value pair (for the first entry, the key is list1, and in the second, list2).
What may have confused you in your first real example was how to access each element. Since you tagged this yaml-cpp, here's how you would get, say, the list of the toys in the greenhouse of your first example:
doc["items"]["garden"][0]["treehouse"][0]["toys"];
(Note the [0] before accessing the "treehouse" and "toys" keys.)

Related

Ansible object list from merging a list and a value

Given a string list
list:
- a
- b
and a string
str: c
How can be obtained an object list with the same number of elements as the first list with the following structure?:
new_list:
- list_key: a
str_key: c
- list_key: b
str_key: c
Also, I'm using Ansible 2.8
For example
- set_fact:
new_list: "{{ list|
map('regex_replace', '^(.*)$', '{list_key: \\1}')|
map('from_yaml')|
map('combine', {'str_key': str})|
list }}"

How do I add a map to an array of maps in ytt?

I'm trying to add a map to an array of maps in ytt to modify a YAML doc.
I tried the below but it errors out and says it expects a map but getting an array.
https://gist.github.com/amalagaura/c8b5c7c92402120ed76dec95dfafb276
---
id: 1
type: book
awards:
books:
- id: 1
title: International Botev
reviewers:
- id: 2
name: PersonB
- id: 2
title: Dayton Literary Peace Prize
reviewers:
- id: 3
name: PersonC
#! How do I add a map to an array of maps?
## load("#ytt:overlay", "overlay")
##overlay/match by=overlay.all
---
awards:
books:
##overlay/match by=overlay.all, expects="1+"
##overlay/match missing_ok=True
reviewers:
##overlay/append
- id: 1
name: PersonA
## load("#ytt:overlay", "overlay")
#! Add a map to an array of maps:
##overlay/match by=overlay.all
---
awards:
books:
##overlay/match by=overlay.all, expects="1+"
- reviewers:
##overlay/append
- id: 1
name: Person A
You were really close in your solution, all you really needed was to make reviewers an array item. If you want to be able to add reviewers to a book that does not have that key, then you will have to add a matcher on the array item and the map item; a gist is included below to see this behavior overlay in action.
If you have more than one ##overlay/match annotation on the same item, the last one wins. There are plans to improve this behavior: https://github.com/k14s/ytt/issues/114.
https://get-ytt.io/#gist:https://gist.github.com/gcheadle-vmware/a6243ee73fa5cc139dba870690eb15c5

Ruby: Yaml::dump outputs integers in quotes using Postgres

I am writing a Ruby gem that converts a sql executable to yaml, using Yaml::dump. However, when testing it in Postgresql I am finding that the integers are output with single quotes around them (as strings) unless they start with a zero. Below is the code snippet of the call to Yaml::dump and some resulting data.
db_object = {}
db_output = {}
full_table = ActiveRecord::Base.connection.execute("SELECT * FROM #{model};")
keys = full_table[0].keys
db_object["columns"] = keys
model_arr=[]
full_table.each do |row|
model_arr << row.values_at(*keys)
end
db_object["records"] = model_arr
db_output[model] = db_object
YAML::dump(db_output, file)
And here are the first couple rows of results:
schema_migrations:
columns:
- version
records:
- - '20121225230020'
- - '20121225230129'
---
students:
columns:
- id
- first_name
- last_name
- date_of_birth
- rank
- phone
records:
- - '1'
- Celestino
- Towne
- '2007-09-20'
- '2'
- '6417358360'
Any insight would be much appreciated.

Why doesn't YAML alias work unless I use flow style?

If you run this YAML 1.1
- &first {'first': ['description', ['aliases'], ["Explanatory sentences ", "go here."]]}
- *first
- &second 'second':
- 'description'
- ['aliases']
-
- "Explanatory sentences "
- "go here."
- *second
through YAMLlint, you get this:
---
-
first:
- description
-
- aliases
-
- "Explanatory sentences "
- "go here."
-
first:
- description
-
- aliases
-
- "Explanatory sentences "
- "go here."
-
second:
- description
-
- aliases
-
- "Explanatory sentences "
- "go here."
- second
Notice that the first group is repeated twice, while the second group is only shown in full once, with just the name where the repeated block should be. The first group and the second group have exactly the same data - the only difference is the layout. Why doesn't the alias work properly for the second group?
My best guess is that the &anchor has very high precedence. I tried this
- &first 'first': ['description', ['aliases'], ["Explanatory sentences ", "go here."]]
- *first
Rather than this:
- &first {'first': ['description', ['aliases'], ["Explanatory sentences ", "go here."]]}
- *first
And suddenly it behaved the same way as the second group. So it appears that unless you explicitly include the 'first' in a larger node, the &first anchor attaches to just the 'first' string and nothing else.

How do you exclude a specific character pattern with regular expressions

I working with some regular expression matching and I'm trying to figure out how you would exclude a specific character pattern. Specifically, I want to exclude the following pattern:
5 - #in words: digit, space, dash & space)
I know how to exclude the components individually: [^5 ^-] but I'm looking to exclude the specific pattern. Is this possible?
Update - I'm using Ruby as my programming language.
Here is some sample input and desired output.:
Input: 1 - Blue-Stork Stables; 2 - Young, Robert, S.; 3 - Seahorse Stable; 4 - Carney, Elvis; 5 - Guerrero, Juan, Carlos-Martin; 6 - Dubb, Michael; 7 - Summers, Hope; 8 - DTH Stables; 9 - Peebles, Matthew\n
the desired output would be:
Output: Blue-Stork Stables; Young, Robert, S.; Seahorse Stable; Carney, Elvis; Guerrero, Juan, Carlos-Marting; Dubb, Michael; Summers, Hope; DTH Stables; Peebles, Matthew\n
Please take note of the dashes on Blue-Stork Stables and Juan Carlos-Martin.
EDIT: So you mean "remove", not "exclude". No problem:
result = subject.gsub(/\d+ - /, '')
transforms your input into the desired output. I've taken the liberty to allow more than one digit (after all, if numbers reach 10 or higher, you probably want to remove those entirely, too. Right?).
(Old answer for "historical reasons")
Depending on what you mean by "exclude", it appears that you're looking for negative lookahead assertions:
^(?!.*\d - )
will fail on strings that contain 5 - anywhere and succeed on all other strings:
"5 - " // fail
"5 -" // match
"abc5 - xyz" // fail
"foobar5 - " // fail

Resources