Skip to content

Commit

Permalink
Merge pull request #130 from goccy/feature/resolve-alias-before-unmar…
Browse files Browse the repository at this point in the history
…shal-yaml

Fix processing of MergeKey and Alias at decoding
  • Loading branch information
goccy authored Jun 11, 2020
2 parents 015269f + a56d78c commit ea7dd73
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
40 changes: 28 additions & 12 deletions decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,15 @@ func (d *Decoder) castToFloat(v interface{}) interface{} {
func (d *Decoder) setToMapValue(node ast.Node, m map[string]interface{}) {
switch n := node.(type) {
case *ast.MappingValueNode:
if n.Key.Type() == ast.MergeKeyType && n.Value.Type() == ast.AliasType {
aliasNode := n.Value.(*ast.AliasNode)
aliasName := aliasNode.Value.GetToken().Value
node := d.anchorNodeMap[aliasName]
d.setToMapValue(node, m)
if n.Key.Type() == ast.MergeKeyType {
if n.Value.Type() == ast.AliasType {
aliasNode := n.Value.(*ast.AliasNode)
aliasName := aliasNode.Value.GetToken().Value
node := d.anchorNodeMap[aliasName]
d.setToMapValue(node, m)
} else {
d.setToMapValue(n.Value, m)
}
} else {
key := n.Key.GetToken().Value
m[key] = d.nodeToValue(n.Value)
Expand All @@ -114,11 +118,15 @@ func (d *Decoder) setToMapValue(node ast.Node, m map[string]interface{}) {
func (d *Decoder) setToOrderedMapValue(node ast.Node, m *MapSlice) {
switch n := node.(type) {
case *ast.MappingValueNode:
if n.Key.Type() == ast.MergeKeyType && n.Value.Type() == ast.AliasType {
aliasNode := n.Value.(*ast.AliasNode)
aliasName := aliasNode.Value.GetToken().Value
node := d.anchorNodeMap[aliasName]
d.setToOrderedMapValue(node, m)
if n.Key.Type() == ast.MergeKeyType {
if n.Value.Type() == ast.AliasType {
aliasNode := n.Value.(*ast.AliasNode)
aliasName := aliasNode.Value.GetToken().Value
node := d.anchorNodeMap[aliasName]
d.setToOrderedMapValue(node, m)
} else {
d.setToOrderedMapValue(n.Value, m)
}
} else {
key := n.Key.GetToken().Value
*m = append(*m, MapItem{Key: key, Value: d.nodeToValue(n.Value)})
Expand Down Expand Up @@ -221,8 +229,16 @@ func (d *Decoder) resolveAlias(node ast.Node) ast.Node {
n.Values[idx] = d.resolveAlias(value).(*ast.MappingValueNode)
}
case *ast.MappingValueNode:
n.Key = d.resolveAlias(n.Key)
n.Value = d.resolveAlias(n.Value)
if n.Key.Type() == ast.MergeKeyType && n.Value.Type() == ast.AliasType {
value := d.resolveAlias(n.Value)
keyColumn := n.Key.GetToken().Position.Column
requiredColumn := keyColumn + 2
value.AddColumn(requiredColumn)
n.Value = value
} else {
n.Key = d.resolveAlias(n.Key)
n.Value = d.resolveAlias(n.Value)
}
case *ast.SequenceNode:
for idx, value := range n.Values {
n.Values[idx] = d.resolveAlias(value)
Expand Down
4 changes: 2 additions & 2 deletions decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1903,8 +1903,8 @@ func TestDecoder_UnmarshalYAMLWithAlias(t *testing.T) {
anchors:
x: &x hello
map: &y
a: b
c: d
a: b
c: d
a: *x
b:
<<: *y
Expand Down

0 comments on commit ea7dd73

Please sign in to comment.