mirror of
https://github.com/mikefarah/yq.git
synced 2025-01-26 16:35:38 +00:00
Fixed Toml decoding when table array defined before parent #1922
This commit is contained in:
parent
c32a9ceab8
commit
d9e1a789ea
@ -26,6 +26,17 @@ func (d *dataTreeNavigator) DeeplyAssign(context Context, path []interface{}, rh
|
|||||||
|
|
||||||
assignmentOp := &Operation{OperationType: assignOpType, Preferences: assignPreferences{}}
|
assignmentOp := &Operation{OperationType: assignOpType, Preferences: assignPreferences{}}
|
||||||
|
|
||||||
|
if rhsCandidateNode.Kind == MappingNode {
|
||||||
|
log.Debug("[DataTreeNavigator] DeeplyAssign: deeply merging object")
|
||||||
|
// if the rhs is a map, we need to deeply merge it in.
|
||||||
|
// otherwise we'll clobber any existing fields
|
||||||
|
assignmentOp = &Operation{OperationType: multiplyAssignOpType, Preferences: multiplyPreferences{
|
||||||
|
AppendArrays: true,
|
||||||
|
TraversePrefs: traversePreferences{DontFollowAlias: true},
|
||||||
|
AssignPrefs: assignPreferences{},
|
||||||
|
}}
|
||||||
|
}
|
||||||
|
|
||||||
rhsOp := &Operation{OperationType: valueOpType, CandidateNode: rhsCandidateNode}
|
rhsOp := &Operation{OperationType: valueOpType, CandidateNode: rhsCandidateNode}
|
||||||
|
|
||||||
assignmentOpNode := &ExpressionNode{
|
assignmentOpNode := &ExpressionNode{
|
||||||
|
@ -56,7 +56,7 @@ func (dec *tomlDecoder) getFullPath(tomlNode *toml.Node) []interface{} {
|
|||||||
func (dec *tomlDecoder) processKeyValueIntoMap(rootMap *CandidateNode, tomlNode *toml.Node) error {
|
func (dec *tomlDecoder) processKeyValueIntoMap(rootMap *CandidateNode, tomlNode *toml.Node) error {
|
||||||
value := tomlNode.Value()
|
value := tomlNode.Value()
|
||||||
path := dec.getFullPath(value.Next())
|
path := dec.getFullPath(value.Next())
|
||||||
log.Debug("!!!processKeyValueIntoMap: %v", path)
|
log.Debug("[DecoderTOML] processKeyValueIntoMap: %v", path)
|
||||||
|
|
||||||
valueNode, err := dec.decodeNode(value)
|
valueNode, err := dec.decodeNode(value)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -70,14 +70,14 @@ func (dec *tomlDecoder) processKeyValueIntoMap(rootMap *CandidateNode, tomlNode
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (dec *tomlDecoder) decodeKeyValuesIntoMap(rootMap *CandidateNode, tomlNode *toml.Node) (bool, error) {
|
func (dec *tomlDecoder) decodeKeyValuesIntoMap(rootMap *CandidateNode, tomlNode *toml.Node) (bool, error) {
|
||||||
log.Debug("!! DECODE_KV_INTO_MAP -- processing first (current) entry")
|
log.Debug("[DecoderTOML] decodeKeyValuesIntoMap -- processing first (current) entry")
|
||||||
if err := dec.processKeyValueIntoMap(rootMap, tomlNode); err != nil {
|
if err := dec.processKeyValueIntoMap(rootMap, tomlNode); err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
|
|
||||||
for dec.parser.NextExpression() {
|
for dec.parser.NextExpression() {
|
||||||
nextItem := dec.parser.Expression()
|
nextItem := dec.parser.Expression()
|
||||||
log.Debug("!! DECODE_KV_INTO_MAP -- next exp, its a %v", nextItem.Kind)
|
log.Debug("[DecoderTOML] decodeKeyValuesIntoMap -- next exp, its a %v", nextItem.Kind)
|
||||||
|
|
||||||
if nextItem.Kind == toml.KeyValue {
|
if nextItem.Kind == toml.KeyValue {
|
||||||
if err := dec.processKeyValueIntoMap(rootMap, nextItem); err != nil {
|
if err := dec.processKeyValueIntoMap(rootMap, nextItem); err != nil {
|
||||||
@ -249,7 +249,7 @@ func (dec *tomlDecoder) Decode() (*CandidateNode, error) {
|
|||||||
func (dec *tomlDecoder) processTopLevelNode(currentNode *toml.Node) (bool, error) {
|
func (dec *tomlDecoder) processTopLevelNode(currentNode *toml.Node) (bool, error) {
|
||||||
var runAgainstCurrentExp bool
|
var runAgainstCurrentExp bool
|
||||||
var err error
|
var err error
|
||||||
log.Debug("!!!!!!!!!!!!Going to process %v state is current %v", currentNode.Kind, NodeToString(dec.rootMap))
|
log.Debug("[DecoderTOML] processTopLevelNode: Going to process %v state is current %v", currentNode.Kind, NodeToString(dec.rootMap))
|
||||||
if currentNode.Kind == toml.Table {
|
if currentNode.Kind == toml.Table {
|
||||||
runAgainstCurrentExp, err = dec.processTable(currentNode)
|
runAgainstCurrentExp, err = dec.processTable(currentNode)
|
||||||
} else if currentNode.Kind == toml.ArrayTable {
|
} else if currentNode.Kind == toml.ArrayTable {
|
||||||
@ -258,14 +258,14 @@ func (dec *tomlDecoder) processTopLevelNode(currentNode *toml.Node) (bool, error
|
|||||||
runAgainstCurrentExp, err = dec.decodeKeyValuesIntoMap(dec.rootMap, currentNode)
|
runAgainstCurrentExp, err = dec.decodeKeyValuesIntoMap(dec.rootMap, currentNode)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Debug("!!!!!!!!!!!!DONE Processing state is now %v", NodeToString(dec.rootMap))
|
log.Debug("[DecoderTOML] processTopLevelNode: DONE Processing state is now %v", NodeToString(dec.rootMap))
|
||||||
return runAgainstCurrentExp, err
|
return runAgainstCurrentExp, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dec *tomlDecoder) processTable(currentNode *toml.Node) (bool, error) {
|
func (dec *tomlDecoder) processTable(currentNode *toml.Node) (bool, error) {
|
||||||
log.Debug("!!! processing table")
|
log.Debug("[DecoderTOML] Enter processTable")
|
||||||
fullPath := dec.getFullPath(currentNode.Child())
|
fullPath := dec.getFullPath(currentNode.Child())
|
||||||
log.Debug("!!!fullpath: %v", fullPath)
|
log.Debug("[DecoderTOML] fullpath: %v", fullPath)
|
||||||
|
|
||||||
tableNodeValue := &CandidateNode{
|
tableNodeValue := &CandidateNode{
|
||||||
Kind: MappingNode,
|
Kind: MappingNode,
|
||||||
@ -302,6 +302,7 @@ func (dec *tomlDecoder) processTable(currentNode *toml.Node) (bool, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (dec *tomlDecoder) arrayAppend(context Context, path []interface{}, rhsNode *CandidateNode) error {
|
func (dec *tomlDecoder) arrayAppend(context Context, path []interface{}, rhsNode *CandidateNode) error {
|
||||||
|
log.Debug("[DecoderTOML] arrayAppend to path: %v,%v", path, NodeToString(rhsNode))
|
||||||
rhsCandidateNode := &CandidateNode{
|
rhsCandidateNode := &CandidateNode{
|
||||||
Kind: SequenceNode,
|
Kind: SequenceNode,
|
||||||
Tag: "!!seq",
|
Tag: "!!seq",
|
||||||
@ -323,9 +324,9 @@ func (dec *tomlDecoder) arrayAppend(context Context, path []interface{}, rhsNode
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (dec *tomlDecoder) processArrayTable(currentNode *toml.Node) (bool, error) {
|
func (dec *tomlDecoder) processArrayTable(currentNode *toml.Node) (bool, error) {
|
||||||
log.Debug("!!! processing table")
|
log.Debug("[DecoderTOML] Entering processArrayTable")
|
||||||
fullPath := dec.getFullPath(currentNode.Child())
|
fullPath := dec.getFullPath(currentNode.Child())
|
||||||
log.Debug("!!!fullpath: %v", fullPath)
|
log.Debug("[DecoderTOML] Fullpath: %v", fullPath)
|
||||||
|
|
||||||
// need to use the array append exp to add another entry to
|
// need to use the array append exp to add another entry to
|
||||||
// this array: fullpath += [ thing ]
|
// this array: fullpath += [ thing ]
|
||||||
|
@ -32,7 +32,7 @@ type expressionScenario struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestMain(m *testing.M) {
|
func TestMain(m *testing.M) {
|
||||||
logging.SetLevel(logging.ERROR, "")
|
logging.SetLevel(logging.DEBUG, "")
|
||||||
Now = func() time.Time {
|
Now = func() time.Time {
|
||||||
return time.Date(2021, time.May, 19, 1, 2, 3, 4, time.UTC)
|
return time.Date(2021, time.May, 19, 1, 2, 3, 4, time.UTC)
|
||||||
}
|
}
|
||||||
|
@ -16,6 +16,19 @@ name = "Tom Preston-Werner"
|
|||||||
age = 36
|
age = 36
|
||||||
`
|
`
|
||||||
|
|
||||||
|
var tableArrayBeforeOwners = `
|
||||||
|
[[owner.addresses]]
|
||||||
|
street = "first street"
|
||||||
|
|
||||||
|
[owner]
|
||||||
|
name = "Tom Preston-Werner"
|
||||||
|
`
|
||||||
|
var expectedTableArrayBeforeOwners = `owner:
|
||||||
|
addresses:
|
||||||
|
- street: first street
|
||||||
|
name: Tom Preston-Werner
|
||||||
|
`
|
||||||
|
|
||||||
var sampleTableExpected = `var: x
|
var sampleTableExpected = `var: x
|
||||||
owner:
|
owner:
|
||||||
contact:
|
contact:
|
||||||
@ -74,6 +87,13 @@ var tomlScenarios = []formatScenario{
|
|||||||
expected: "",
|
expected: "",
|
||||||
scenarioType: "decode",
|
scenarioType: "decode",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
skipDoc: true,
|
||||||
|
description: "table array before owners",
|
||||||
|
input: tableArrayBeforeOwners,
|
||||||
|
expected: expectedTableArrayBeforeOwners,
|
||||||
|
scenarioType: "decode",
|
||||||
|
},
|
||||||
{
|
{
|
||||||
skipDoc: true,
|
skipDoc: true,
|
||||||
description: "datetime",
|
description: "datetime",
|
||||||
|
Loading…
Reference in New Issue
Block a user