joao-r-reis commented on code in PR #1939:
URL:
https://github.com/apache/cassandra-gocql-driver/pull/1939#discussion_r3017249067
##########
marshal.go:
##########
@@ -2923,9 +2923,13 @@ func (udt UDTTypeInfo) Marshal(value interface{})
([]byte, error) {
func (udt UDTTypeInfo) Unmarshal(data []byte, value interface{}) error {
// do this up here so we don't need to duplicate all of the map logic
below
if iptr, ok := value.(*interface{}); ok && iptr != nil {
- v := map[string]interface{}{}
- *iptr = v
+ var v map[string]interface{}
value = &v
+ // Deffering this to the end of the method so dst var will hold
updated map value
+ //
https://issues.apache.org/jira/projects/CASSGO/issues/CASSGO-115
+ defer func() {
+ *iptr = v
+ }()
}
Review Comment:
something roughly like this:
```
func (udt UDTTypeInfo) Unmarshal(data []byte, value interface{}) error {
switch v := value.(type) {
case *interface{}:
if v == nil {
return unmarshalErrorf("can not unmarshal into nil *interface{}")
}
// Unmarshal into a temporary map, then assign to *interface{}
m := map[string]interface{}{}
if err := udt.unmarshalIntoMap(data, &m); err != nil {
return err
}
*v = m
return nil
case UDTUnmarshaler:
// ... existing code ...
case *map[string]interface{}:
return udt.unmarshalIntoMap(data, v)
// ... rest of cases ...
}
}
// Extract common map unmarshaling logic
func (udt UDTTypeInfo) unmarshalIntoMap(data []byte, v
*map[string]interface{}) error {
if data == nil {
*v = nil
return nil
}
m := map[string]interface{}{}
for id, e := range udt.Elements {
if len(data) == 0 {
break
}
if len(data) < 4 {
return unmarshalErrorf("can not unmarshal UDT: field [%d]%s:
unexpected eof", id, e.Name)
}
var p []byte
p, data = readBytes(data)
elemValue := reflect.New(reflect.TypeOf(e.Type.Zero()))
if err := Unmarshal(e.Type, p, elemValue.Interface()); err != nil {
return err
}
m[e.Name] = elemValue.Elem().Interface()
}
*v = m
return nil
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]