From b8e688c5be4b4bae46fdc2a4ade22324afee885e Mon Sep 17 00:00:00 2001 From: Martin Sustrik Date: Sun, 30 May 2021 07:27:55 +0200 Subject: [PATCH 1/2] Call MarshalYAML on embedded structures --- encode.go | 16 ++++++++++++++++ encode_test.go | 18 ++++++++++++++++-- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/encode.go b/encode.go index 0ee738e1..c789c9cc 100644 --- a/encode.go +++ b/encode.go @@ -100,6 +100,22 @@ func (e *encoder) marshal(tag string, in reflect.Value) { e.nilv() return } + // In case of embedded fields, let's get a poiter etc. + if in.CanAddr() { + m, ok := in.Addr().Interface().(Marshaler) + if ok { + v, err := m.MarshalYAML() + if err != nil { + fail(err) + } + if v == nil { + e.nilv() + return + } + in = reflect.ValueOf(v) + } + } + iface := in.Interface() switch m := iface.(type) { case jsonNumber: diff --git a/encode_test.go b/encode_test.go index c8caedc5..5693739f 100644 --- a/encode_test.go +++ b/encode_test.go @@ -35,10 +35,24 @@ func (j jsonNumberT) String() string { var marshalIntTest = 123 +type Outer struct { + Inner Inner +} + +type Inner struct{} + +func (i *Inner) MarshalYAML() (interface{}, error) { + return "foo", nil +} + var marshalTests = []struct { value interface{} data string }{ + { + &Outer{}, + "inner: foo\n", + }, { nil, "null\n", @@ -406,8 +420,8 @@ func (s *S) TestLineWrapping(c *C) { data, err := yaml.Marshal(v) c.Assert(err, IsNil) c.Assert(string(data), Equals, - "a: 'abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890 abcdefghijklmnopqrstuvwxyz\n" + - " ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890 '\n") + "a: 'abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890 abcdefghijklmnopqrstuvwxyz\n"+ + " ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890 '\n") // The API does not allow this process to be reversed as it's intended // for migration only. v3 drops this method and instead offers more From 0514f13498deec7a96944c9b9a937786f4f57421 Mon Sep 17 00:00:00 2001 From: Martin Sustrik Date: Sun, 30 May 2021 07:52:16 +0200 Subject: [PATCH 2/2] change package name for the fork --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 2cbb85ae..f8fca74a 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module gopkg.in/yaml.v2 +module github.com/sustrik/yaml go 1.15