Skip to content

Commit

Permalink
Added properties convert to numbers example #2023
Browse files Browse the repository at this point in the history
  • Loading branch information
mikefarah committed May 12, 2024
1 parent d9adcdb commit 0c15cf3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
17 changes: 17 additions & 0 deletions pkg/yqlib/doc/usage/properties.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,23 @@ person:
- 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.

Given a sample.properties file of:
```properties
a.b = 10
```
then
```bash
yq -p=props ' (.. | select(tag == "!!str")) |= from_yaml' sample.properties
```
will output
```yaml
a:
b: 10
```

## 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.

Expand Down
9 changes: 8 additions & 1 deletion pkg/yqlib/properties_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,14 @@ var propertyScenarios = []formatScenario{
expected: "- person\n- name\n",
scenarioType: "decode",
},

{
description: "Decode properties: numbers",
subdescription: "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.",
input: "a.b = 10",
expression: " (.. | select(tag == \"!!str\")) |= from_yaml",
expected: "a:\n b: 10\n",
scenarioType: "decode",
},
{
description: "Decode properties - array should be a map",
subdescription: "If you have a numeric map key in your property files, use array_to_map to convert them to maps.",
Expand Down

0 comments on commit 0c15cf3

Please sign in to comment.