Open
Description
i have a file with alot of anchors that is seperated because its needed by alot of files. btw that feature why i use this awesome library. now i need to add an import functionality to my application. that means i read my struct that contains a slice of structs and add a new item to it... which also contains an alias to some other struct. i cant find a way to solve this with this lib. it would create a file with no alias structs or it fails with
cannot find anchor name from pointer address for automatically alias detection:
github.com/goccy/go-yaml.(*Encoder).encodeStruct
/home/marv/tmp/ytest/vendor/github.com/goccy/go-yaml/encode.go:652
exit status 1
i need to encode to a yaml that still can be used with the other yaml file with the anchors, as used with yaml.ReferenceDir
on decoding.
something like this, that it just ignores the anchor objects:
package main
import (
"log"
"os"
"github.com/goccy/go-yaml"
"github.com/goccy/go-yaml/ast"
)
type Application struct {
Name string
}
type Item struct {
Name string
Application *Application `yaml:",alias"`
}
func main() {
a := Application{Name: "foo"}
i := Item{
Name: "bar",
Application: &a,
}
if err := yaml.NewEncoder(
os.Stdout,
yaml.MarshalAnchor(
func(anchor *ast.AnchorNode, value interface{}) error {
v, ok := value.(*Application)
if ok {
nameNode := anchor.Name.(*ast.StringNode) //nolint:forcetypeassert
nameNode.Value = v.Name
}
return nil
},
),
).Encode(i); err != nil {
log.Fatal(err)
}
}