Fixed to_entries[]

This commit is contained in:
Mike Farah 2023-12-12 15:44:34 +11:00
parent eff7d99e2b
commit 4cf123fed5
5 changed files with 702 additions and 639 deletions

View File

@ -298,7 +298,7 @@ func opTokenWithPrefs(opType *operationType, assignOpType *operationType, prefer
if assignOpType != nil { if assignOpType != nil {
assign = &Operation{OperationType: assignOpType, Value: assignOpType.Type, StringValue: value, Preferences: preferences} assign = &Operation{OperationType: assignOpType, Value: assignOpType.Type, StringValue: value, Preferences: preferences}
} }
return &token{TokenType: operationToken, Operation: op, AssignOperation: assign}, nil return &token{TokenType: operationToken, Operation: op, AssignOperation: assign, CheckForPostTraverse: op.OperationType.CheckForPostTraverse}, nil
} }
} }

File diff suppressed because it is too large Load Diff

View File

@ -33,6 +33,7 @@ type operationType struct {
NumArgs uint // number of arguments to the op NumArgs uint // number of arguments to the op
Precedence uint Precedence uint
Handler operatorHandler Handler operatorHandler
CheckForPostTraverse bool
} }
var orOpType = &operationType{Type: "OR", NumArgs: 2, Precedence: 20, Handler: orOperator} var orOpType = &operationType{Type: "OR", NumArgs: 2, Precedence: 20, Handler: orOperator}
@ -108,7 +109,7 @@ var containsOpType = &operationType{Type: "CONTAINS", NumArgs: 1, Precedence: 50
var anyConditionOpType = &operationType{Type: "ANY_CONDITION", NumArgs: 1, Precedence: 50, Handler: anyOperator} var anyConditionOpType = &operationType{Type: "ANY_CONDITION", NumArgs: 1, Precedence: 50, Handler: anyOperator}
var allConditionOpType = &operationType{Type: "ALL_CONDITION", NumArgs: 1, Precedence: 50, Handler: allOperator} var allConditionOpType = &operationType{Type: "ALL_CONDITION", NumArgs: 1, Precedence: 50, Handler: allOperator}
var toEntriesOpType = &operationType{Type: "TO_ENTRIES", NumArgs: 0, Precedence: 50, Handler: toEntriesOperator} var toEntriesOpType = &operationType{Type: "TO_ENTRIES", NumArgs: 0, Precedence: 52, Handler: toEntriesOperator, CheckForPostTraverse: true}
var fromEntriesOpType = &operationType{Type: "FROM_ENTRIES", NumArgs: 0, Precedence: 50, Handler: fromEntriesOperator} var fromEntriesOpType = &operationType{Type: "FROM_ENTRIES", NumArgs: 0, Precedence: 50, Handler: fromEntriesOperator}
var withEntriesOpType = &operationType{Type: "WITH_ENTRIES", NumArgs: 1, Precedence: 50, Handler: withEntriesOperator} var withEntriesOpType = &operationType{Type: "WITH_ENTRIES", NumArgs: 1, Precedence: 50, Handler: withEntriesOperator}
@ -152,7 +153,7 @@ var splitStringOpType = &operationType{Type: "SPLIT", NumArgs: 1, Precedence: 50
var changeCaseOpType = &operationType{Type: "CHANGE_CASE", NumArgs: 0, Precedence: 50, Handler: changeCaseOperator} var changeCaseOpType = &operationType{Type: "CHANGE_CASE", NumArgs: 0, Precedence: 50, Handler: changeCaseOperator}
var trimOpType = &operationType{Type: "TRIM", NumArgs: 0, Precedence: 50, Handler: trimSpaceOperator} var trimOpType = &operationType{Type: "TRIM", NumArgs: 0, Precedence: 50, Handler: trimSpaceOperator}
var loadOpType = &operationType{Type: "LOAD", NumArgs: 1, Precedence: 50, Handler: loadYamlOperator} var loadOpType = &operationType{Type: "LOAD", NumArgs: 1, Precedence: 52, Handler: loadYamlOperator}
var keysOpType = &operationType{Type: "KEYS", NumArgs: 0, Precedence: 50, Handler: keysOperator} var keysOpType = &operationType{Type: "KEYS", NumArgs: 0, Precedence: 50, Handler: keysOperator}

View File

@ -5,6 +5,16 @@ import (
) )
var entriesOperatorScenarios = []expressionScenario{ var entriesOperatorScenarios = []expressionScenario{
{
description: "to_entries splat",
skipDoc: true,
document: `{a: 1, b: 2}`,
expression: `to_entries[]`,
expected: []string{
"D0, P[0], (!!map)::key: a\nvalue: 1\n",
"D0, P[1], (!!map)::key: b\nvalue: 2\n",
},
},
{ {
description: "to_entries, delete key", description: "to_entries, delete key",
skipDoc: true, skipDoc: true,

View File

@ -13,6 +13,22 @@ var loadScenarios = []expressionScenario{
"D0, P[], (!!null)::# comment\n", "D0, P[], (!!null)::# comment\n",
}, },
}, },
{
skipDoc: true,
description: "Load and splat",
expression: `load("../../examples/small.yaml")[]`,
expected: []string{
"D0, P[a], (!!str)::cat\n",
},
},
{
skipDoc: true,
description: "Load and traverse",
expression: `load("../../examples/small.yaml").a`,
expected: []string{
"D0, P[a], (!!str)::cat\n",
},
},
{ {
skipDoc: true, skipDoc: true,
description: "Load file with a header comment into an array", description: "Load file with a header comment into an array",