2022-02-01 04:34:17 +00:00
|
|
|
# Properties
|
|
|
|
|
2022-08-01 00:28:34 +00:00
|
|
|
Encode/Decode/Roundtrip to/from a property file. Line comments on value nodes will be copied across.
|
2022-02-01 04:34:17 +00:00
|
|
|
|
|
|
|
By default, empty maps and arrays are not encoded - see below for an example on how to encode a value for these.
|
|
|
|
|
2022-02-06 03:39:46 +00:00
|
|
|
{% hint style="warning" %}
|
|
|
|
Note that versions prior to 4.18 require the 'eval/e' command to be specified. 
|
|
|
|
|
|
|
|
`yq e <exp> <file>`
|
|
|
|
{% endhint %}
|
|
|
|
|
2022-02-01 04:34:17 +00:00
|
|
|
## Encode properties
|
|
|
|
Note that empty arrays and maps are not encoded by default.
|
|
|
|
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
# block comments don't come through
|
|
|
|
person: # neither do comments on maps
|
2022-06-25 02:22:03 +00:00
|
|
|
name: Mike Wazowski # comments on values appear
|
2022-02-01 04:34:17 +00:00
|
|
|
pets:
|
|
|
|
- cat # comments on array values appear
|
|
|
|
food: [pizza] # comments on arrays do not
|
|
|
|
emptyArray: []
|
|
|
|
emptyMap: []
|
|
|
|
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2022-02-10 01:02:53 +00:00
|
|
|
yq -o=props sample.yml
|
2022-02-01 04:34:17 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```properties
|
|
|
|
# comments on values appear
|
2022-06-25 02:22:03 +00:00
|
|
|
person.name = Mike Wazowski
|
|
|
|
|
|
|
|
# comments on array values appear
|
|
|
|
person.pets.0 = cat
|
|
|
|
person.food.0 = pizza
|
|
|
|
```
|
|
|
|
|
|
|
|
## Encode properties: scalar encapsulation
|
|
|
|
Note that string values with blank characters in them are encapsulated with double quotes
|
|
|
|
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
# block comments don't come through
|
|
|
|
person: # neither do comments on maps
|
|
|
|
name: Mike Wazowski # comments on values appear
|
|
|
|
pets:
|
|
|
|
- cat # comments on array values appear
|
|
|
|
food: [pizza] # comments on arrays do not
|
|
|
|
emptyArray: []
|
|
|
|
emptyMap: []
|
|
|
|
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
|
|
|
yq -o=props --unwrapScalar=false sample.yml
|
|
|
|
```
|
|
|
|
will output
|
|
|
|
```properties
|
|
|
|
# comments on values appear
|
|
|
|
person.name = "Mike Wazowski"
|
2022-02-01 04:34:17 +00:00
|
|
|
|
|
|
|
# comments on array values appear
|
|
|
|
person.pets.0 = cat
|
|
|
|
person.food.0 = pizza
|
|
|
|
```
|
|
|
|
|
|
|
|
## Encode properties: no comments
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
# block comments don't come through
|
|
|
|
person: # neither do comments on maps
|
2022-06-25 02:22:03 +00:00
|
|
|
name: Mike Wazowski # comments on values appear
|
2022-02-01 04:34:17 +00:00
|
|
|
pets:
|
|
|
|
- cat # comments on array values appear
|
|
|
|
food: [pizza] # comments on arrays do not
|
|
|
|
emptyArray: []
|
|
|
|
emptyMap: []
|
|
|
|
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2022-02-10 01:02:53 +00:00
|
|
|
yq -o=props '... comments = ""' sample.yml
|
2022-02-01 04:34:17 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```properties
|
2022-06-25 02:22:03 +00:00
|
|
|
person.name = Mike Wazowski
|
2022-02-01 04:34:17 +00:00
|
|
|
person.pets.0 = cat
|
|
|
|
person.food.0 = pizza
|
|
|
|
```
|
|
|
|
|
|
|
|
## Encode properties: include empty maps and arrays
|
|
|
|
Use a yq expression to set the empty maps and sequences to your desired value.
|
|
|
|
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
# block comments don't come through
|
|
|
|
person: # neither do comments on maps
|
2022-06-25 02:22:03 +00:00
|
|
|
name: Mike Wazowski # comments on values appear
|
2022-02-01 04:34:17 +00:00
|
|
|
pets:
|
|
|
|
- cat # comments on array values appear
|
|
|
|
food: [pizza] # comments on arrays do not
|
|
|
|
emptyArray: []
|
|
|
|
emptyMap: []
|
|
|
|
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2022-02-10 01:02:53 +00:00
|
|
|
yq -o=props '(.. | select( (tag == "!!map" or tag =="!!seq") and length == 0)) = ""' sample.yml
|
2022-02-01 04:34:17 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```properties
|
|
|
|
# comments on values appear
|
2022-06-25 02:22:03 +00:00
|
|
|
person.name = Mike Wazowski
|
2022-02-01 04:34:17 +00:00
|
|
|
|
|
|
|
# comments on array values appear
|
|
|
|
person.pets.0 = cat
|
|
|
|
person.food.0 = pizza
|
|
|
|
emptyArray =
|
|
|
|
emptyMap =
|
|
|
|
```
|
|
|
|
|
2022-02-10 01:02:53 +00:00
|
|
|
## Decode properties
|
|
|
|
Given a sample.properties file of:
|
|
|
|
```properties
|
|
|
|
# comments on values appear
|
2022-06-25 02:22:03 +00:00
|
|
|
person.name = Mike Wazowski
|
2022-02-10 01:02:53 +00:00
|
|
|
|
|
|
|
# comments on array values appear
|
|
|
|
person.pets.0 = cat
|
|
|
|
person.food.0 = pizza
|
|
|
|
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
|
|
|
yq -p=props sample.properties
|
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
person:
|
2022-06-25 02:22:03 +00:00
|
|
|
name: Mike Wazowski # comments on values appear
|
2022-02-10 01:02:53 +00:00
|
|
|
pets:
|
|
|
|
- cat # comments on array values appear
|
|
|
|
food:
|
|
|
|
- pizza
|
|
|
|
```
|
|
|
|
|
|
|
|
## Roundtrip
|
|
|
|
Given a sample.properties file of:
|
|
|
|
```properties
|
|
|
|
# comments on values appear
|
2022-06-25 02:22:03 +00:00
|
|
|
person.name = Mike Wazowski
|
2022-02-10 01:02:53 +00:00
|
|
|
|
|
|
|
# comments on array values appear
|
|
|
|
person.pets.0 = cat
|
|
|
|
person.food.0 = pizza
|
|
|
|
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
|
|
|
yq -p=props -o=props '.person.pets.0 = "dog"' sample.properties
|
|
|
|
```
|
|
|
|
will output
|
|
|
|
```properties
|
|
|
|
# comments on values appear
|
2022-06-25 02:22:03 +00:00
|
|
|
person.name = Mike Wazowski
|
2022-02-10 01:02:53 +00:00
|
|
|
|
|
|
|
# comments on array values appear
|
|
|
|
person.pets.0 = dog
|
|
|
|
person.food.0 = pizza
|
|
|
|
```
|
|
|
|
|