Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the use of star(*) in yaml file?

I was going through spring boot actuator when I stumbled upon this quote:

* has a special meaning in YAML, so be sure to add quotes if you want to include (or exclude) all endpoints.

I tried to look over the internet about it without any luck. What is the use of * in yaml file?

like image 315
Yara Avatar asked May 27 '20 16:05

Yara


People also ask

What is <<: In Yml?

The <<: inserts the content of that node. Allow me to quote the YAML spec here: Repeated nodes (objects) are first identified by an anchor (marked with the ampersand - “&”), and are then aliased (referenced with an asterisk - “*”) thereafter.

What is the use of YAML file?

What is YAML used for? One of the most common uses for YAML is to create configuration files. It's recommended that configuration files be written in YAML rather than JSON, even though they can be used interchangeably in most cases, because YAML has better readability and is more user-friendly.


1 Answers

* is used to remove the repeated nodes. Consider this yaml example:

myprop:
  uid: &id XXX
myprop1:
  id: *id

The above will expand to:

myprop:
  uid: XXX
myprop1:
  id: XXX

Now try running this code:

@Value("${myprop.uid}") String uid;
@Value("${myprop1.id}") String id;

@Bean
ApplicationRunner runner() {
    return args -> {
        System.out.println(uid);  // prints "XXX"
        System.out.println(id); // prints "XXX"
        System.out.println(uid.equals(id)); // prints "true"
    };
}

From the spec:

Repeated nodes (objects) are first identified by an anchor (marked with the ampersand - “&”), and are then aliased (referenced with an asterisk - “*”) thereafter.

like image 155
Aniket Sahrawat Avatar answered Sep 28 '22 08:09

Aniket Sahrawat