Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
161 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# To Number | ||
Parses the input as a number. yq will try to parse values as an int first, failing that it will try float. Values that already ints or floats will be left alone. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# To Number | ||
Parses the input as a number. yq will try to parse values as an int first, failing that it will try float. Values that already ints or floats will be left alone. | ||
|
||
## Converts strings to numbers | ||
Given a sample.yml file of: | ||
```yaml | ||
- "3" | ||
- "3.1" | ||
- "-1e3" | ||
``` | ||
then | ||
```bash | ||
yq '.[] | to_number' sample.yml | ||
``` | ||
will output | ||
```yaml | ||
3 | ||
3.1 | ||
-1e3 | ||
``` | ||
|
||
## Doesn't change numbers | ||
Given a sample.yml file of: | ||
```yaml | ||
- 3 | ||
- 3.1 | ||
- -1e3 | ||
``` | ||
then | ||
```bash | ||
yq '.[] | to_number' sample.yml | ||
``` | ||
will output | ||
```yaml | ||
3 | ||
3.1 | ||
-1e3 | ||
``` | ||
|
||
## Cannot convert null | ||
Running | ||
```bash | ||
yq --null-input '.a.b | to_number' | ||
``` | ||
will output | ||
```bash | ||
Error: cannot convert node value [null] at path a.b of tag !!null to number | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package yqlib | ||
|
||
import ( | ||
"container/list" | ||
"fmt" | ||
"strconv" | ||
|
||
yaml "gopkg.in/yaml.v3" | ||
) | ||
|
||
func tryConvertToNumber(value string) (string, bool) { | ||
// try a int first | ||
_, _, err := parseInt64(value) | ||
if err == nil { | ||
return "!!int", true | ||
} | ||
// try float | ||
_, floatErr := strconv.ParseFloat(value, 64) | ||
|
||
if floatErr == nil { | ||
return "!!float", true | ||
} | ||
return "", false | ||
|
||
} | ||
|
||
func toNumberOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) { | ||
log.Debugf("ToNumberOperator") | ||
|
||
var results = list.New() | ||
|
||
for el := context.MatchingNodes.Front(); el != nil; el = el.Next() { | ||
candidate := el.Value.(*CandidateNode) | ||
if candidate.Node.Kind != yaml.ScalarNode { | ||
return Context{}, fmt.Errorf("cannot convert node at path %v of tag %v to number", candidate.GetNicePath(), candidate.GetNiceTag()) | ||
} | ||
|
||
if candidate.Node.Tag == "!!int" || candidate.Node.Tag == "!!float" { | ||
// it already is a number! | ||
results.PushBack(candidate) | ||
} else { | ||
tag, converted := tryConvertToNumber(candidate.Node.Value) | ||
if converted { | ||
node := &yaml.Node{Kind: yaml.ScalarNode, Value: candidate.Node.Value, Tag: tag} | ||
|
||
result := candidate.CreateReplacement(node) | ||
results.PushBack(result) | ||
} else { | ||
return Context{}, fmt.Errorf("cannot convert node value [%v] at path %v of tag %v to number", candidate.Node.Value, candidate.GetNicePath(), candidate.GetNiceTag()) | ||
} | ||
|
||
} | ||
} | ||
|
||
return context.ChildContext(results), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package yqlib | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
var toNumberScenarios = []expressionScenario{ | ||
{ | ||
description: "Converts strings to numbers", | ||
document: `["3", "3.1", "-1e3"]`, | ||
expression: `.[] | to_number`, | ||
expected: []string{ | ||
"D0, P[0], (!!int)::3\n", | ||
"D0, P[1], (!!float)::3.1\n", | ||
"D0, P[2], (!!float)::-1e3\n", | ||
}, | ||
}, | ||
{ | ||
skipDoc: true, | ||
description: "Converts strings to numbers, with tonumber because jq", | ||
document: `["3", "3.1", "-1e3"]`, | ||
expression: `.[] | tonumber`, | ||
expected: []string{ | ||
"D0, P[0], (!!int)::3\n", | ||
"D0, P[1], (!!float)::3.1\n", | ||
"D0, P[2], (!!float)::-1e3\n", | ||
}, | ||
}, | ||
{ | ||
description: "Doesn't change numbers", | ||
document: `[3, 3.1, -1e3]`, | ||
expression: `.[] | to_number`, | ||
expected: []string{ | ||
"D0, P[0], (!!int)::3\n", | ||
"D0, P[1], (!!float)::3.1\n", | ||
"D0, P[2], (!!float)::-1e3\n", | ||
}, | ||
}, | ||
{ | ||
description: "Cannot convert null", | ||
expression: `.a.b | to_number`, | ||
expectedError: "cannot convert node value [null] at path a.b of tag !!null to number", | ||
}, | ||
} | ||
|
||
func TestToNumberOperatorScenarios(t *testing.T) { | ||
for _, tt := range toNumberScenarios { | ||
testScenario(t, &tt) | ||
} | ||
documentOperatorScenarios(t, "to_number", toNumberScenarios) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -249,3 +249,4 @@ yamld | |
yqlib | ||
yuin | ||
zabbix | ||
tonumber |