Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the YAML features that most programmers don't know?

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "what are the functions of YAML that most programmers do not know". In daily operation, I believe many people have doubts about the functions of YAML that most programmers do not know. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the questions of "what are the functions of YAML that most programmers do not know?" Next, please follow the editor to study!

Improve your YAML knowledge to write clearer YAML documents

YAML is a file format commonly used for data serialization. There are a large number of projects that use YAML files for configuration, such as Docker-compose,pre-commit,TravisCI,AWS Cloudformation,ESLint,Kubernetes,Ansible, and so on. Understanding the capabilities of YAML can help you achieve all of these features.

Let's start with the basics: YAML is a superset of JSON (source). Each valid JSON file is also a valid YAML file. This means that you have all the expected types: integers, floating-point numbers, strings, Boolean values, null values. And sequences and diagrams. Depending on your programming language, you might say "array" or "list" instead of sequence, and say "dictionary" instead of map.

It usually looks like this:

Mysql: host: localhost user: root password: something preprocessing_queue: # Line comments are available!-name: preprocessing.scale_and_center width: 32 height: 32-preprocessing.dot_reduction use_anonymous: true

Equivalent symbol

There are many equivalent writing methods for YAML:

List_by_dash:-foo-bar list_by_square_bracets: [foo, bar] map_by_indentation: foo: bar bar: baz map_by_curly_braces: {foo: bar, bar: baz} string_no_quotes: Monty Python string_double_quotes: "Monty Python" string_single_quotes: 'Monty Python' bool_english: yes bool_english_no: no bool_python: True bool_json: true

Here are some warnings:

Language: no # ISO 639-1 code for the Norwegian language

This is not interpreted as false. You need to enter "no" or "no".

In general, I recommend using true and false like Boolean JSON, but YAML supports 11 ways to write Boolean values. If you want to use quotation marks on strings, I will use "like JSON". You still need to remember "no", but at least the file looks more familiar to YAML beginners.

As Tom Tom Ritchford points out, there are many more similar dangerous cases:

013 maps to 11 because leading zeros trigger octal representation

4:30 maps to 270th. Max Werner Kaul-Gothe and Niklas Baumstark tell me that this is automatically converted to minutes (or seconds?) because it is interpreted as duration: 4 * 60 + 30 = 270. Interestingly, this mode can still "work" in the case of 1: 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4, and 30.

Long string

Disclaimer: > Lorem ipsum dolor sit amet, consectetur adipiscing elit. In nec urna pellentesque, imperdiet urna vitae, hendrerit odio. Donec porta aliquet laoreet. Sed viverra tempus fringilla.

This is equivalent to the following JSON (newline characters added for ease of reading; ignore them):

{"disclaimer": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. In nec urna pellentesque, imperdiet urna vitae, hendrerit odio. Donec porta aliquet laoreet. Sed viverra tempus fringilla."}

Multiline string

Mail_signature: | Martin Thoma Tel. + 49 123 4567

This is equivalent to JSON:

{"mail_signature": "Martin Thoma\ nTel. + 49 123 4567"}

Notice how to ignore leading spaces. The first line ("Martin Thoma") determines the number of leading whitespace ignored.

Anchor

Email: & emailAddress "info@example.de" id: * emailAddress

This is equivalent to the following JSON:

{"email": "info@example.de", "id": "info@example.de"}

& defines a variable emailAddress whose value is "info@example.de." then, * indicates that the variable name is immediately followed by the variable name.

You can do the same with the mapping:

Foo: & default_settings db: host: localhost name: main_db port: 1337 email: admin: admin@example.com prod: * default_settings dev: * default_settings

This makes:

{"dev": {"db": {"localhost", "name": "main_db", "port": 1337}, "email": {"admin": "admin@example.com"}}, "foo": {"db": {"host": "localhost" "name": "main_db", "port": 1337}, "email": {"admin": "admin@example.com"}, "prod": {"db": {"host": "localhost", "name": "main_db", "port": 1337} "email": {"admin": "admin@example.com"}}

Now, you may want to insert passwords in your development and production settings. You can use the merge key

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report