2022-02-06 03:41:27 +00:00
|
|
|
# Properties
|
2021-10-30 03:14:39 +00:00
|
|
|
|
2022-08-01 06:46:09 +00:00
|
|
|
Encode/Decode/Roundtrip to/from a property file. 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
|
|
|
## 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
|
2022-10-29 07:24:11 +00:00
|
|
|
# block comments come through
|
2021-10-30 03:14:39 +00:00
|
|
|
person: # neither do comments on maps
|
2022-07-15 00:45:22 +00:00
|
|
|
name: Mike Wazowski # comments on values appear
|
2021-10-30 03:14:39 +00:00
|
|
|
pets:
|
|
|
|
- cat # comments on array values appear
|
2024-03-04 23:14:15 +00:00
|
|
|
- nested:
|
|
|
|
- list entry
|
2021-10-30 03:14:39 +00:00
|
|
|
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
|
2022-10-29 07:24:11 +00:00
|
|
|
# block comments come through
|
2022-02-06 03:41:27 +00:00
|
|
|
# comments on values appear
|
2022-07-15 00:45:22 +00:00
|
|
|
person.name = Mike Wazowski
|
|
|
|
|
|
|
|
# comments on array values appear
|
|
|
|
person.pets.0 = cat
|
2024-03-04 23:14:15 +00:00
|
|
|
person.pets.1.nested.0 = list entry
|
2022-07-15 00:45:22 +00:00
|
|
|
person.food.0 = pizza
|
|
|
|
```
|
|
|
|
|
2024-03-04 23:14:15 +00:00
|
|
|
## Encode properties with array brackets
|
|
|
|
Declare the --properties-array-brackets flag to give array paths in brackets (e.g. SpringBoot).
|
|
|
|
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
# block comments come through
|
|
|
|
person: # neither do comments on maps
|
|
|
|
name: Mike Wazowski # comments on values appear
|
|
|
|
pets:
|
|
|
|
- cat # comments on array values appear
|
|
|
|
- nested:
|
|
|
|
- list entry
|
|
|
|
food: [pizza] # comments on arrays do not
|
|
|
|
emptyArray: []
|
|
|
|
emptyMap: []
|
|
|
|
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
|
|
|
yq -o=props --properties-array-brackets sample.yml
|
|
|
|
```
|
|
|
|
will output
|
|
|
|
```properties
|
|
|
|
# block comments come through
|
|
|
|
# comments on values appear
|
|
|
|
person.name = Mike Wazowski
|
|
|
|
|
|
|
|
# comments on array values appear
|
|
|
|
person.pets[0] = cat
|
|
|
|
person.pets[1].nested[0] = list entry
|
|
|
|
person.food[0] = pizza
|
|
|
|
```
|
|
|
|
|
|
|
|
## Encode properties - custom separator
|
|
|
|
Use the --properties-customer-separator flag to specify your own key/value separator.
|
|
|
|
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
# block comments come through
|
|
|
|
person: # neither do comments on maps
|
|
|
|
name: Mike Wazowski # comments on values appear
|
|
|
|
pets:
|
|
|
|
- cat # comments on array values appear
|
|
|
|
- nested:
|
|
|
|
- list entry
|
|
|
|
food: [pizza] # comments on arrays do not
|
|
|
|
emptyArray: []
|
|
|
|
emptyMap: []
|
|
|
|
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
|
|
|
yq -o=props --properties-customer-separator=" :@ " sample.yml
|
|
|
|
```
|
|
|
|
will output
|
|
|
|
```properties
|
|
|
|
# block comments come through
|
|
|
|
# comments on values appear
|
|
|
|
person.name :@ Mike Wazowski
|
|
|
|
|
|
|
|
# comments on array values appear
|
|
|
|
person.pets.0 :@ cat
|
|
|
|
person.pets.1.nested.0 :@ list entry
|
|
|
|
person.food.0 :@ pizza
|
|
|
|
```
|
|
|
|
|
2022-07-15 00:45:22 +00:00
|
|
|
## 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
|
2022-10-29 07:24:11 +00:00
|
|
|
# block comments come through
|
2022-07-15 00:45:22 +00:00
|
|
|
person: # neither do comments on maps
|
|
|
|
name: Mike Wazowski # comments on values appear
|
|
|
|
pets:
|
|
|
|
- cat # comments on array values appear
|
2024-03-04 23:14:15 +00:00
|
|
|
- nested:
|
|
|
|
- list entry
|
2022-07-15 00:45:22 +00:00
|
|
|
food: [pizza] # comments on arrays do not
|
|
|
|
emptyArray: []
|
|
|
|
emptyMap: []
|
|
|
|
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
|
|
|
yq -o=props --unwrapScalar=false sample.yml
|
|
|
|
```
|
|
|
|
will output
|
|
|
|
```properties
|
2022-10-29 07:24:11 +00:00
|
|
|
# block comments come through
|
2022-07-15 00:45:22 +00:00
|
|
|
# comments on values appear
|
|
|
|
person.name = "Mike Wazowski"
|
2022-02-06 03:41:27 +00:00
|
|
|
|
|
|
|
# comments on array values appear
|
|
|
|
person.pets.0 = cat
|
2024-03-04 23:14:15 +00:00
|
|
|
person.pets.1.nested.0 = "list entry"
|
2022-02-06 03:41:27 +00:00
|
|
|
person.food.0 = pizza
|
|
|
|
```
|
|
|
|
|
|
|
|
## Encode properties: no comments
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
2022-10-29 07:24:11 +00:00
|
|
|
# block comments come through
|
2022-02-06 03:41:27 +00:00
|
|
|
person: # neither do comments on maps
|
2022-07-15 00:45:22 +00:00
|
|
|
name: Mike Wazowski # comments on values appear
|
2022-02-06 03:41:27 +00:00
|
|
|
pets:
|
|
|
|
- cat # comments on array values appear
|
2024-03-04 23:14:15 +00:00
|
|
|
- nested:
|
|
|
|
- list entry
|
2022-02-06 03:41:27 +00:00
|
|
|
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
|
2022-07-15 00:45:22 +00:00
|
|
|
person.name = Mike Wazowski
|
2022-02-06 03:41:27 +00:00
|
|
|
person.pets.0 = cat
|
2024-03-04 23:14:15 +00:00
|
|
|
person.pets.1.nested.0 = list entry
|
2022-02-06 03:41:27 +00:00
|
|
|
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
|
2022-10-29 07:24:11 +00:00
|
|
|
# block comments come through
|
2022-02-06 03:41:27 +00:00
|
|
|
person: # neither do comments on maps
|
2022-07-15 00:45:22 +00:00
|
|
|
name: Mike Wazowski # comments on values appear
|
2022-02-06 03:41:27 +00:00
|
|
|
pets:
|
|
|
|
- cat # comments on array values appear
|
2024-03-04 23:14:15 +00:00
|
|
|
- nested:
|
|
|
|
- list entry
|
2022-02-06 03:41:27 +00:00
|
|
|
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
|
2022-10-29 07:24:11 +00:00
|
|
|
# block comments come through
|
2021-10-30 03:14:39 +00:00
|
|
|
# comments on values appear
|
2022-07-15 00:45:22 +00:00
|
|
|
person.name = Mike Wazowski
|
2021-10-30 03:14:39 +00:00
|
|
|
|
|
|
|
# comments on array values appear
|
|
|
|
person.pets.0 = cat
|
2024-03-04 23:14:15 +00:00
|
|
|
person.pets.1.nested.0 = list entry
|
2021-10-30 03:14:39 +00:00
|
|
|
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
|
2022-10-29 07:24:11 +00:00
|
|
|
# block comments come through
|
2022-02-15 03:16:04 +00:00
|
|
|
# comments on values appear
|
2022-07-15 00:45:22 +00:00
|
|
|
person.name = Mike Wazowski
|
2022-02-15 03:16:04 +00:00
|
|
|
|
|
|
|
# comments on array values appear
|
|
|
|
person.pets.0 = cat
|
2024-03-04 23:14:15 +00:00
|
|
|
person.pets.1.nested.0 = list entry
|
2022-02-15 03:16:04 +00:00
|
|
|
person.food.0 = pizza
|
|
|
|
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
|
|
|
yq -p=props sample.properties
|
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
person:
|
2024-03-04 23:14:15 +00:00
|
|
|
# block comments come through
|
|
|
|
# comments on values appear
|
|
|
|
name: Mike Wazowski
|
|
|
|
pets:
|
|
|
|
# comments on array values appear
|
|
|
|
- cat
|
|
|
|
- nested:
|
|
|
|
- list entry
|
|
|
|
food:
|
|
|
|
- pizza
|
2022-02-15 03:16:04 +00:00
|
|
|
```
|
|
|
|
|
2022-11-11 03:58:39 +00:00
|
|
|
## Decode properties - array should be a map
|
|
|
|
If you have a numeric map key in your property files, use array_to_map to convert them to maps.
|
|
|
|
|
|
|
|
Given a sample.properties file of:
|
|
|
|
```properties
|
|
|
|
things.10 = mike
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
|
|
|
yq -p=props '.things |= array_to_map' sample.properties
|
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
things:
|
2024-03-04 23:14:15 +00:00
|
|
|
10: mike
|
2022-11-11 03:58:39 +00:00
|
|
|
```
|
|
|
|
|
2022-02-15 03:16:04 +00:00
|
|
|
## Roundtrip
|
|
|
|
Given a sample.properties file of:
|
|
|
|
```properties
|
2022-10-29 07:24:11 +00:00
|
|
|
# block comments come through
|
2022-02-15 03:16:04 +00:00
|
|
|
# comments on values appear
|
2022-07-15 00:45:22 +00:00
|
|
|
person.name = Mike Wazowski
|
2022-02-15 03:16:04 +00:00
|
|
|
|
|
|
|
# comments on array values appear
|
|
|
|
person.pets.0 = cat
|
2024-03-04 23:14:15 +00:00
|
|
|
person.pets.1.nested.0 = list entry
|
2022-02-15 03:16:04 +00:00
|
|
|
person.food.0 = pizza
|
|
|
|
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
|
|
|
yq -p=props -o=props '.person.pets.0 = "dog"' sample.properties
|
|
|
|
```
|
|
|
|
will output
|
|
|
|
```properties
|
2022-10-29 07:24:11 +00:00
|
|
|
# block comments come through
|
2022-02-15 03:16:04 +00:00
|
|
|
# comments on values appear
|
2022-07-15 00:45:22 +00:00
|
|
|
person.name = Mike Wazowski
|
2022-02-15 03:16:04 +00:00
|
|
|
|
|
|
|
# comments on array values appear
|
|
|
|
person.pets.0 = dog
|
2024-03-04 23:14:15 +00:00
|
|
|
person.pets.1.nested.0 = list entry
|
2022-02-15 03:16:04 +00:00
|
|
|
person.food.0 = pizza
|
|
|
|
```
|
|
|
|
|