yq/usage/properties.md

160 lines
3.0 KiB
Markdown
Raw Normal View History

2022-02-06 03:41:27 +00:00
# Properties
2021-10-30 03:14:39 +00:00
2022-02-06 03:41:27 +00:00
Encode to a property file (decode not yet supported). Line comments on value nodes will be copied across.
2021-10-30 03:14:39 +00:00
2022-02-06 03:41:27 +00:00
By default, empty maps and arrays are not encoded - see below for an example on how to encode a value for these.
2021-10-30 03:14:39 +00:00
2022-02-06 03:41:27 +00:00
{% hint style="warning" %}
Note that versions prior to 4.18 require the 'eval/e' command to be specified. 
2021-10-30 03:14:39 +00:00
2022-02-06 03:41:27 +00:00
`yq e <exp> <file>`
{% endhint %}
## Encode properties
Note that empty arrays and maps are not encoded by default.
Given a sample.yml file of:
2021-10-30 03:14:39 +00:00
```yaml
# block comments don't come through
person: # neither do comments on maps
name: Mike # comments on values appear
pets:
- cat # comments on array values appear
food: [pizza] # comments on arrays do not
2022-02-06 03:41:27 +00:00
emptyArray: []
emptyMap: []
2021-10-30 03:14:39 +00:00
2022-02-06 03:41:27 +00:00
```
2021-10-30 03:14:39 +00:00
then
2022-02-06 03:41:27 +00:00
```bash
2022-02-15 03:16:04 +00:00
yq -o=props sample.yml
2022-02-06 03:41:27 +00:00
```
will output
```properties
# comments on values appear
person.name = Mike
# 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
name: Mike # comments on values appear
pets:
- cat # comments on array values appear
food: [pizza] # comments on arrays do not
emptyArray: []
emptyMap: []
2021-10-30 03:14:39 +00:00
2022-02-06 03:41:27 +00:00
```
then
2021-10-30 03:14:39 +00:00
```bash
2022-02-15 03:16:04 +00:00
yq -o=props '... comments = ""' sample.yml
2022-02-06 03:41:27 +00:00
```
will output
```properties
person.name = Mike
person.pets.0 = cat
person.food.0 = pizza
2021-10-30 03:14:39 +00:00
```
2022-02-06 03:41:27 +00:00
## Encode properties: include empty maps and arrays
Use a yq expression to set the empty maps and sequences to your desired value.
2021-10-30 03:14:39 +00:00
2022-02-06 03:41:27 +00:00
Given a sample.yml file of:
```yaml
# block comments don't come through
person: # neither do comments on maps
name: Mike # comments on values appear
pets:
- cat # comments on array values appear
food: [pizza] # comments on arrays do not
emptyArray: []
emptyMap: []
```
then
```bash
2022-02-15 03:16:04 +00:00
yq -o=props '(.. | select( (tag == "!!map" or tag =="!!seq") and length == 0)) = ""' sample.yml
2021-10-30 03:14:39 +00:00
```
2022-02-06 03:41:27 +00:00
will output
```properties
2021-10-30 03:14:39 +00:00
# comments on values appear
person.name = Mike
# comments on array values appear
person.pets.0 = cat
person.food.0 = pizza
2022-02-06 03:41:27 +00:00
emptyArray =
emptyMap =
2021-10-30 03:14:39 +00:00
```
2022-02-15 03:16:04 +00:00
## Decode properties
Given a sample.properties file of:
```properties
# comments on values appear
person.name = Mike
# comments on array values appear
person.pets.0 = cat
person.food.0 = pizza
```
then
```bash
yq -p=props sample.properties
```
will output
```yaml
person:
name: Mike # comments on values appear
pets:
- cat # comments on array values appear
food:
- pizza
```
## Roundtrip
Given a sample.properties file of:
```properties
# comments on values appear
person.name = Mike
# 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
person.name = Mike
# comments on array values appear
person.pets.0 = dog
person.food.0 = pizza
```
## Empty doc
Given a sample.properties file of:
```properties
```
then
```bash
yq -p=props sample.properties
```
will output
```yaml
```