copystructure icon indicating copy to clipboard operation
copystructure copied to clipboard

Public fields of embedded private struct not copied

Open sbinq opened this issue 8 years ago • 2 comments

Described case seems possible to implement I suppose (although did not look into the details yet) - at least "encoding/json" package manages to do this:

package main

import (
	"encoding/json"
	"reflect"
	"testing"

	"github.com/mitchellh/copystructure"
	"github.com/stretchr/testify/require"
)

func TestCopyPrivateEmbeddedStructWithPublicFields(t *testing.T) {
	type subStruct struct {
		Field string
	}

	type Struct struct {
		subStruct
	}

	input := Struct{
		subStruct: subStruct{
			Field: "111",
		},
	}

	{
		out, err := copyViaJsonMarshalUnmarshal(input)
		require.Nil(t, err)
		require.Equal(t, input, out, "json package does marshal and unmarshal public fields of private embedded struct - so it might be possible")
	}
	{
		out, err := copystructure.Copy(input)
		require.Nil(t, err)
		require.Equal(t, input, out, "copystructure does not copy public fields of private embedded struct")
	}
}

func copyViaJsonMarshalUnmarshal(v interface{}) (interface{}, error) {
	b, err := json.Marshal(v)
	if err != nil {
		return nil, err
	}

	dest := reflect.New(reflect.ValueOf(v).Type()).Interface()

	if err := json.Unmarshal(b, dest); err != nil {
		return nil, err
	}

	return reflect.ValueOf(dest).Elem().Interface(), nil
}

Output:

=== RUN   TestCopyPrivateEmbeddedStructWithPublicFields
--- FAIL: TestCopyPrivateEmbeddedStructWithPublicFields (0.00s)
        Error Trace:    sample_test.go:35
	Error:		Not equal: 
			expected: main.Struct{subStruct:main.subStruct{Field:"111"}}
			received: main.Struct{subStruct:main.subStruct{Field:""}}
			
			Diff:
			--- Expected
			+++ Actual
			@@ -2,3 +2,3 @@
			  subStruct: (main.subStruct) {
			-  Field: (string) (len=3) "111"
			+  Field: (string) ""
			  }
	Messages:	copystructure does not copy public fields of private embedded struct
		
FAIL
exit status 1
FAIL	_/home/sbinq/work/gopath/src	0.002s

sbinq avatar Jan 06 '17 16:01 sbinq

This isn't possible for copystructure (compared to the JSON package) because we can't actually create a new unexported struct value (we can't create a subStruct{} in your example). The encoding/json package copies contents directly into exported fields of an already allocated struct.

What I'm saying is probably not strictly true. copystructure could likely be more clever around non-pointer fields knowing it doesn't need to "allocate" them at all and can directly access their exported fields...

This is currently not super close to how copystructure works so I'm going to just leave this open. Its possible to fix this but not very trivial. Here is a test case though:

func TestCopy_nestedStructUnexportedEmbedded(t *testing.T) {
	type subTest struct {
		Sub string
	}

	type test struct {
		subTest

		Value string
	}

	v := test{Value: "foo", subTest: subTest{Sub: "bar"}}

	result, err := Copy(v)
	if err != nil {
		t.Fatalf("err: %s", err)
	}

	if !reflect.DeepEqual(result, v) {
		t.Fatalf("bad: %#v", result)
	}
}

mitchellh avatar Jan 07 '17 08:01 mitchellh

I see, thank you for clarification.

sbinq avatar Jan 09 '17 09:01 sbinq