Added tonumber support #71

This commit is contained in:
Mike Farah 2023-10-05 15:13:46 +11:00
parent 6e65d44a98
commit d113344abf
7 changed files with 161 additions and 0 deletions

View File

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

View File

@ -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
```

View File

@ -34,6 +34,7 @@ var participleYqRules = []*participleYqRule{
simpleOp("line", lineOpType), simpleOp("line", lineOpType),
simpleOp("column", columnOpType), simpleOp("column", columnOpType),
simpleOp("eval", evalOpType), simpleOp("eval", evalOpType),
simpleOp("to_?number", toNumberOpType),
{"MapValues", `map_?values`, opToken(mapValuesOpType), 0}, {"MapValues", `map_?values`, opToken(mapValuesOpType), 0},
simpleOp("map", mapOpType), simpleOp("map", mapOpType),

View File

@ -167,6 +167,7 @@ var valueOpType = &operationType{Type: "VALUE", NumArgs: 0, Precedence: 50, Hand
var referenceOpType = &operationType{Type: "REF", NumArgs: 0, Precedence: 50, Handler: referenceOperator} var referenceOpType = &operationType{Type: "REF", NumArgs: 0, Precedence: 50, Handler: referenceOperator}
var envOpType = &operationType{Type: "ENV", NumArgs: 0, Precedence: 50, Handler: envOperator} var envOpType = &operationType{Type: "ENV", NumArgs: 0, Precedence: 50, Handler: envOperator}
var notOpType = &operationType{Type: "NOT", NumArgs: 0, Precedence: 50, Handler: notOperator} var notOpType = &operationType{Type: "NOT", NumArgs: 0, Precedence: 50, Handler: notOperator}
var toNumberOpType = &operationType{Type: "TO_NUMBER", NumArgs: 0, Precedence: 50, Handler: toNumberOperator}
var emptyOpType = &operationType{Type: "EMPTY", Precedence: 50, Handler: emptyOperator} var emptyOpType = &operationType{Type: "EMPTY", Precedence: 50, Handler: emptyOperator}
var envsubstOpType = &operationType{Type: "ENVSUBST", NumArgs: 0, Precedence: 50, Handler: envsubstOperator} var envsubstOpType = &operationType{Type: "ENVSUBST", NumArgs: 0, Precedence: 50, Handler: envsubstOperator}

View File

@ -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
}

View File

@ -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)
}

View File

@ -249,3 +249,4 @@ yamld
yqlib yqlib
yuin yuin
zabbix zabbix
tonumber