Reputation: 9
I have two keys - app.user = 'user1' and app.user.age= '21' . I need to write these in yml file. I am getting errors when I am writing these like-
1) app:
user: 'user1'
age: '21'
error: mapping values are not allowed here
2) app:
user: 'user1'
app:
user:
age:'21'
error: **duplication error**
Upvotes: 0
Views: 78
Reputation: 39768
Your data model contradicts itself when interpreted as YAML data, if we assume that the notation x.y
has the semantics the value of y
in a mapping referred to by x
. Note that this notation is not defined within YAML. Based on this assumption, here's the contradiction:
app.user = 'user1' (1)
app.user.age= '21' (2)
Claim (1)
means:
The value of app is a mapping. This mapping contains a key user whose value is a scalar with the content
user1
.
Claim (2)
means:
The value of app is a mapping. This mapping contains a key user whose value is also a mapping. In that mapping, there is a key age with value
21
.
The contradiction is obvious: app.user
cannot be a scalar value as implied by claim (1)
and a mapping as implied by claim (2)
at the same time. So the solution obviously is to remove the contradiction from your data model. One possible way to do this is:
app:
user:
id: 'user1'
age: '21'
As for the duplication error: YAML forbids a mapping to contain equal keys. In your code, you have two keys app
in the root mapping, which causes the error.
Note:
I am aware that there are data serialisations that allow a key having a simple and a complex value at the same time. I can only guess you got the idea for your model from there. It is not inherently wrong, just not supported by YAML. Boost's INFO file format is one of those formats.
Edit:
If you disagree with my assumption above about the semantics of x.y
, you probably come from Java land where java.lang
and java.util
have absolutely nothing to do with each other except for their names having the same prefix. Most importantly, they are not child packages of the package java
(if that surprises you, read the section Apparent Hierarchies of Packages in the docs).
Following this semantics, there is no hierarchy in your data, and thus, no hierarchy in your YAML. instead of writing nested mappings, you would write:
app.user: 'user1'
app.user.age: '21'
Note that the .
is not a special YAML character, but just a part of the value's key.
Upvotes: 1