yaml syntax for an object list - yaml

so we use yaml for translations at the moment.
So imagine the following list as an idea (not the real one)
# Employee records
- martin:
name: Martin D'vloper
job: Developer
skills: "Preferred skills"
mostPreferred: python
leastPreferred: perl
notImportant: pascal
- tabitha:
name: Tabitha Bitumen
job: Developer
skills:
- lisp
- fortran
- erlang
So what I want to know is, in the case of skills for Martin, I want to define skills, and then it needs to have a subgroup of itself for the other keys.
explaining how it works, site checks for the keys on the translation file, and there is a key that would be employee.martin.skills.mostPreferred for which it retrieves the variable, but for the label of that section it also wants to get the variable saved for the workd employee.martin.skills.
how would this be achieved on yaml?
at the moment I see the code doing this
- martin:
name: Martin D'vloper
job: Developer
skills: "Preferred skills"
"skills":
mostPreferred: python
leastPreferred: perl
notImportant: pascal
is this correct and necessary?

Related

Ansible - recursive argument_spec

I'm trying to create a role with argument validation via the meta/arguments_spec.yml file, and I'd like a user to be able to pass in a tree of arbitrary depth as one of the arguments. In other words, a valid family_tree argument might look like:
family_tree:
name: Sarah
children:
- name: John
- name: Jimmy
- name: Billy
But it may also look like:
family_tree:
name: Bob
children:
- name: Bob, Jr.
children:
- name: Bob, III
children:
- name: Bob, IV
children:
# and so on...
This would require a recursive reference of some kind, however, I can't seem to find anything that supports this in the native argument_spec options. I know how to create recursive references via JSON Schema, and I'm aware the ansible.utils.validate supports JSON Schema as well, but as far as I can tell, this would mean manually adding a task to the beginning of my role's tasks list, which defeats the purpose of a dedicated argument_spec file and makes things harder to reason about, so not my preferred option.

Adding an array of same variable names in yaml file

for this website, it takes information from a authors.Yaml file.
this file holds the detail for the demo author.
authors:
- slug: wutali
name: Takahiro Fujiwara
introduction:
Co-founder and CTO at Fuller, Inc. / Software & Data Engineer /
Python / Golang / React
however, I wish to put in my information.
- slug: Hemming
name: Alexander Hemming
introduction:
Developer, React, Node, Sveltekit, Goland.
- slug: Owner
name: John Doe
introduction:
Founder and CEO at a company.
adding this to the end doesn't seem to work. How would I make it some that this information is accessible for additional potential authors?
You have two indentation errors:
- slug: Hemming
name: Alexander Hemming
introduction:
Developer, React, Node, Sveltekit, Goland.
- slug: Owner # fixed indentation to be on par with previous item
name: John Doe
introduction: # next line must be more indented to be the value
# of this key.
Founder and CEO at a company.

merge multiple YAML files under a new field

I have several YAML files representing the pieces of a whole. I want to merge them under a new field ("guests") that declares the whole.
file1.yml
name: johnny
age: 23
file2.yml
name: sally
age: 21
output.yml
guests:
- name: johnny
age: 23
- name: sally
age: 21
tools like yq make merging/overwriting easy, but I can't find any that helps me nest values under new fields.
The tool you are looking for comes with several different names and
are called programming languages or scripting languages. I recommend
you use Python with ruamel.yaml installed. (disclaimer: I am the author of
that package).
Once you have that you can do:
python -c "import sys, ruamel.yaml; yaml=ruamel.yaml.YAML(); yaml.indent(sequence=4, offset=2); yaml.dump(dict(guest=[yaml.load(open(f)) for f in sys.argv[1:]]), sys.stdout)" file*.yml > output.yml
To get the desired output.
A few notes:
YAML files should have the .yaml extension unless your filesystem doesn't support that.
By default sequence elements are indented two spaces and the dash has no offset within that (i.e. would align with the g of guests. Hence the yaml.indent() call.
Any comments on the key-value files of your input file would be preserved but not automatically pushed out to the right from their original starting column unless necessary because of a mapping value getting in the way. Adjusting that is possible, but I would not recommend trying that in a one-liner.
If you need to preserve quotes add yaml.preserve_quotes = True; in the one-liner
If any of your YAML files contain multiple YAML documents, the above will fail. You would need to think about how to combine the documents, and use a try except clause to fall back to yaml.load_all() for documents that do (it would be a good idea to abandon the one-liner in favour of a multiline Python program at that point).
You can also do the above using the yaml commandline utility (installable with pip install ruamel.yaml.cmd>=0.5.0):
yaml from-dirs --sequence ./*.yml | yaml map --indent 2,4,2 guest - > output.yml
but this is a two step process (first combining multiple yaml files as root level sequence, then pushing that sequence to be a value for mapping), and thus twice as slow as the one-liner.

Citeproc YAML for a legal case citation

I am using Pandoc and want to cite a legal case. I want to enter the necessary data in YAML, similar to this:
- title: One-click science marketing
volume: '11'
URL: http://dx.doi.org/10.1038/nmat3283
DOI: 10.1038/nmat3283
issue: '4'
container-title: Nature Materials
publisher: Nature Publishing Group
author:
- family: Fenner
given: Martin
orcid: 0000-0003-1419-2405
page: 261-263
id: fenner2012a
type: article-journal
issued:
date-parts:
- 2012
- 3
(Cited: Blog entry by Martin Fenner)
After lengthy research I still don't know which fields I can use for a legal case. There are several CLS for legal cases, but I don't find any information what fields they expect. How I do find them out without reading through the CLS file, which I hardly understand?
Thanks for any help!
Some of the fields you can use for a legal case are shown in this Zotero entry exported to CSL JSON and then converted to CSL YAML. You will still have to find out which of the CSL files support these fields. https://forums.zotero.org/11/ may provide further assistance.
---
references:
- id: a
type: legal-case
author:
- family: Author
given: Al
issued:
- year: 1999
title: Title
container-title: Reporter
authority: Court
page: 123-456
volume: 9
number: Docket Number
references: History
abstract: Abstract
language: en-US
...

How to use YAML front and back matter in Ruby?

I have heard of the term "front matter" and "back matter" to refer to some YAML parsing at the beginning or end of a non-YAML file. However, I can't seem to find any examples/documentation of how to implement this. Maybe this isn't a standard YAML feature. How can I make use of this feature in my Ruby project?
FYI: The reason I want to do this is to be able to require some ruby files at the top, and assume the rest is YAML. I don't think this is normally allowed in a YAML file.
I just came across a nice example of something similar to what I am trying to do. It isn't necessarily an example of "front/back matter" but it might help someone in the future:
Using the __END__ keyword, you can stop ruby from parsing the rest of the file. The rest of the file is stored in a DATA variable, which is actually a File object:
#!/usr/bin/env ruby
%w(yaml pp).each { |dep| require dep }
obj = YAML::load(DATA)
pp obj
__END__
---
-
name: Adam
age: 28
admin: true
-
name: Maggie
age: 28
admin: false
Source

Resources