user3724031
user3724031

Reputation: 245

How to write mapping(s) in single line?

I have the following YAML

version: "0.1"
services: 
  svc: 
    image: test
    networks: 
      - test_net_1
      - test_net_2
      - test_net_3

networkMapping: 
  test_net_1: 
    external: true
  test_net_2: 
    external: true
  test_net_3: 
    external: true

I would like to rewrite the networkMapping value in a single line like the following:

version: "0.2"
services: 
  svc: 
    image: test
    networks: ['test_net_1', 'test_net_2', 'test_net_3']

networkMapping: {{'test_net_1': {'external': true}}, {'test_net_2': {'external': true}}, {'test_net_3': {'external': true}}}

but when linting / parsing it, it returns:

version: "0.2"
services: 
  svc: 
    image: test
    networks: 
      - test_net_1
      - test_net_2
      - test_net_3

networkMapping: 
  ? 
    test_net_1': 
      external: true
  : ~
  ? 
    test_net_2: 
      external: true
  : ~
  ? 
    test_net_3: 
      external: true
  : ~

and it cause error in the application:

invalid map key: map[interface {}]interface {}{"test_net_1":map[interface {}]interface {}{"external":true}}'

I checked with or without quotes; single or double, but no luck.

I did try changing it to associative arrays by replacing the first and last {} with [], but the application needs it as mapping rather than associate array.

Upvotes: 10

Views: 8937

Answers (1)

flyx
flyx

Reputation: 39638

You are using too many {}.
This is how you should write it:

networkMapping: {'test_net_1': {'external': true}, 'test_net_2': {'external': true}, 'test_net_3': {'external': true}}

Upvotes: 15

Related Questions