From 749611fa9fcc816e41c2b7ca2aae7a71fa87fccd Mon Sep 17 00:00:00 2001 From: Gustavo Niemeyer Date: Thu, 7 Jan 2021 17:22:59 +0000 Subject: [PATCH] Allow encoding Node by value (#673). --- encode.go | 5 +++++ encode_test.go | 8 ++++++++ 2 files changed, 13 insertions(+) diff --git a/encode.go b/encode.go index 45e8d1e..de9e72a 100644 --- a/encode.go +++ b/encode.go @@ -120,6 +120,11 @@ func (e *encoder) marshal(tag string, in reflect.Value) { e.nodev(in) return case Node: + if !in.CanAddr() { + var n = reflect.New(in.Type()).Elem() + n.Set(in) + in = n + } e.nodev(in.Addr()) return case time.Time: diff --git a/encode_test.go b/encode_test.go index 978d4d6..4a8bf2e 100644 --- a/encode_test.go +++ b/encode_test.go @@ -448,6 +448,14 @@ var marshalTests = []struct { }, }, "value: 'foo'\n", + }, { + yaml.Node{ + Kind: yaml.ScalarNode, + Tag: "!!str", + Value: "foo", + Style: yaml.SingleQuotedStyle, + }, + "'foo'\n", }, // Enforced tagging with shorthand notation (issue #616).