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 was 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
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: