jameshartig commented on code in PR #1855:
URL:
https://github.com/apache/cassandra-gocql-driver/pull/1855#discussion_r2127475953
##########
marshal.go:
##########
@@ -1909,92 +2008,113 @@ func marshalMap(info TypeInfo, value interface{})
([]byte, error) {
buf := &bytes.Buffer{}
n := rv.Len()
- if err := writeCollectionSize(mapInfo, n, buf); err != nil {
+ if err := writeCollectionSize(c.proto, n, buf); err != nil {
return nil, err
}
keys := rv.MapKeys()
for _, key := range keys {
- item, err := Marshal(mapInfo.Key, key.Interface())
+ item, err := Marshal(c.Key, key.Interface())
if err != nil {
return nil, err
}
itemLen := len(item)
// Set the key to null for supported protocols
- if item == nil && mapInfo.proto > protoVersion2 {
+ if item == nil && c.proto > protoVersion2 {
itemLen = -1
}
- if err := writeCollectionSize(mapInfo, itemLen, buf); err !=
nil {
+ if err := writeCollectionSize(c.proto, itemLen, buf); err !=
nil {
return nil, err
}
buf.Write(item)
- item, err = Marshal(mapInfo.Elem, rv.MapIndex(key).Interface())
+ item, err = Marshal(c.Elem, rv.MapIndex(key).Interface())
if err != nil {
return nil, err
}
itemLen = len(item)
// Set the value to null for supported protocols
- if item == nil && mapInfo.proto > protoVersion2 {
+ if item == nil && c.proto > protoVersion2 {
itemLen = -1
}
- if err := writeCollectionSize(mapInfo, itemLen, buf); err !=
nil {
+ if err := writeCollectionSize(c.proto, itemLen, buf); err !=
nil {
return nil, err
}
buf.Write(item)
}
return buf.Bytes(), nil
}
-func unmarshalMap(info TypeInfo, data []byte, value interface{}) error {
- mapInfo, ok := info.(CollectionType)
- if !ok {
- return unmarshalErrorf("unmarshal: can not unmarshal none
collection type into map")
- }
-
+func (c CollectionType) unmarshalMap(data []byte, value interface{}) error {
rv := reflect.ValueOf(value)
if rv.Kind() != reflect.Ptr {
- return unmarshalErrorf("can not unmarshal into non-pointer %T",
value)
+ return unmarshalErrorf("can not unmarshal map into non-pointer
%T", value)
}
rv = rv.Elem()
t := rv.Type()
- if t.Kind() != reflect.Map {
- return unmarshalErrorf("can not unmarshal %s into %T", info,
value)
+ if t.Kind() == reflect.Interface {
+ if t.NumMethod() != 0 {
+ return unmarshalErrorf("can not unmarshal map into
non-empty interface %T", value)
+ }
+ var key interface{}
+ // this relies on Unmarshal marshalling default values when
presented with nil
+ if err := Unmarshal(c.Key, []byte(nil), &key); err != nil {
+ return err
+ }
+ if key == nil {
+ panic(fmt.Errorf("key was nil after unmarshalling from
%T", c.Key))
Review Comment:
I'll go through and either comment or replace all of them, thanks for the
reminder!
--
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]