Skip to content

Commit

Permalink
Added tonumber support #71
Browse files Browse the repository at this point in the history
  • Loading branch information
mikefarah committed Oct 5, 2023
1 parent 6e65d44 commit d113344
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pkg/yqlib/doc/operators/headers/to_number.md
@@ -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.
49 changes: 49 additions & 0 deletions pkg/yqlib/doc/operators/to_number.md
@@ -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
```

1 change: 1 addition & 0 deletions pkg/yqlib/lexer_participle.go
Expand Up @@ -34,6 +34,7 @@ var participleYqRules = []*participleYqRule{
simpleOp("line", lineOpType),
simpleOp("column", columnOpType),
simpleOp("eval", evalOpType),
simpleOp("to_?number", toNumberOpType),

{"MapValues", `map_?values`, opToken(mapValuesOpType), 0},
simpleOp("map", mapOpType),
Expand Down
1 change: 1 addition & 0 deletions pkg/yqlib/lib.go
Expand Up @@ -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 envOpType = &operationType{Type: "ENV", NumArgs: 0, Precedence: 50, Handler: envOperator}
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 envsubstOpType = &operationType{Type: "ENVSUBST", NumArgs: 0, Precedence: 50, Handler: envsubstOperator}
Expand Down
56 changes: 56 additions & 0 deletions pkg/yqlib/operator_to_number.go
@@ -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
}
51 changes: 51 additions & 0 deletions pkg/yqlib/operator_to_number_test.go
@@ -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)
}
1 change: 1 addition & 0 deletions project-words.txt
Expand Up @@ -249,3 +249,4 @@ yamld
yqlib
yuin
zabbix
tonumber

0 comments on commit d113344

Please sign in to comment.