Skip to content

Commit

Permalink
Support env[] expression #2071
Browse files Browse the repository at this point in the history
  • Loading branch information
mikefarah committed Jun 15, 2024
1 parent 7bad2f4 commit d9af2dd
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pkg/yqlib/lexer_participle.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ func envOp(strenv bool) yqAction {
envOperation.OperationType = envOpType
envOperation.Preferences = preferences

return &token{TokenType: operationToken, Operation: envOperation}, nil
return &token{TokenType: operationToken, Operation: envOperation, CheckForPostTraverse: envOpType.CheckForPostTraverse}, nil
}
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/yqlib/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ var stringInterpolationOpType = &operationType{Type: "STRING_INT", NumArgs: 0, P
var loadOpType = &operationType{Type: "LOAD", NumArgs: 1, Precedence: 52, Handler: loadOperator}
var loadStringOpType = &operationType{Type: "LOAD_STRING", NumArgs: 1, Precedence: 52, Handler: loadStringOperator}

var keysOpType = &operationType{Type: "KEYS", NumArgs: 0, Precedence: 50, Handler: keysOperator}
var keysOpType = &operationType{Type: "KEYS", NumArgs: 0, Precedence: 52, Handler: keysOperator, CheckForPostTraverse: true}

var collectObjectOpType = &operationType{Type: "COLLECT_OBJECT", NumArgs: 0, Precedence: 50, Handler: collectObjectOperator}

Expand All @@ -175,7 +175,7 @@ var traverseArrayOpType = &operationType{Type: "TRAVERSE_ARRAY", NumArgs: 2, Pre
var selfReferenceOpType = &operationType{Type: "SELF", NumArgs: 0, Precedence: 55, Handler: selfOperator}
var valueOpType = &operationType{Type: "VALUE", NumArgs: 0, Precedence: 50, Handler: valueOperator, ToString: valueToStringFunc}
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: 52, Handler: envOperator, CheckForPostTraverse: true}
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}
Expand Down
10 changes: 10 additions & 0 deletions pkg/yqlib/operator_env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ import (
)

var envOperatorScenarios = []expressionScenario{
{
description: "Read string environment variable",
skipDoc: true,
environmentVariables: map[string]string{"myenv": "[cat,dog]"},
expression: `env(myenv)[]`,
expected: []string{
"D0, P[0], (!!str)::cat\n",
"D0, P[1], (!!str)::dog\n",
},
},
{
description: "Read string environment variable",
environmentVariables: map[string]string{"myenv": "cat meow"},
Expand Down
10 changes: 10 additions & 0 deletions pkg/yqlib/operator_keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ var keysOperatorScenarios = []expressionScenario{
"D0, P[], (!!seq)::- dog\n- cat\n",
},
},
{
description: "Map keys with splat",
skipDoc: true,
document: `{dog: woof, cat: meow}`,
expression: `keys[]`,
expected: []string{
"D0, P[dog], (!!str)::dog\n",
"D0, P[cat], (!!str)::cat\n",
},
},
{
skipDoc: true,
document: `{}`,
Expand Down

0 comments on commit d9af2dd

Please sign in to comment.