You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.
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
The text was updated successfully, but these errors were encountered:
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:
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:
Output:
The text was updated successfully, but these errors were encountered: