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 throughperson:# neither do comments on mapsname:Mike Wazowski# comments on values appearpets: - cat# comments on array values appear - nested: - list entryfood: [pizza] # comments on arrays do notemptyArray: []emptyMap: []
then
yq-o=propssample.yml
will output
# block comments come through# comments on values appearperson.name= Mike Wazowski# comments on array values appearperson.pets.0= catperson.pets.1.nested.0= list entryperson.food.0= pizza
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:
# block comments come throughperson:# neither do comments on mapsname:Mike Wazowski# comments on values appearpets: - cat# comments on array values appear - nested: - list entryfood: [pizza] # comments on arrays do notemptyArray: []emptyMap: []
then
yq-o=props--properties-array-bracketssample.yml
will output
# block comments come through# comments on values appearperson.name= Mike Wazowski# comments on array values appearperson.pets[0] = catperson.pets[1].nested[0] = list entryperson.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:
# block comments come throughperson:# neither do comments on mapsname:Mike Wazowski# comments on values appearpets: - cat# comments on array values appear - nested: - list entryfood: [pizza] # comments on arrays do notemptyArray: []emptyMap: []
# block comments come through# comments on values appearperson.name :@ Mike Wazowski# comments on array values appearperson.pets.0 :@ catperson.pets.1.nested.0 :@ list entryperson.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 throughperson:# neither do comments on mapsname:Mike Wazowski# comments on values appearpets: - cat# comments on array values appear - nested: - list entryfood: [pizza] # comments on arrays do notemptyArray: []emptyMap: []
then
yq-o=props--unwrapScalar=falsesample.yml
will output
# block comments come through# comments on values appearperson.name="Mike Wazowski"# comments on array values appearperson.pets.0= catperson.pets.1.nested.0="list entry"person.food.0= pizza
Encode properties: no comments
Given a sample.yml file of:
# block comments come throughperson:# neither do comments on mapsname:Mike Wazowski# comments on values appearpets: - cat# comments on array values appear - nested: - list entryfood: [pizza] # comments on arrays do notemptyArray: []emptyMap: []
then
yq-o=props'... comments = ""'sample.yml
will output
person.name= Mike Wazowskiperson.pets.0= catperson.pets.1.nested.0= list entryperson.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 throughperson:# neither do comments on mapsname:Mike Wazowski# comments on values appearpets: - cat# comments on array values appear - nested: - list entryfood: [pizza] # comments on arrays do notemptyArray: []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 appearperson.name= Mike Wazowski# comments on array values appearperson.pets.0= catperson.pets.1.nested.0= list entryperson.food.0= pizzaemptyArray=emptyMap=
Decode properties
Given a sample.properties file of:
# block comments come through# comments on values appearperson.name= Mike Wazowski# comments on array values appearperson.pets.0= catperson.pets.1.nested.0= list entryperson.food.0= pizza
then
yq-p=propssample.properties
will output
person:# block comments come through# comments on values appearname:Mike Wazowskipets:# comments on array values appear - cat - nested: - list entryfood: - pizza
Decode properties: numbers
All values are assumed to be strings when parsing properties, but you can use the from_yaml operator on all the strings values to autoparse into the correct type.
# block comments come through# comments on values appearperson.name= Mike Wazowski# comments on array values appearperson.pets.0= catperson.pets.1.nested.0= list entryperson.food.0= pizza
# block comments come through# comments on values appearperson.name= Mike Wazowski# comments on array values appearperson.pets.0= dogperson.pets.1.nested.0= list entryperson.food.0= pizza