yq/pkg/yqlib/doc/usage/properties.md
Ryan Drew 2865022cf8
Add --properties-separator option (#1951)
This commit adds the --properties-separator option, which lets users
specify the separator used between keys and values in the properties
output format. This is done by adjusting the value of
github.com/magiconair/properties#Properties.WriteSeparator at encode
time.

Some refactoring of the properties encoder unit tests was done to make
it easier to write unit tests that include different separator values.

Fixes: #1864

Signed-off-by: Ryan Drew <ryan.drew@isovalent.com>
2024-02-20 10:57:44 +11:00

4.6 KiB

Properties

Encode/Decode/Roundtrip to/from a property file. 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.

Encode properties

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

Given a sample.yml file of:

# block comments 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

# block comments come through
# 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 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

# block comments come through
# 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 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 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

# block comments come through
# comments on values appear
person.name = Mike Wazowski

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

Encode properties: use custom separator

Provide a custom key-value separator using the --properties-separator flag.

Given a sample.yml file of:

# block comments 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 --properties-separator=";" sample.yml

will output

# block comments come through
# comments on values appear
person.name;Mike Wazowski

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

Decode properties

Given a sample.properties file of:

# block comments come through
# 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:
    # block comments come through
    # comments on values appear
    name: Mike Wazowski
    pets:
        # comments on array values appear
        - cat
    food:
        - pizza

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:

things.10 = mike

then

yq -p=props '.things |= array_to_map' sample.properties

will output

things:
    10: mike

Roundtrip

Given a sample.properties file of:

# block comments come through
# 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

# block comments come through
# comments on values appear
person.name = Mike Wazowski

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