yq/pkg/yqlib/doc/usage/properties.md
Daniel Carbone 98b411f82e
adding support for --wrapScalar=false in properties encoder (#1241)
* adding support for --wrapScalar=false in properties encoder

* altering encoder and decoder tests somewhat

* adding .idea

* Revert "altering encoder and decoder tests somewhat"

This reverts commit e3655130e2.

* adding test scenario for encoding with wrapped scalars
2022-06-25 12:22:03 +10:00

3.6 KiB

Properties

Encode to a property file (decode not yet supported). Line comments on value nodes will be copied across.

By default, empty maps and arrays are not encoded - see below for an example on how to encode a value for these.

{% hint style="warning" %} Note that versions prior to 4.18 require the 'eval/e' command to be specified.

yq e <exp> <file> {% endhint %}

Encode properties

Note that empty arrays and maps are not encoded by default.

Given a sample.yml file of:

# 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

yq -o=props sample.yml

will output

# comments on values appear
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:

# 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

yq -o=props --unwrapScalar=false sample.yml

will output

# comments on values appear
person.name = "Mike Wazowski"

# comments on array values appear
person.pets.0 = cat
person.food.0 = pizza

Encode properties: no comments

Given a sample.yml file of:

# 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

yq -o=props '... comments = ""' sample.yml

will output

person.name = Mike Wazowski
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:

# 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

yq -o=props '(.. | select( (tag == "!!map" or tag =="!!seq") and length == 0)) = ""' sample.yml

will output

# comments on values appear
person.name = Mike Wazowski

# comments on array values appear
person.pets.0 = cat
person.food.0 = pizza
emptyArray = 
emptyMap = 

Decode properties

Given a sample.properties file of:

# comments on values appear
person.name = Mike Wazowski

# comments on array values appear
person.pets.0 = cat
person.food.0 = pizza

then

yq -p=props sample.properties

will output

person:
    name: Mike Wazowski # comments on values appear
    pets:
        - cat # comments on array values appear
    food:
        - pizza

Roundtrip

Given a sample.properties file of:

# comments on values appear
person.name = Mike Wazowski

# comments on array values appear
person.pets.0 = cat
person.food.0 = pizza

then

yq -p=props -o=props '.person.pets.0 = "dog"' sample.properties

will output

# comments on values appear
person.name = Mike Wazowski

# comments on array values appear
person.pets.0 = dog
person.food.0 = pizza