mirror of
https://github.com/mikefarah/yq.git
synced 2024-12-19 20:19:04 +00:00
wip
This commit is contained in:
parent
018a6e616d
commit
2d9cc3c107
32
pkg/yqlib/operator_env.go
Normal file
32
pkg/yqlib/operator_env.go
Normal file
@ -0,0 +1,32 @@
|
||||
package yqlib
|
||||
|
||||
import (
|
||||
"container/list"
|
||||
"os"
|
||||
|
||||
yaml "gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
// type EnvOpPreferences struct {
|
||||
// StringValue bool
|
||||
// }
|
||||
|
||||
func EnvOperator(d *dataTreeNavigator, matchMap *list.List, pathNode *PathTreeNode) (*list.List, error) {
|
||||
envName := pathNode.Operation.CandidateNode.Node.Value
|
||||
log.Debug("EnvOperator, env name:", envName)
|
||||
|
||||
rawValue := os.Getenv(envName)
|
||||
|
||||
target := &CandidateNode{
|
||||
Path: make([]interface{}, 0),
|
||||
Document: 0,
|
||||
Filename: "",
|
||||
Node: &yaml.Node{
|
||||
Kind: yaml.ScalarNode,
|
||||
Tag: "!!str",
|
||||
Value: rawValue,
|
||||
},
|
||||
}
|
||||
|
||||
return nodeToMap(target), nil
|
||||
}
|
100
pkg/yqlib/operator_env_test.go
Normal file
100
pkg/yqlib/operator_env_test.go
Normal file
@ -0,0 +1,100 @@
|
||||
package yqlib
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
var envOperatorScenarios = []expressionScenario{
|
||||
{
|
||||
document: ``,
|
||||
expression: `1`,
|
||||
expected: []string{
|
||||
"D0, P[], (!!int)::1\n",
|
||||
},
|
||||
},
|
||||
{
|
||||
document: ``,
|
||||
expression: `-1`,
|
||||
expected: []string{
|
||||
"D0, P[], (!!int)::-1\n",
|
||||
},
|
||||
}, {
|
||||
document: ``,
|
||||
expression: `1.2`,
|
||||
expected: []string{
|
||||
"D0, P[], (!!float)::1.2\n",
|
||||
},
|
||||
}, {
|
||||
document: ``,
|
||||
expression: `-5.2e11`,
|
||||
expected: []string{
|
||||
"D0, P[], (!!float)::-5.2e11\n",
|
||||
},
|
||||
}, {
|
||||
document: ``,
|
||||
expression: `5e-10`,
|
||||
expected: []string{
|
||||
"D0, P[], (!!float)::5e-10\n",
|
||||
},
|
||||
},
|
||||
{
|
||||
document: ``,
|
||||
expression: `"cat"`,
|
||||
expected: []string{
|
||||
"D0, P[], (!!str)::cat\n",
|
||||
},
|
||||
},
|
||||
{
|
||||
document: ``,
|
||||
expression: `"frog jumps"`,
|
||||
expected: []string{
|
||||
"D0, P[], (!!str)::frog jumps\n",
|
||||
},
|
||||
},
|
||||
{
|
||||
document: ``,
|
||||
expression: `"1.3"`,
|
||||
expected: []string{
|
||||
"D0, P[], (!!str)::\"1.3\"\n",
|
||||
},
|
||||
}, {
|
||||
document: ``,
|
||||
expression: `"true"`,
|
||||
expected: []string{
|
||||
"D0, P[], (!!str)::\"true\"\n",
|
||||
},
|
||||
}, {
|
||||
document: ``,
|
||||
expression: `true`,
|
||||
expected: []string{
|
||||
"D0, P[], (!!bool)::true\n",
|
||||
},
|
||||
}, {
|
||||
document: ``,
|
||||
expression: `false`,
|
||||
expected: []string{
|
||||
"D0, P[], (!!bool)::false\n",
|
||||
},
|
||||
},
|
||||
{
|
||||
document: ``,
|
||||
expression: `Null`,
|
||||
expected: []string{
|
||||
"D0, P[], (!!null)::Null\n",
|
||||
},
|
||||
},
|
||||
{
|
||||
document: ``,
|
||||
expression: `~`,
|
||||
expected: []string{
|
||||
"D0, P[], (!!null)::~\n",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
func TestEnvOperatorScenarios(t *testing.T) {
|
||||
for _, tt := range envOperatorScenarios {
|
||||
testScenario(t, &tt)
|
||||
}
|
||||
documentScenarios(t, "Env Variable Operators", addOperatorScenarios)
|
||||
}
|
Loading…
Reference in New Issue
Block a user