A type that has a property of another struct that proxies a primitive like a string cannot be unmarshaled after being marshaled with the following error: "positioned on string, but attempted to read embedded document".
Example code:
type encValueWrapper struct {
EncValue EncryptedValue
}
type EncryptedValue struct {
value string
rawValue []byte
}
func (ev EncryptedValue) DecryptValue(crypter Crypter, out interface{}) error {
if ev.value != "" {
return crypter.DecryptValue(ev.value, out)
}
return crypter.DecryptParsedValue(ev.rawValue, out)
}
func (ev EncryptedValue) ProxyBSON() (interface{}, error) {
if ev.value == "" {
return nil, ErrNotEncryptedValue
}
return ev.value, nil
}
func (ev *EncryptedValue) UnmarshalBSONValue(t bsontype.Type, data []byte) error {
temp, err := NewEncryptedValueFromBSON(bson.RawValue{Type: t, Value: data})
if err != nil {
return err
}
*ev = temp
return nil
}