Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Public fields of embedded private struct not copied #17

Open
sbinq opened this issue Jan 6, 2017 · 2 comments
Open

Public fields of embedded private struct not copied #17

sbinq opened this issue Jan 6, 2017 · 2 comments

Comments

@sbinq
Copy link

sbinq commented Jan 6, 2017

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
@mitchellh
Copy link
Owner

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)
	}
}

@sbinq
Copy link
Author

sbinq commented Jan 9, 2017

I see, thank you for clarification.

@mitchellh mitchellh reopened this Jul 15, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants