From 5ad613b21aaae4e5937bb281b7164d40d889aa3b Mon Sep 17 00:00:00 2001 From: Antoine Toulme Date: Tue, 2 Apr 2024 17:14:20 -0700 Subject: [PATCH] add TestRecursiveUnmarshaling --- confmap/confmap_test.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/confmap/confmap_test.go b/confmap/confmap_test.go index 3e7552a207d..661c3fe550a 100644 --- a/confmap/confmap_test.go +++ b/confmap/confmap_test.go @@ -759,3 +759,27 @@ func TestUnmarshalOwnThroughEmbeddedSquashedStruct(t *testing.T) { require.Equal(t, "success", cfg.Cfg.EmbeddedStructWithUnmarshal.success) require.Equal(t, "bar", cfg.Cfg.EmbeddedStructWithUnmarshal.Foo) } + +type Recursive struct { + Foo string `mapstructure:"foo"` +} + +func (r *Recursive) Unmarshal(conf *Conf) error { + newR := &Recursive{} + if err := conf.Unmarshal(newR); err != nil { + return err + } + *r = *newR + return nil +} + +// Tests that a struct can unmarshal itself by creating a new copy of itself, unmarshaling itself, and setting its value. +func TestRecursiveUnmarshaling(t *testing.T) { + t.Skip("this test fails to run because it engages in recursive unmarshaling") + conf := NewFromStringMap(map[string]any{ + "foo": "something", + }) + r := &Recursive{} + require.NoError(t, conf.Unmarshal(r)) + require.Equal(t, "something", r.Foo) +}