Can you add a comment beside a field in YAML? - yaml

I have the following YAML object:
# profile fields
firstName: string
lastName: string
phoneNum: string # optional
active: boolean
notes: string # optional
role: string # admin | market | client
by: User_Id
Is it allowed to add comments beside the properties' names such as:
notes: string # optional
role: string # admin | market | client
I couldn't find a resource that mentions that, and I have no way to test this

Yes, it's allowed by the YAML spec, latest version 1.2.2: https://yaml.org/spec/1.2.2/.
The same examples that you posted appear on the spec's section on 2.1 Collections:
Example 2.2 Mapping Scalars to Scalars (player statistics)
hr: 65 # Home runs
avg: 0.278 # Batting average
rbi: 147 # Runs Batted In
The full comment specs has its own section, 6.6 Comments. The main rules are that it starts with a # and "must be separated from other tokens by white space characters". It can also span multiple lines.
firstName: john # comment line 1
# comment line 2
lastName: doe
As for knowing if it would work and
I have no way to test this
You can use any of the popular YAML linters (based on your comment, see What is "Linting"?). It can either be a command-line tool (like yq), a website (like YAMLlint), an integrated tool on where you would be using your YAML file (like Gitlab's CI Lint Tool), an extension on your IDE/editor, or some language-specific library (like ruamel.yaml for Python as demonstrated by this answer on a related question).
On YAMLlint, it shows your YAML as valid, but it strips out the comments because it "give you a nice clean UTF-8 version of it." (also, as the YAML spec says, "Comments are a presentation detail" only and "must not be used to convey content information". Processors should just ignore them). The image does show the payload to the site included the comments, and the site responded with 200/OK:
I personally use the yq command line utility.
$ cat test.yaml
# profile fields
firstName: string
lastName: string
phoneNum: string # optional
active: boolean
notes: string # optional
role: string # admin | market | client
by: User_Id
$ yq '.role' test.yaml
string
$ cat invalid.yaml
key1: [value2]
key2: key3: key4: value3
$ yq '.key2' invalid.yaml
Error: bad file 'invalid.yaml': yaml: line 2: mapping values are not allowed in this context

Related

Cloud build dynamic tags to help filter

I'm triggering a build on a web hook from a pull-request, and then it reads in the payload of the request. The payload has things like PR name, author, commit hash, etc.
I'd like to inject some of these into the tag attribute but their format doesn't comply:
invalid build: invalid build tag "Ari Example": must match format "^[\\w][\\w.-]{0,127}$"
Is there any way I could modify the tag dynamically, in the example above for a users name?
Ari Example -> ari-example
My cloudbuild.yaml looks like so:
substitutions:
# These a dynamically populated by the PR and substitutions
# below is just for illustrative purposes
_PR_AUTHOR_ID: '827364872-23472634-2352'
_PR_AUTHOR: 'Ari Example'
_PR_TITLE: 'fix: update some file'
steps:
- id: 'Pull Request Payload'
name: 'gcr.io/cloud-builders/git'
entrypoint: '/bin/bash'
args:
- '-c'
- |
echo Author: $_PR_AUTHOR (id: $_PR_AUTHOR_ID)
echo Title: $_PR_TITLE
tags: ['$_PR_AUTHOR']
Pure bash. Below you replace any spaces with dashes and convert the string to lowercase:
_PR_AUTHOR='Ari Example'
echo ${_PR_AUTHOR// /-} | awk '{print tolower($0)}'
Output: ari-example
If you want to convert special characters and add more validation you might need to use regex.

Anisble Yaml file formating for quotes

I'm running an ansible playbook that allows me to (or should allow me to) replace two lines in a configuration file for a remote server. Most of the file replacements are dictionary style entries where neither the key nor the value have quotes. But there is one replacement where the name needs to be quoted. Here is the task from the ansible playbook:
- name: Enable traefikExporter within vars.jsonnet configuration file.
ansible.builtin.replace:
path: /home/bill/cluster-monitoring/vars.jsonnet
regexp: "name: 'traefikExporter',[\n\r].*[\n\r]"
replace: |6
name: 'traefikExporter',
enabled: true,
The error thrown is:
The offending line appears to be
replace: |6
name: 'traefikExporter'
^ here
and it notes that it is a mismatched quote. But I've tried changing the yaml replace parameter to > and using \n for line breaks on a single line as well as the | with quoted lines. They all throw versions of the mismatched quote error.
For reference, the following task, which is immediately above this task, runs correctly without errors:
- name: Enable k3s Monitoring within vars.jsonnet configuration file.
ansible.builtin.replace:
path: /home/bill/cluster-monitoring/vars.jsonnet
regexp: "\ k3s: {[^}]*},"
replace: |2
k3s: {
enabled: true,
master_ip: ['192.168.2.139'],
},
The closest thing I could find is here, but this didn't work. In both cases the regexp covers multiple lines and the replace covers the same lines identified by the regexp. What am I missing?
update
I also tried replacing the replacement text and now believe that the actual formatting issue is in the regexp: "name: 'traefikExporter',[\n\r].*[\n\r]" line. No matter what I place in the replace line it throws the error. I think the quotes in the regexp are the issue.
work around
I came up with a work around but it still isn't right. The following is very close to what I'd like - but a bit frustrated that it isn't exactly what I expected:
- name: Enable traefikExporter within vars.jsonnet configuration file.
ansible.builtin.replace:
path: /home/bill/cluster-monitoring/vars.jsonnet
regexp: "name: 'traefikExporter',[\n\r].*"
replace: >4
name: 'traefikExporter',
enabled: true,
The |6 or >6 was the problem - but not sure why. My expected behavior was to have a 6 space indent. But this is the thing throwing the error. When I put it to 4 there is no quote error. But as you can see - to get the formatting right I have to do some weird spacing (the name: is the correct indentation with the 4, but I have to add 6 actual spaces to get the next line to align. Not part of this question - but neither the | nor the > seems to impact the trailing return in both cases there is an extra line after the replacement.
YAML processes escape sequences in double-quoted scalars, so you should use
regexp: "name: 'traefikExporter',[\\n\\r].*[\\n\\r]"
I suggest however to use a block scalar to avoid escaping hell:
regexp: >-
name: 'traefikExporter',[\n\r].*[\n\r]
Regarding the indentation indicator, this cannot work:
replace: |6
name: 'traefikExporter',
enabled: true,
You are basically saying „the following block scalar has 6 spaces indentation in addition to the indentation of the replace: key“. However, it only has two, which makes the YAML parser immediately drop out of block scalar parsing mode. The following error results from the YAML parser trying to interpret what you intend to be the block scalar's content as part of the YAML structure.
I suggest doing:
replace: |-2
name: 'traefikExporter',
enabled: true,
to give the block scalar 2 space in front of each line (which seems to be what you want) and also to remove the trailing newline (with the -).

Using date as an ENV variable in GitHub action

This is very funny but very frustrating problem. I am using an ENV variable, which specifies date. I provide an ISO 8601 compliant version and in application, I retrieve it and parse. When I specify it in GH action workflow, it is get parsed as a date (rather than a string) and formatted. Therefore, my application parsing fails.
Example:
.github/workflows/rust.yaml
env:
MY_DATE: '2020-10-07T12:00:00+01:00'
run: echo $MY_DATE
Result (GH action UI):
env:
TMOU_GAME_END: 10/07/2020 11:00:00
10/07/2020 11:00:00
It is specific to GitHub action and their yaml parsing, it works OK on Heroku, on various local setups, etc.
Things I tried and they don't work:
using no quotes, single quotes ('), double quotes (")
setting another ENV var, LC_TIME to en_DK.UTF-8
using !!str shorthand (see https://yaml.org/spec/1.2/spec.html, section Example 2.23. Various Explicit Tags); this one fails either with  The workflow is not valid. .github/workflows/rust.yml: Unexpected tag 'tag:yaml.org,2002:str' or with The workflow is not valid. .github/workflows/rust.yml: The scalar style 'DoubleQuoted | SingleQuoted' on line 29 and column 24 is not valid with the tag 'tag:yaml.org,2002:str'
Is there any help? Any secret parameter I can turn on? Any escaping sequence? I just wanna GH Actions yaml parser to treat the value as a string.
Surprisingly, it seems GitHub Actions workflow YAML parser does not fully implement the standard and using explicit typing (like !!str) does not work. You can, however, workaround it by setting the environment variable to the desired value not in the YAML file itself but dynamically during workflow execution using a dedicated workflow command:
steps:
- name: Dynamically set MY_DATE environment variable
run: echo "MY_DATE=2020-10-07T12:00:00+01:00" >> $GITHUB_ENV
- name: Test MY_DATE variable
run: echo ${{ env.MY_DATE }}
This should do the trick.

How to run multiple cucumberjs tags

I'm using grunt tasks to run my feature files using grunt cucumberjs
grunt cucumberjs --cucumbertags=#favourite
Above command runs all the scenarios with #favourite tag. I have an issue where i want scenarios to run on different env with diff data that belongs to environment.
#Book
Scenario Outline: Search by Author
When I search for "Leo Tolstoy"
Then I should see "<number_of_books>" books
#qa_env
Examples:
| number_of_books |
| 5 |
#dev_env
Examples:
| number_of_books |
| 3 |
How can I run scenario #Book with #qa_env data and #Book with #dev_env data ?
From the readme of grunt-cucumber
========================================================================
tags
Type: String or Array
Default: ''
Only execute the features or scenarios with tags matching TAG_EXPRESSION. Scenarios inherit tags declared on the Feature level. The simplest TAG_EXPRESSION is simply a tag. Example: tags: '#dev'
When a tag in a tag expression starts with a ~, this represents boolean NOT. Example: tags: ~#dev
A tag expression can have several tags separated by a comma, which represents logical OR. Example: tags: #dev,#wip
To represent a logical AND, use an array. This is useful if you want to skip certain features and run other specific features. Example: tags: ['~#wip', '#dev']
========================================================================
When you are on Windows you need to mind the usage of single and double quotes

Custom Syntax Highlighting in Sublime Text 3 – cannot convert

I am trying to create my own syntax highlighting for Sublime Text 3. The main purpose of it is to distinguish text written in Latin script from text written in Cyrillic script. I have already installed AAAPackageControl and read the tutorial, but I cannot make it work for some reason.
Here's the syntax I wrote
# [PackageDev] target_format: plist, ext: tmLanguage
---
name: ADVANCED TEXT
scopeName: text.mirkowy
fileTypes: []
uuid: 78dbe755-58eb-4cdf-b954-4438334aedb9
patterns:
- comment: Words in Latin Script
name: latin_text.text.mirkowy
match: [A-Za-z]+
- comment: Words in Cyrillic Script
name: cyrillic_text.text.mirkowy
match: [ЁЂЃЄЅІЇЈЉЊЋЌЎЏАБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдежзийклмнопрстуфхцчшщъыьэюяёђѓєѕіїјљњћќўџҐґ]+
- comment: Numbers
name: numbers.text.mirkowy
match: \d
...
However, when I press F7 to convert that file to plist, I keep getting and error and I don't understand why (bear in mind that I am completely new to creating one's own syntaxes and the like) — here's what it looks like:
Input type not specified, auto-detecting... YAML
No target format specified, searching in file... Property List
Parsing YAML... (C:\Users\iyoossaev\AppData\Roaming\Sublime Text 3\Packages\User\mirkowy.YAML-tmLanguage)
Error parsing YAML: while parsing a block mapping
What do I do wrong?
You almost got it, but there is a minor issue with your regexes - character classes surrounded by square brackets [ ] need to be within parentheses ( ). Your "Numbers" regex \d works fine without parens. So, just change your code to the following:
# [PackageDev] target_format: plist, ext: tmLanguage
---
name: ADVANCED TEXT
scopeName: text.mirkowy
fileTypes: []
uuid: 78dbe755-58eb-4cdf-b954-4438334aedb9
patterns:
- comment: Words in Latin Script
name: latin_text.text.mirkowy
match: ([A-Za-z]+)
- comment: Words in Cyrillic Script
name: cyrillic_text.text.mirkowy
match: ([ЁЂЃЄЅІЇЈЉЊЋЌЎЏАБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдежзийклмнопрстуфхцчшщъыьэюяёђѓєѕіїјљњћќўџҐґ]+)
- comment: Numbers
name: numbers.text.mirkowy
match: \d
and you should be all set. Note that I put a blank space between each block - it's not required, but it helps you visually separate each block, which comes in very useful when you get very complicated blocks.
If you'd like some examples of more complex .YAML-tmLanguage syntax definitions, two places come immediately to mind. The first is the Syntax Definitions folder in the PackageDev package itself. The second is actually in a personal project of mine, the Python Improved syntax definition which aims to be a much better replacement for the built-in Python syntax that ships with Sublime. You can find the source for PythonImproved.YAML-tmLanguage on Github. Feel free to open an issue there if you have any questions on syntax design, or just ask a new question here.
Good luck!

Resources