50

When I review the cryptogen(a fabric command) config file . I saw there symbol.

Profiles:

    SampleInsecureSolo:
        Orderer:
            <<: *OrdererDefaults  ## what is the `<<`
            Organizations:
                - *ExampleCom     ## what is the `*`
        Consortiums:
            SampleConsortium:
                Organizations:
                    - *Org1ExampleCom
                    - *Org2ExampleCom

Above there a two symbol << and *.

Application: &ApplicationDefaults  # what is the `&` mean

    Organizations:

As you can see there is another symbol &. I don't know what are there mean. I didn't get any information even by reviewing the source code (fabric/common/configtx/tool/configtxgen/main.go)

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
blackdog
  • 2,007
  • 3
  • 25
  • 43

2 Answers2

76

Well, those are elements of the YAML file format, which is used here to provide a configuration file for configtxgen. The "&" sign mean anchor and "*" reference to the anchor, this is basically used to avoid duplication, for example:

person: &person
    name: "John Doe"

employee: &employee
    << : *person
    salary : 5000

will reuse fields of person and has similar meaning as:

employee: &employee
    name   : "John Doe"
    salary : 5000

another example is simply reusing value:

key1: &key some very common value

key2: *key

equivalent to:

key1: some very common value

key2: some very common value

Since abric/common/configtx/tool/configtxgen/main.go uses of the shelf YAML parser you won't find any reference to these symbols in configtxgen related code. I would suggest to read a bit more about YAML file format.

lanoxx
  • 12,249
  • 13
  • 87
  • 142
Artem Barger
  • 40,769
  • 9
  • 59
  • 81
  • 5
    Tried your ': << *person' line in https://yaml-online-parser.appspot.com/ and it failed to compile. That line should be '<< : *person' though. And << need to indent with salary on next line. – Wappenull Jul 11 '19 at 11:48
  • 3
    @Wappenull: I can confirm your fix and have updated the answer accordingly. – lanoxx Apr 10 '20 at 14:26
  • Quick question, is it possible to create a list using multiple references ? if L1 is -a -b -c and L2 is -d -e -f, can I create L3 by using *l1a & l2a , anchors to L1 and L2 respectively to create -a -b -c -d -e -f ? – Dhiwakar Ravikumar Apr 18 '22 at 16:10
  • `*` and `&` are described clearly in the [yaml spec](https://yaml.org/spec/1.2.2/). The confusing part is `<<:` which isn't. – spinkus Mar 18 '23 at 12:33
1

in yaml if data is like

user: &userId '123'
username: *userId
equivalent yml is
user: '123'
username: '123'

or

equivalent json will is
{
    "user": "123",
    "username": "123"
}

so it basically allows to reuse data, you can also try with array instead of single value like 123

try converting below yml to json using any yml to json online converter

users: &users
  k1: v1
  k2: v2
  
usernames: *users
Arvind G
  • 11
  • 2