Repository: goccy/go-yaml (go)

Issue:
“cannot find anchor by alias name”
I'm working with `github.com/goccy/go-yaml` because I'm interested in loading a YAML config file that includes YAML 1.2 anchors, aliases, and extensions. For instance, the YAML below shows what I'm hoping to load. Having the ability to replicate certain fields like this will reduce a great deal of configuration duplication.

```
%YAML 1.2
---
foo:
 <<: &test-anchor
   K1: "One"
 K2: "Two"

bar:
 <<: *test-anchor
 K3: "Three"
```
Given everything that this package can do I assume it's possible to achieve this. However, when I `Unmarshal()` YAML such as above I receive the error: “cannot find anchor by alias name test-anchor.”

The YAML processes correctly with other tools so I believe I have the syntax correct.

Below is simple test code for exercising the above. In this test code I'm attempting to demonstrate processing the aliases in the YAML blurb by unmarshaling it and then marshaling the resulting structure as printable YAML again.

Am I missing something in my YAML? Have I misunderstood the abilities of `goccy/go-yaml` or used the incorrect configuration?

```
package main

import (
	"fmt"
	"github.com/goccy/go-yaml"
)

func main() {
	type foo struct {
		K1 string
		K2 string
	}

	type bar struct {
		K1 string
		K3 string
	}

	type doc struct {
		Foo foo
		Bar bar
	}

	yml := `
%YAML 1.2
---
foo:
 <<: &test-anchor
   K1: "One"
 K2: "Two"

bar:
 <<: *test-anchor
 K3: "Three"
`
	var v doc
	var err error
	if err = yaml.Unmarshal([]byte(yml), &v); err != nil {
		fmt.Println(err.Error())
	} else {
		bytes, err := yaml.Marshal(v)
		if err != nil {
			fmt.Println(err.Error())
		} else {
			fmt.Println(string(bytes))
		}
	}
}
```


Pull request description:
Add roundtrip test for anchor and alias
ref #211 

Patch:
diff --git a/decode.go b/decode.go
index 5246953..45d3044 100644
--- a/decode.go
+++ b/decode.go
@@ -127,6 +127,9 @@ func (d *Decoder) setToMapValue(node ast.Node, m map[string]interface{}) {
 		for _, value := range n.Values {
 			d.setToMapValue(value, m)
 		}
+	case *ast.AnchorNode:
+		anchorName := n.Name.GetToken().Value
+		d.anchorNodeMap[anchorName] = n.Value
 	}
 }
 


Test patch:
diff --git a/decode_test.go b/decode_test.go
index 9ea9ce2..7d5960f 100644
--- a/decode_test.go
+++ b/decode_test.go
@@ -2357,3 +2357,92 @@ func TestDecoder_DecodeWithNode(t *testing.T) {
 		}
 	})
 }
+
+func TestRoundtripAnchorAlias(t *testing.T) {
+	t.Run("irreversible", func(t *testing.T) {
+		type foo struct {
+			K1 string
+			K2 string
+		}
+
+		type bar struct {
+			K1 string
+			K3 string
+		}
+
+		type doc struct {
+			Foo foo
+			Bar bar
+		}
+		yml := `
+foo:
+ <<: &test-anchor
+   k1: "One"
+ k2: "Two"
+
+bar:
+ <<: *test-anchor
+ k3: "Three"
+`
+		var v doc
+		if err := yaml.Unmarshal([]byte(yml), &v); err != nil {
+			t.Fatalf("%+v", err)
+		}
+		bytes, err := yaml.Marshal(v)
+		if err != nil {
+			t.Fatalf("%+v", err)
+		}
+		expected := `
+foo:
+  k1: One
+  k2: Two
+bar:
+  k1: One
+  k3: Three
+`
+		got := "\n" + string(bytes)
+		if expected != got {
+			t.Fatalf("expected:[%s] but got [%s]", expected, got)
+		}
+	})
+	t.Run("reversible", func(t *testing.T) {
+		type TestAnchor struct {
+			K1 string
+		}
+		type foo struct {
+			*TestAnchor `yaml:",inline,alias"`
+			K2          string
+		}
+		type bar struct {
+			*TestAnchor `yaml:",inline,alias"`
+			K3          string
+		}
+		type doc struct {
+			TestAnchor *TestAnchor `yaml:"test-anchor,anchor"`
+			Foo        foo
+			Bar        bar
+		}
+		yml := `
+test-anchor: &test-anchor
+  k1: One
+foo:
+  <<: *test-anchor
+  k2: Two
+bar:
+  <<: *test-anchor
+  k3: Three
+`
+		var v doc
+		if err := yaml.Unmarshal([]byte(yml), &v); err != nil {
+			t.Fatalf("%+v", err)
+		}
+		bytes, err := yaml.Marshal(v)
+		if err != nil {
+			t.Fatalf("%+v", err)
+		}
+		got := "\n" + string(bytes)
+		if yml != got {
+			t.Fatalf("expected:[%s] but got [%s]", yml, got)
+		}
+	})
+}